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