Blame nptl/pthread_clock_settime.c

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