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/>. */
25 /* Forward declaration. */
29 /* Definitions for an internal thread of the POSIX timer implementation. */
32 struct list_head links
;
36 struct list_head timer_queue
;
38 struct timer_node
*current_timer
;
44 /* Internal representation of a timer. */
47 struct list_head links
;
48 struct sigevent event
;
50 struct itimerspec value
;
51 struct timespec expirytime
;
56 TIMER_FREE
, TIMER_INUSE
, TIMER_DELETED
58 struct thread_node
*thread
;
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. */
69 # define TIMER_MAX 256
72 /* Static array with the structures for all the timers. */
73 extern struct timer_node __timer_array
[TIMER_MAX
];
75 /* Global lock to protect operation on the lists. */
76 extern pthread_mutex_t __timer_mutex
;
78 /* Variable to protext initialization. */
79 extern pthread_once_t __timer_init_once_control
;
81 /* Nonzero if initialization of timer implementation failed. */
82 extern int __timer_init_failed
;
84 /* Node for the thread used to deliver signals. */
85 extern struct thread_node __timer_signal_thread_rclk
;
88 /* Return pointer to timer structure corresponding to ID. */
89 #define timer_id2ptr(timerid) ((struct timer_node *) timerid)
90 #define timer_ptr2id(timerid) ((void *) timerid)
92 /* Check whether timer is valid; global mutex must be held. */
94 timer_valid (struct timer_node
*timer
)
96 return timer
&& timer
->inuse
== TIMER_INUSE
;
99 /* Timer refcount functions; need global mutex. */
100 extern void __timer_dealloc (struct timer_node
*timer
);
103 timer_addref (struct timer_node
*timer
)
109 timer_delref (struct timer_node
*timer
)
111 if (--timer
->refcount
== 0)
112 __timer_dealloc (timer
);
115 /* Timespec helper routines. */
117 __attribute ((always_inline
))
118 timespec_compare (const struct timespec
*left
, const struct timespec
*right
)
120 if (left
->tv_sec
< right
->tv_sec
)
122 if (left
->tv_sec
> right
->tv_sec
)
125 if (left
->tv_nsec
< right
->tv_nsec
)
127 if (left
->tv_nsec
> right
->tv_nsec
)
134 timespec_add (struct timespec
*sum
, const struct timespec
*left
,
135 const struct timespec
*right
)
137 sum
->tv_sec
= left
->tv_sec
+ right
->tv_sec
;
138 sum
->tv_nsec
= left
->tv_nsec
+ right
->tv_nsec
;
140 if (sum
->tv_nsec
>= 1000000000)
143 sum
->tv_nsec
-= 1000000000;
148 timespec_sub (struct timespec
*diff
, const struct timespec
*left
,
149 const struct timespec
*right
)
151 diff
->tv_sec
= left
->tv_sec
- right
->tv_sec
;
152 diff
->tv_nsec
= left
->tv_nsec
- right
->tv_nsec
;
154 if (diff
->tv_nsec
< 0)
157 diff
->tv_nsec
+= 1000000000;
162 /* We need one of the list functions in the other modules. */
164 list_unlink_ip (struct list_head
*list
)
166 struct list_head
*lnext
= list
->next
, *lprev
= list
->prev
;
171 /* The suffix ip means idempotent; list_unlink_ip can be called
172 * two or more times on the same node.
180 /* Functions in the helper file. */
181 extern void __timer_mutex_cancel_handler (void *arg
);
182 extern void __timer_init_once (void);
183 extern struct timer_node
*__timer_alloc (void);
184 extern int __timer_thread_start (struct thread_node
*thread
);
185 extern struct thread_node
*__timer_thread_find_matching (const pthread_attr_t
*desired_attr
, clockid_t
);
186 extern struct thread_node
*__timer_thread_alloc (const pthread_attr_t
*desired_attr
, clockid_t
);
187 extern void __timer_thread_dealloc (struct thread_node
*thread
);
188 extern int __timer_thread_queue_timer (struct thread_node
*thread
,
189 struct timer_node
*insert
);
190 extern void __timer_thread_wakeup (struct thread_node
*thread
);