1 | /* Copyright (C) 2003-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2003. |
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 |
17 | not, see <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <pthread.h> |
20 | #include <setjmp.h> |
21 | #include <signal.h> |
22 | #include <sys/types.h> |
23 | |
24 | |
25 | /* Nonzero if the system calls are not available. */ |
26 | extern int __no_posix_timers attribute_hidden; |
27 | |
28 | /* Callback to start helper thread. */ |
29 | extern void __start_helper_thread (void) attribute_hidden; |
30 | |
31 | /* Control variable for helper thread creation. */ |
32 | extern pthread_once_t __helper_once attribute_hidden; |
33 | |
34 | /* TID of the helper thread. */ |
35 | extern pid_t __helper_tid attribute_hidden; |
36 | |
37 | /* List of active SIGEV_THREAD timers. */ |
38 | extern struct timer *__active_timer_sigev_thread attribute_hidden; |
39 | /* Lock for the __active_timer_sigev_thread. */ |
40 | extern pthread_mutex_t __active_timer_sigev_thread_lock attribute_hidden; |
41 | |
42 | |
43 | /* Type of timers in the kernel. */ |
44 | typedef int kernel_timer_t; |
45 | |
46 | /* Internal representation of SIGEV_THREAD timer. */ |
47 | struct timer |
48 | { |
49 | kernel_timer_t ktimerid; |
50 | |
51 | void (*thrfunc) (sigval_t); |
52 | sigval_t sival; |
53 | pthread_attr_t attr; |
54 | |
55 | /* Next element in list of active SIGEV_THREAD timers. */ |
56 | struct timer *next; |
57 | }; |
58 | |
59 | |
60 | /* For !SIGEV_THREAD, the resulting 'timer_t' is the returned kernel timer |
61 | identifer (kernel_timer_t), while for SIGEV_THREAD it uses the fact malloc |
62 | returns at least _Alignof (max_align_t) pointers plus that valid |
63 | kernel_timer_t are always positive to set the MSB bit of the returned |
64 | 'timer_t' to indicate the timer handles a SIGEV_THREAD. */ |
65 | |
66 | static inline timer_t |
67 | kernel_timer_to_timerid (kernel_timer_t ktimerid) |
68 | { |
69 | return (timer_t) ((intptr_t) ktimerid); |
70 | } |
71 | |
72 | static inline timer_t |
73 | timer_to_timerid (struct timer *ptr) |
74 | { |
75 | return (timer_t) (INTPTR_MIN | (uintptr_t) ptr >> 1); |
76 | } |
77 | |
78 | static inline bool |
79 | timer_is_sigev_thread (timer_t timerid) |
80 | { |
81 | return (intptr_t) timerid < 0; |
82 | } |
83 | |
84 | static inline struct timer * |
85 | timerid_to_timer (timer_t timerid) |
86 | { |
87 | return (struct timer *)((uintptr_t) timerid << 1); |
88 | } |
89 | |
90 | static inline kernel_timer_t |
91 | timerid_to_kernel_timer (timer_t timerid) |
92 | { |
93 | if (timer_is_sigev_thread (timerid)) |
94 | return timerid_to_timer (timerid)->ktimerid; |
95 | else |
96 | return (kernel_timer_t) ((uintptr_t) timerid); |
97 | } |
98 | |