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