hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame nptl/pthread_clock_gettime.c

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