yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* pthread_getcpuclockid -- Get POSIX clockid_t for a pthread_t. Linux version |
| 2 | Copyright (C) 2000, 2001, 2004, 2005 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public License as |
| 7 | published by the Free Software Foundation; either version 2.1 of the |
| 8 | License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; see the file COPYING.LIB. If not, |
| 17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 | Boston, MA 02111-1307, USA. */ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <pthread.h> |
| 22 | #include <sys/time.h> |
| 23 | #include <time.h> |
| 24 | #include <internals.h> |
| 25 | #include <spinlock.h> |
| 26 | #include <bits/kernel-features.h> |
| 27 | #include <kernel-posix-cpu-timers.h> |
| 28 | |
| 29 | |
| 30 | #if !(__ASSUME_POSIX_CPU_TIMERS > 0) |
| 31 | int __libc_missing_posix_cpu_timers attribute_hidden; |
| 32 | #endif |
| 33 | #if !(__ASSUME_POSIX_TIMERS > 0) |
| 34 | int __libc_missing_posix_timers attribute_hidden; |
| 35 | #endif |
| 36 | |
| 37 | int |
| 38 | pthread_getcpuclockid (pthread_t thread_id, clockid_t *clock_id) |
| 39 | { |
| 40 | #ifdef __NR_clock_getres |
| 41 | pthread_handle handle = thread_handle(thread_id); |
| 42 | int pid; |
| 43 | |
| 44 | __pthread_lock (&handle->h_lock, NULL); |
| 45 | if (nonexisting_handle (handle, thread_id)) |
| 46 | { |
| 47 | __pthread_unlock (&handle->h_lock); |
| 48 | return ESRCH; |
| 49 | } |
| 50 | pid = handle->h_descr->p_pid; |
| 51 | __pthread_unlock (&handle->h_lock); |
| 52 | |
| 53 | /* The clockid_t value is a simple computation from the PID. |
| 54 | But we do a clock_getres call to validate it if we aren't |
| 55 | yet sure we have the kernel support. */ |
| 56 | |
| 57 | const clockid_t pidclock = MAKE_PROCESS_CPUCLOCK (pid, CPUCLOCK_SCHED); |
| 58 | |
| 59 | # if !(__ASSUME_POSIX_CPU_TIMERS > 0) |
| 60 | # if !(__ASSUME_POSIX_TIMERS > 0) |
| 61 | if (__libc_missing_posix_timers && !__libc_missing_posix_cpu_timers) |
| 62 | __libc_missing_posix_cpu_timers = 1; |
| 63 | # endif |
| 64 | if (!__libc_missing_posix_cpu_timers) |
| 65 | { |
| 66 | INTERNAL_SYSCALL_DECL (err); |
| 67 | int r = INTERNAL_SYSCALL (clock_getres, err, 2, pidclock, NULL); |
| 68 | if (!INTERNAL_SYSCALL_ERROR_P (r, err)) |
| 69 | # endif |
| 70 | { |
| 71 | *clock_id = pidclock; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | # if !(__ASSUME_POSIX_CPU_TIMERS > 0) |
| 76 | # if !(__ASSUME_POSIX_TIMERS > 0) |
| 77 | if (INTERNAL_SYSCALL_ERRNO (r, err) == ENOSYS) |
| 78 | { |
| 79 | /* The kernel doesn't support these calls at all. */ |
| 80 | __libc_missing_posix_timers = 1; |
| 81 | __libc_missing_posix_cpu_timers = 1; |
| 82 | } |
| 83 | else |
| 84 | # endif |
| 85 | if (INTERNAL_SYSCALL_ERRNO (r, err) == EINVAL) |
| 86 | { |
| 87 | /* The kernel doesn't support these clocks at all. */ |
| 88 | __libc_missing_posix_cpu_timers = 1; |
| 89 | } |
| 90 | else |
| 91 | return INTERNAL_SYSCALL_ERRNO (r, err); |
| 92 | } |
| 93 | # endif |
| 94 | #endif |
| 95 | |
| 96 | #ifdef CLOCK_THREAD_CPUTIME_ID |
| 97 | /* We need to store the thread ID in the CLOCKID variable together |
| 98 | with a number identifying the clock. We reserve the low 3 bits |
| 99 | for the clock ID and the rest for the thread ID. This is |
| 100 | problematic if the thread ID is too large. But 29 bits should be |
| 101 | fine. |
| 102 | |
| 103 | If some day more clock IDs are needed the ID part can be |
| 104 | enlarged. The IDs are entirely internal. */ |
| 105 | if (2 * PTHREAD_THREADS_MAX |
| 106 | >= 1 << (8 * sizeof (*clock_id) - CLOCK_IDFIELD_SIZE)) |
| 107 | return ERANGE; |
| 108 | |
| 109 | /* Store the number. */ |
| 110 | *clock_id = CLOCK_THREAD_CPUTIME_ID | (thread_id << CLOCK_IDFIELD_SIZE); |
| 111 | |
| 112 | return 0; |
| 113 | #else |
| 114 | /* We don't have a timer for that. */ |
| 115 | return ENOENT; |
| 116 | #endif |
| 117 | } |