]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re-core/uclibc/lib/contrib/uclibc/libpthread/linuxthreads/sysdeps/pthread/posix-timer.h
Update
[l4.git] / l4 / pkg / l4re-core / uclibc / lib / contrib / uclibc / libpthread / linuxthreads / sysdeps / pthread / posix-timer.h
1 /* Definitions for POSIX timer implementation on top of LinuxThreads.
2    Copyright (C) 2000, 2002, 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
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 /* 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 /* Nodes for the threads used to deliver signals.  */
84 /* A distinct thread is used for each clock type.  */
85
86 extern struct thread_node __timer_signal_thread_rclk;
87
88
89 /* Return pointer to timer structure corresponding to ID.  */
90 static __inline__ struct timer_node *
91 timer_id2ptr (timer_t timerid)
92 {
93   if (timerid >= 0 && timerid < TIMER_MAX)
94     return &__timer_array[timerid];
95
96   return NULL;
97 }
98
99 /* Return ID of TIMER.  */
100 static __inline__ int
101 timer_ptr2id (struct timer_node *timer)
102 {
103   return timer - __timer_array;
104 }
105
106 /* Check whether timer is valid; global mutex must be held. */
107 static __inline__ int
108 timer_valid (struct timer_node *timer)
109 {
110   return timer && timer->inuse == TIMER_INUSE;
111 }
112
113 /* Timer refcount functions; need global mutex. */
114 extern void __timer_dealloc (struct timer_node *timer);
115
116 static __inline__ void
117 timer_addref (struct timer_node *timer)
118 {
119   timer->refcount++;
120 }
121
122 static __inline__ void
123 timer_delref (struct timer_node *timer)
124 {
125   if (--timer->refcount == 0)
126     __timer_dealloc (timer);
127 }
128
129 /* Timespec helper routines.  */
130 static __inline__ int
131 timespec_compare (const struct timespec *left, const struct timespec *right)
132 {
133   if (left->tv_sec < right->tv_sec)
134     return -1;
135   if (left->tv_sec > right->tv_sec)
136     return 1;
137
138   if (left->tv_nsec < right->tv_nsec)
139     return -1;
140   if (left->tv_nsec > right->tv_nsec)
141     return 1;
142
143   return 0;
144 }
145
146 static __inline__ void
147 timespec_add (struct timespec *sum, const struct timespec *left,
148               const struct timespec *right)
149 {
150   sum->tv_sec = left->tv_sec + right->tv_sec;
151   sum->tv_nsec = left->tv_nsec + right->tv_nsec;
152
153   if (sum->tv_nsec >= 1000000000)
154     {
155       ++sum->tv_sec;
156       sum->tv_nsec -= 1000000000;
157     }
158 }
159
160 static __inline__ void
161 timespec_sub (struct timespec *diff, const struct timespec *left,
162               const struct timespec *right)
163 {
164   diff->tv_sec = left->tv_sec - right->tv_sec;
165   diff->tv_nsec = left->tv_nsec - right->tv_nsec;
166
167   if (diff->tv_nsec < 0)
168     {
169       --diff->tv_sec;
170       diff->tv_nsec += 1000000000;
171     }
172 }
173
174
175 /* We need one of the list functions in the other modules.  */
176 static __inline__ void
177 list_unlink_ip (struct list_links *list)
178 {
179   struct list_links *lnext = list->next, *lprev = list->prev;
180
181   lnext->prev = lprev;
182   lprev->next = lnext;
183
184   /* The suffix ip means idempotent; list_unlink_ip can be called
185    * two or more times on the same node.
186    */
187
188   list->next = list;
189   list->prev = list;
190 }
191
192
193 /* Functions in the helper file.  */
194 extern void __timer_mutex_cancel_handler (void *arg);
195 extern void __timer_init_once (void);
196 extern struct timer_node *__timer_alloc (void);
197 extern int __timer_thread_start (struct thread_node *thread);
198 extern struct thread_node *__timer_thread_find_matching (const pthread_attr_t *desired_attr, clockid_t);
199 extern struct thread_node *__timer_thread_alloc (const pthread_attr_t *desired_attr, clockid_t);
200 extern void __timer_thread_dealloc (struct thread_node *thread);
201 extern int __timer_thread_queue_timer (struct thread_node *thread,
202                                        struct timer_node *insert);
203 extern void __timer_thread_wakeup (struct thread_node *thread);