blob: 8b4cbc8cdf481bf9f3a8253b1cc6ba68564bdd46 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* Definitions for POSIX timer implementation on top of NPTL.
2 Copyright (C) 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21#include <limits.h>
22#include <signal.h>
23
24/* Double linked list. */
25struct list_links
26{
27 struct list_links *next;
28 struct list_links *prev;
29};
30
31
32/* Forward declaration. */
33struct timer_node;
34
35
36/* Definitions for an internal thread of the POSIX timer implementation. */
37struct thread_node
38{
39 struct list_links links;
40 pthread_attr_t attr;
41 pthread_t id;
42 unsigned int exists;
43 struct list_links timer_queue;
44 pthread_cond_t cond;
45 struct timer_node *current_timer;
46 pthread_t captured;
47 clockid_t clock_id;
48};
49
50
51/* Internal representation of a timer. */
52struct timer_node
53{
54 struct list_links links;
55 struct sigevent event;
56 clockid_t clock;
57 struct itimerspec value;
58 struct timespec expirytime;
59 pthread_attr_t attr;
60 unsigned int abstime;
61 unsigned int armed;
62 enum {
63 TIMER_FREE, TIMER_INUSE, TIMER_DELETED
64 } inuse;
65 struct thread_node *thread;
66 pid_t creator_pid;
67 int refcount;
68 int overrun_count;
69};
70
71
72/* The limit is not published if we are compiled with kernel timer support.
73 But we still compiled in this implementation with its limit unless built
74 to require the kernel support. */
75#ifndef TIMER_MAX
76# define TIMER_MAX 256
77#endif
78
79/* Static array with the structures for all the timers. */
80extern struct timer_node __timer_array[TIMER_MAX];
81
82/* Global lock to protect operation on the lists. */
83extern pthread_mutex_t __timer_mutex;
84
85/* Variable to protext initialization. */
86extern pthread_once_t __timer_init_once_control;
87
88/* Nonzero if initialization of timer implementation failed. */
89extern int __timer_init_failed;
90
91/* Node for the thread used to deliver signals. */
92extern struct thread_node __timer_signal_thread_rclk;
93
94
95/* Return pointer to timer structure corresponding to ID. */
96#define timer_id2ptr(timerid) ((struct timer_node *) timerid)
97#define timer_ptr2id(timerid) ((void *) timerid)
98
99/* Check whether timer is valid; global mutex must be held. */
100static inline int
101timer_valid (struct timer_node *timer)
102{
103 return timer && timer->inuse == TIMER_INUSE;
104}
105
106/* Timer refcount functions; need global mutex. */
107extern void __timer_dealloc (struct timer_node *timer);
108
109static inline void
110timer_addref (struct timer_node *timer)
111{
112 timer->refcount++;
113}
114
115static inline void
116timer_delref (struct timer_node *timer)
117{
118 if (--timer->refcount == 0)
119 __timer_dealloc (timer);
120}
121
122/* Timespec helper routines. */
123static inline int
124__attribute ((always_inline))
125timespec_compare (const struct timespec *left, const struct timespec *right)
126{
127 if (left->tv_sec < right->tv_sec)
128 return -1;
129 if (left->tv_sec > right->tv_sec)
130 return 1;
131
132 if (left->tv_nsec < right->tv_nsec)
133 return -1;
134 if (left->tv_nsec > right->tv_nsec)
135 return 1;
136
137 return 0;
138}
139
140static inline void
141timespec_add (struct timespec *sum, const struct timespec *left,
142 const struct timespec *right)
143{
144 sum->tv_sec = left->tv_sec + right->tv_sec;
145 sum->tv_nsec = left->tv_nsec + right->tv_nsec;
146
147 if (sum->tv_nsec >= 1000000000)
148 {
149 ++sum->tv_sec;
150 sum->tv_nsec -= 1000000000;
151 }
152}
153
154static inline void
155timespec_sub (struct timespec *diff, const struct timespec *left,
156 const struct timespec *right)
157{
158 diff->tv_sec = left->tv_sec - right->tv_sec;
159 diff->tv_nsec = left->tv_nsec - right->tv_nsec;
160
161 if (diff->tv_nsec < 0)
162 {
163 --diff->tv_sec;
164 diff->tv_nsec += 1000000000;
165 }
166}
167
168
169/* We need one of the list functions in the other modules. */
170static inline void
171list_unlink_ip (struct list_links *list)
172{
173 struct list_links *lnext = list->next, *lprev = list->prev;
174
175 lnext->prev = lprev;
176 lprev->next = lnext;
177
178 /* The suffix ip means idempotent; list_unlink_ip can be called
179 * two or more times on the same node.
180 */
181
182 list->next = list;
183 list->prev = list;
184}
185
186
187/* Functions in the helper file. */
188extern void __timer_mutex_cancel_handler (void *arg);
189extern void __timer_init_once (void);
190extern struct timer_node *__timer_alloc (void);
191extern int __timer_thread_start (struct thread_node *thread);
192extern struct thread_node *__timer_thread_find_matching (const pthread_attr_t *desired_attr, clockid_t);
193extern struct thread_node *__timer_thread_alloc (const pthread_attr_t *desired_attr, clockid_t);
194extern void __timer_thread_dealloc (struct thread_node *thread);
195extern int __timer_thread_queue_timer (struct thread_node *thread,
196 struct timer_node *insert);
197extern void __timer_thread_wakeup (struct thread_node *thread);