1 | /* Definitions for POSIX timer implementation on top of NPTL. |
2 | Copyright (C) 2000-2022 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 |
17 | not, see <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <limits.h> |
20 | #include <signal.h> |
21 | #include <list.h> |
22 | |
23 | |
24 | /* Forward declaration. */ |
25 | struct timer_node; |
26 | |
27 | |
28 | /* Definitions for an internal thread of the POSIX timer implementation. */ |
29 | struct thread_node |
30 | { |
31 | struct list_head links; |
32 | pthread_attr_t attr; |
33 | pthread_t id; |
34 | unsigned int exists; |
35 | struct list_head timer_queue; |
36 | pthread_cond_t cond; |
37 | struct timer_node *current_timer; |
38 | pthread_t captured; |
39 | clockid_t clock_id; |
40 | }; |
41 | |
42 | |
43 | /* Internal representation of a timer. */ |
44 | struct timer_node |
45 | { |
46 | struct list_head links; |
47 | struct sigevent event; |
48 | clockid_t clock; |
49 | struct itimerspec value; |
50 | struct timespec expirytime; |
51 | pthread_attr_t attr; |
52 | unsigned int abstime; |
53 | unsigned int armed; |
54 | enum { |
55 | TIMER_FREE, TIMER_INUSE, TIMER_DELETED |
56 | } inuse; |
57 | struct thread_node *thread; |
58 | pid_t creator_pid; |
59 | int refcount; |
60 | int overrun_count; |
61 | }; |
62 | |
63 | |
64 | /* The limit is not published if we are compiled with kernel timer support. |
65 | But we still compiled in this implementation with its limit unless built |
66 | to require the kernel support. */ |
67 | #ifndef TIMER_MAX |
68 | # define TIMER_MAX 256 |
69 | #endif |
70 | |
71 | /* Static array with the structures for all the timers. */ |
72 | extern struct timer_node __timer_array[TIMER_MAX]; |
73 | |
74 | /* Global lock to protect operation on the lists. */ |
75 | extern pthread_mutex_t __timer_mutex; |
76 | |
77 | /* Variable to protext initialization. */ |
78 | extern pthread_once_t __timer_init_once_control; |
79 | |
80 | /* Nonzero if initialization of timer implementation failed. */ |
81 | extern int __timer_init_failed; |
82 | |
83 | /* Node for the thread used to deliver signals. */ |
84 | extern struct thread_node __timer_signal_thread_rclk; |
85 | |
86 | |
87 | /* Return pointer to timer structure corresponding to ID. */ |
88 | #define timer_id2ptr(timerid) ((struct timer_node *) timerid) |
89 | #define timer_ptr2id(timerid) ((timer_t) timerid) |
90 | |
91 | /* Check whether timer is valid; global mutex must be held. */ |
92 | static inline int |
93 | timer_valid (struct timer_node *timer) |
94 | { |
95 | return timer && timer->inuse == TIMER_INUSE; |
96 | } |
97 | |
98 | /* Timer refcount functions; need global mutex. */ |
99 | extern void __timer_dealloc (struct timer_node *timer); |
100 | |
101 | static inline void |
102 | timer_addref (struct timer_node *timer) |
103 | { |
104 | timer->refcount++; |
105 | } |
106 | |
107 | static inline void |
108 | timer_delref (struct timer_node *timer) |
109 | { |
110 | if (--timer->refcount == 0) |
111 | __timer_dealloc (timer); |
112 | } |
113 | |
114 | /* Timespec helper routines. */ |
115 | static inline int |
116 | __attribute ((always_inline)) |
117 | timespec_compare (const struct timespec *left, const struct timespec *right) |
118 | { |
119 | if (left->tv_sec < right->tv_sec) |
120 | return -1; |
121 | if (left->tv_sec > right->tv_sec) |
122 | return 1; |
123 | |
124 | if (left->tv_nsec < right->tv_nsec) |
125 | return -1; |
126 | if (left->tv_nsec > right->tv_nsec) |
127 | return 1; |
128 | |
129 | return 0; |
130 | } |
131 | |
132 | static inline void |
133 | timespec_add (struct timespec *sum, const struct timespec *left, |
134 | const struct timespec *right) |
135 | { |
136 | sum->tv_sec = left->tv_sec + right->tv_sec; |
137 | sum->tv_nsec = left->tv_nsec + right->tv_nsec; |
138 | |
139 | if (sum->tv_nsec >= 1000000000) |
140 | { |
141 | ++sum->tv_sec; |
142 | sum->tv_nsec -= 1000000000; |
143 | } |
144 | } |
145 | |
146 | static inline void |
147 | timespec_sub (struct timespec *diff, const struct timespec *left, |
148 | const struct timespec *right) |
149 | { |
150 | diff->tv_sec = left->tv_sec - right->tv_sec; |
151 | diff->tv_nsec = left->tv_nsec - right->tv_nsec; |
152 | |
153 | if (diff->tv_nsec < 0) |
154 | { |
155 | --diff->tv_sec; |
156 | diff->tv_nsec += 1000000000; |
157 | } |
158 | } |
159 | |
160 | |
161 | /* We need one of the list functions in the other modules. */ |
162 | static inline void |
163 | list_unlink_ip (struct list_head *list) |
164 | { |
165 | struct list_head *lnext = list->next, *lprev = list->prev; |
166 | |
167 | lnext->prev = lprev; |
168 | lprev->next = lnext; |
169 | |
170 | /* The suffix ip means idempotent; list_unlink_ip can be called |
171 | * two or more times on the same node. |
172 | */ |
173 | |
174 | list->next = list; |
175 | list->prev = list; |
176 | } |
177 | |
178 | |
179 | /* Functions in the helper file. */ |
180 | extern void __timer_mutex_cancel_handler (void *arg); |
181 | extern void __timer_init_once (void); |
182 | extern struct timer_node *__timer_alloc (void); |
183 | extern int __timer_thread_start (struct thread_node *thread); |
184 | extern struct thread_node *__timer_thread_find_matching (const pthread_attr_t *desired_attr, clockid_t); |
185 | extern struct thread_node *__timer_thread_alloc (const pthread_attr_t *desired_attr, clockid_t); |
186 | extern void __timer_thread_dealloc (struct thread_node *thread); |
187 | extern int __timer_thread_queue_timer (struct thread_node *thread, |
188 | struct timer_node *insert); |
189 | extern void __timer_thread_wakeup (struct thread_node *thread); |
190 | |