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>.
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.
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.
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/>. */
23 /* Double linked list. */
26 struct list_links *next;
27 struct list_links *prev;
31 /* Forward declaration. */
35 /* Definitions for an internal thread of the POSIX timer implementation. */
38 struct list_links links;
42 struct list_links timer_queue;
44 struct timer_node *current_timer;
50 /* Internal representation of a timer. */
53 struct list_links links;
54 struct sigevent event;
56 struct itimerspec value;
57 struct timespec expirytime;
62 TIMER_FREE, TIMER_INUSE, TIMER_DELETED
64 struct thread_node *thread;
71 /* Static array with the structures for all the timers. */
72 extern struct timer_node __timer_array[TIMER_MAX];
74 /* Global lock to protect operation on the lists. */
75 extern pthread_mutex_t __timer_mutex;
77 /* Variable to protext initialization. */
78 extern pthread_once_t __timer_init_once_control;
80 /* Nonzero if initialization of timer implementation failed. */
81 extern int __timer_init_failed;
83 /* Nodes for the threads used to deliver signals. */
84 /* A distinct thread is used for each clock type. */
86 extern struct thread_node __timer_signal_thread_rclk;
89 /* Return pointer to timer structure corresponding to ID. */
90 static __inline__ struct timer_node *
91 timer_id2ptr (timer_t timerid)
93 if (timerid >= 0 && timerid < TIMER_MAX)
94 return &__timer_array[timerid];
99 /* Return ID of TIMER. */
100 static __inline__ int
101 timer_ptr2id (struct timer_node *timer)
103 return timer - __timer_array;
106 /* Check whether timer is valid; global mutex must be held. */
107 static __inline__ int
108 timer_valid (struct timer_node *timer)
110 return timer && timer->inuse == TIMER_INUSE;
113 /* Timer refcount functions; need global mutex. */
114 extern void __timer_dealloc (struct timer_node *timer);
116 static __inline__ void
117 timer_addref (struct timer_node *timer)
122 static __inline__ void
123 timer_delref (struct timer_node *timer)
125 if (--timer->refcount == 0)
126 __timer_dealloc (timer);
129 /* Timespec helper routines. */
130 static __inline__ int
131 timespec_compare (const struct timespec *left, const struct timespec *right)
133 if (left->tv_sec < right->tv_sec)
135 if (left->tv_sec > right->tv_sec)
138 if (left->tv_nsec < right->tv_nsec)
140 if (left->tv_nsec > right->tv_nsec)
146 static __inline__ void
147 timespec_add (struct timespec *sum, const struct timespec *left,
148 const struct timespec *right)
150 sum->tv_sec = left->tv_sec + right->tv_sec;
151 sum->tv_nsec = left->tv_nsec + right->tv_nsec;
153 if (sum->tv_nsec >= 1000000000)
156 sum->tv_nsec -= 1000000000;
160 static __inline__ void
161 timespec_sub (struct timespec *diff, const struct timespec *left,
162 const struct timespec *right)
164 diff->tv_sec = left->tv_sec - right->tv_sec;
165 diff->tv_nsec = left->tv_nsec - right->tv_nsec;
167 if (diff->tv_nsec < 0)
170 diff->tv_nsec += 1000000000;
175 /* We need one of the list functions in the other modules. */
176 static __inline__ void
177 list_unlink_ip (struct list_links *list)
179 struct list_links *lnext = list->next, *lprev = list->prev;
184 /* The suffix ip means idempotent; list_unlink_ip can be called
185 * two or more times on the same node.
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);