lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 2000-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public License as |
| 6 | published by the Free Software Foundation; either version 2.1 of the |
| 7 | License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; see the file COPYING.LIB. If |
| 16 | not, see <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <pthreadP.h> |
| 20 | #include <sys/time.h> |
| 21 | #include <tls.h> |
| 22 | |
| 23 | |
| 24 | int |
| 25 | pthread_getcpuclockid (threadid, clockid) |
| 26 | pthread_t threadid; |
| 27 | clockid_t *clockid; |
| 28 | { |
| 29 | struct pthread *pd = (struct pthread *) threadid; |
| 30 | |
| 31 | /* Make sure the descriptor is valid. */ |
| 32 | if (INVALID_TD_P (pd)) |
| 33 | /* Not a valid thread handle. */ |
| 34 | return ESRCH; |
| 35 | |
| 36 | #ifdef CLOCK_THREAD_CPUTIME_ID |
| 37 | /* We need to store the thread ID in the CLOCKID variable together |
| 38 | with a number identifying the clock. We reserve the low 3 bits |
| 39 | for the clock ID and the rest for the thread ID. This is |
| 40 | problematic if the thread ID is too large. But 29 bits should be |
| 41 | fine. |
| 42 | |
| 43 | If some day more clock IDs are needed the ID part can be |
| 44 | enlarged. The IDs are entirely internal. */ |
| 45 | if (pd->tid >= 1 << (8 * sizeof (*clockid) - CLOCK_IDFIELD_SIZE)) |
| 46 | return ERANGE; |
| 47 | |
| 48 | /* Store the number. */ |
| 49 | *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE); |
| 50 | |
| 51 | return 0; |
| 52 | #else |
| 53 | /* We don't have a timer for that. */ |
| 54 | return ENOENT; |
| 55 | #endif |
| 56 | } |