1 /* Definitions for POSIX timer implementation on top of NPTL.
2 Copyright (C) 2000-2014 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 /* 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. */
75 # define TIMER_MAX 256
78 /* Static array with the structures for all the timers. */
79 extern struct timer_node __timer_array
[TIMER_MAX
];
81 /* Global lock to protect operation on the lists. */
82 extern pthread_mutex_t __timer_mutex
;
84 /* Variable to protext initialization. */
85 extern pthread_once_t __timer_init_once_control
;
87 /* Nonzero if initialization of timer implementation failed. */
88 extern int __timer_init_failed
;
90 /* Node for the thread used to deliver signals. */
91 extern struct thread_node __timer_signal_thread_rclk
;
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)
98 /* Check whether timer is valid; global mutex must be held. */
100 timer_valid (struct timer_node
*timer
)
102 return timer
&& timer
->inuse
== TIMER_INUSE
;
105 /* Timer refcount functions; need global mutex. */
106 extern void __timer_dealloc (struct timer_node
*timer
);
109 timer_addref (struct timer_node
*timer
)
115 timer_delref (struct timer_node
*timer
)
117 if (--timer
->refcount
== 0)
118 __timer_dealloc (timer
);
121 /* Timespec helper routines. */
123 __attribute ((always_inline
))
124 timespec_compare (const struct timespec
*left
, const struct timespec
*right
)
126 if (left
->tv_sec
< right
->tv_sec
)
128 if (left
->tv_sec
> right
->tv_sec
)
131 if (left
->tv_nsec
< right
->tv_nsec
)
133 if (left
->tv_nsec
> right
->tv_nsec
)
140 timespec_add (struct timespec
*sum
, const struct timespec
*left
,
141 const struct timespec
*right
)
143 sum
->tv_sec
= left
->tv_sec
+ right
->tv_sec
;
144 sum
->tv_nsec
= left
->tv_nsec
+ right
->tv_nsec
;
146 if (sum
->tv_nsec
>= 1000000000)
149 sum
->tv_nsec
-= 1000000000;
154 timespec_sub (struct timespec
*diff
, const struct timespec
*left
,
155 const struct timespec
*right
)
157 diff
->tv_sec
= left
->tv_sec
- right
->tv_sec
;
158 diff
->tv_nsec
= left
->tv_nsec
- right
->tv_nsec
;
160 if (diff
->tv_nsec
< 0)
163 diff
->tv_nsec
+= 1000000000;
168 /* We need one of the list functions in the other modules. */
170 list_unlink_ip (struct list_links
*list
)
172 struct list_links
*lnext
= list
->next
, *lprev
= list
->prev
;
177 /* The suffix ip means idempotent; list_unlink_ip can be called
178 * two or more times on the same node.
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
);