|
Packit Service |
c82647 |
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
|
|
Packit Service |
c82647 |
This file is part of the GNU C Library.
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
c82647 |
modify it under the terms of the GNU Lesser General Public License as
|
|
Packit Service |
c82647 |
published by the Free Software Foundation; either version 2.1 of the
|
|
Packit Service |
c82647 |
License, or (at your option) any later version.
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
c82647 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
c82647 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
c82647 |
Lesser General Public License for more details.
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
c82647 |
License along with the GNU C Library; see the file COPYING.LIB. If
|
|
Packit Service |
c82647 |
not, see <http://www.gnu.org/licenses/>. */
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
#include <errno.h>
|
|
Packit Service |
c82647 |
#include <stdlib.h>
|
|
Packit Service |
c82647 |
#include <time.h>
|
|
Packit Service |
c82647 |
#include "pthreadP.h"
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
#if HP_TIMING_AVAIL
|
|
Packit Service |
c82647 |
int
|
|
Packit Service |
c82647 |
__pthread_clock_gettime (clockid_t clock_id, hp_timing_t freq,
|
|
Packit Service |
c82647 |
struct timespec *tp)
|
|
Packit Service |
c82647 |
{
|
|
Packit Service |
c82647 |
hp_timing_t tsc;
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
/* Get the current counter. */
|
|
Packit Service |
c82647 |
HP_TIMING_NOW (tsc);
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
/* This is the ID of the thread we are looking for. */
|
|
Packit Service |
c82647 |
pid_t tid = ((unsigned int) clock_id) >> CLOCK_IDFIELD_SIZE;
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
/* Compute the offset since the start time of the process. */
|
|
Packit Service |
c82647 |
if (tid == 0 || tid == THREAD_GETMEM (THREAD_SELF, tid))
|
|
Packit Service |
c82647 |
/* Our own clock. */
|
|
Packit Service |
c82647 |
tsc -= THREAD_GETMEM (THREAD_SELF, cpuclock_offset);
|
|
Packit Service |
c82647 |
else
|
|
Packit Service |
c82647 |
{
|
|
Packit Service |
c82647 |
/* This is more complicated. We have to locate the thread based
|
|
Packit Service |
c82647 |
on the ID. This means walking the list of existing
|
|
Packit Service |
c82647 |
threads. */
|
|
Packit Service |
c82647 |
struct pthread *thread = __find_thread_by_id (tid);
|
|
Packit Service |
c82647 |
if (thread == NULL)
|
|
Packit Service |
c82647 |
{
|
|
Packit Service |
c82647 |
__set_errno (EINVAL);
|
|
Packit Service |
c82647 |
return -1;
|
|
Packit Service |
c82647 |
}
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
/* There is a race here. The thread might terminate and the stack
|
|
Packit Service |
c82647 |
become unusable. But this is the user's problem. */
|
|
Packit Service |
c82647 |
tsc -= thread->cpuclock_offset;
|
|
Packit Service |
c82647 |
}
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
/* Compute the seconds. */
|
|
Packit Service |
c82647 |
tp->tv_sec = tsc / freq;
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
/* And the nanoseconds. This computation should be stable until
|
|
Packit Service |
c82647 |
we get machines with about 16GHz frequency. */
|
|
Packit Service |
c82647 |
tp->tv_nsec = ((tsc % freq) * 1000000000ull) / freq;
|
|
Packit Service |
c82647 |
|
|
Packit Service |
c82647 |
return 0;
|
|
Packit Service |
c82647 |
}
|
|
Packit Service |
c82647 |
#endif
|