timer_routines.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* Helper code for POSIX timer implementation on LinuxThreads.
  2. Copyright (C) 2000, 2001, 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. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation; either version 2.1 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <pthread.h>
  20. #include <stddef.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sysdep.h>
  24. #include <time.h>
  25. #include <unistd.h>
  26. #include <sys/syscall.h>
  27. #include "posix-timer.h"
  28. /* Number of threads used. */
  29. #define THREAD_MAXNODES 16
  30. /* Array containing the descriptors for the used threads. */
  31. static struct thread_node thread_array[THREAD_MAXNODES];
  32. /* Static array with the structures for all the timers. */
  33. struct timer_node __timer_array[TIMER_MAX];
  34. /* Global lock to protect operation on the lists. */
  35. pthread_mutex_t __timer_mutex = PTHREAD_MUTEX_INITIALIZER;
  36. /* Variable to protext initialization. */
  37. pthread_once_t __timer_init_once_control = PTHREAD_ONCE_INIT;
  38. /* Nonzero if initialization of timer implementation failed. */
  39. int __timer_init_failed;
  40. /* Node for the thread used to deliver signals. */
  41. struct thread_node __timer_signal_thread_rclk;
  42. /* Lists to keep free and used timers and threads. */
  43. struct list_links timer_free_list;
  44. struct list_links thread_free_list;
  45. struct list_links thread_active_list;
  46. #ifdef __NR_rt_sigqueueinfo
  47. extern int __syscall_rt_sigqueueinfo (int, int, siginfo_t *);
  48. #endif
  49. /* List handling functions. */
  50. static __inline__ void
  51. list_init (struct list_links *list)
  52. {
  53. list->next = list->prev = list;
  54. }
  55. static __inline__ void
  56. list_append (struct list_links *list, struct list_links *newp)
  57. {
  58. newp->prev = list->prev;
  59. newp->next = list;
  60. list->prev->next = newp;
  61. list->prev = newp;
  62. }
  63. static __inline__ void
  64. list_insbefore (struct list_links *list, struct list_links *newp)
  65. {
  66. list_append (list, newp);
  67. }
  68. /*
  69. * Like list_unlink_ip, except that calling it on a node that
  70. * is already unlinked is disastrous rather than a noop.
  71. */
  72. static __inline__ void
  73. list_unlink (struct list_links *list)
  74. {
  75. struct list_links *lnext = list->next, *lprev = list->prev;
  76. lnext->prev = lprev;
  77. lprev->next = lnext;
  78. }
  79. static __inline__ struct list_links *
  80. list_first (struct list_links *list)
  81. {
  82. return list->next;
  83. }
  84. static __inline__ struct list_links *
  85. list_null (struct list_links *list)
  86. {
  87. return list;
  88. }
  89. static __inline__ struct list_links *
  90. list_next (struct list_links *list)
  91. {
  92. return list->next;
  93. }
  94. static __inline__ int
  95. list_isempty (struct list_links *list)
  96. {
  97. return list->next == list;
  98. }
  99. /* Functions build on top of the list functions. */
  100. static __inline__ struct thread_node *
  101. thread_links2ptr (struct list_links *list)
  102. {
  103. return (struct thread_node *) ((char *) list
  104. - offsetof (struct thread_node, links));
  105. }
  106. static __inline__ struct timer_node *
  107. timer_links2ptr (struct list_links *list)
  108. {
  109. return (struct timer_node *) ((char *) list
  110. - offsetof (struct timer_node, links));
  111. }
  112. /* Initialize a newly allocated thread structure. */
  113. static void
  114. thread_init (struct thread_node *thread, const pthread_attr_t *attr, clockid_t clock_id)
  115. {
  116. if (attr != NULL)
  117. thread->attr = *attr;
  118. else
  119. {
  120. pthread_attr_init (&thread->attr);
  121. pthread_attr_setdetachstate (&thread->attr, PTHREAD_CREATE_DETACHED);
  122. }
  123. thread->exists = 0;
  124. list_init (&thread->timer_queue);
  125. pthread_cond_init (&thread->cond, 0);
  126. thread->current_timer = 0;
  127. thread->captured = pthread_self ();
  128. thread->clock_id = clock_id;
  129. }
  130. /* Initialize the global lists, and acquire global resources. Error
  131. reporting is done by storing a non-zero value to the global variable
  132. timer_init_failed. */
  133. static void
  134. init_module (void)
  135. {
  136. int i;
  137. list_init (&timer_free_list);
  138. list_init (&thread_free_list);
  139. list_init (&thread_active_list);
  140. for (i = 0; i < TIMER_MAX; ++i)
  141. {
  142. list_append (&timer_free_list, &__timer_array[i].links);
  143. __timer_array[i].inuse = TIMER_FREE;
  144. }
  145. for (i = 0; i < THREAD_MAXNODES; ++i)
  146. list_append (&thread_free_list, &thread_array[i].links);
  147. thread_init (&__timer_signal_thread_rclk, 0, CLOCK_REALTIME);
  148. }
  149. /* This is a handler executed in a child process after a fork()
  150. occurs. It reinitializes the module, resetting all of the data
  151. structures to their initial state. The mutex is initialized in
  152. case it was locked in the parent process. */
  153. static void
  154. reinit_after_fork (void)
  155. {
  156. init_module ();
  157. pthread_mutex_init (&__timer_mutex, 0);
  158. }
  159. /* Called once form pthread_once in timer_init. This initializes the
  160. module and ensures that reinit_after_fork will be executed in any
  161. child process. */
  162. void
  163. __timer_init_once (void)
  164. {
  165. init_module ();
  166. pthread_atfork (0, 0, reinit_after_fork);
  167. }
  168. /* Deinitialize a thread that is about to be deallocated. */
  169. static void
  170. thread_deinit (struct thread_node *thread)
  171. {
  172. assert (list_isempty (&thread->timer_queue));
  173. pthread_cond_destroy (&thread->cond);
  174. }
  175. /* Allocate a thread structure from the global free list. Global
  176. mutex lock must be held by caller. The thread is moved to
  177. the active list. */
  178. struct thread_node *
  179. __timer_thread_alloc (const pthread_attr_t *desired_attr, clockid_t clock_id)
  180. {
  181. struct list_links *node = list_first (&thread_free_list);
  182. if (node != list_null (&thread_free_list))
  183. {
  184. struct thread_node *thread = thread_links2ptr (node);
  185. list_unlink (node);
  186. thread_init (thread, desired_attr, clock_id);
  187. list_append (&thread_active_list, node);
  188. return thread;
  189. }
  190. return 0;
  191. }
  192. /* Return a thread structure to the global free list. Global lock
  193. must be held by caller. */
  194. void
  195. __timer_thread_dealloc (struct thread_node *thread)
  196. {
  197. thread_deinit (thread);
  198. list_unlink (&thread->links);
  199. list_append (&thread_free_list, &thread->links);
  200. }
  201. /* Each of our threads which terminates executes this cleanup
  202. handler. We never terminate threads ourselves; if a thread gets here
  203. it means that the evil application has killed it. If the thread has
  204. timers, these require servicing and so we must hire a replacement
  205. thread right away. We must also unblock another thread that may
  206. have been waiting for this thread to finish servicing a timer (see
  207. timer_delete()). */
  208. static void
  209. thread_cleanup (void *val)
  210. {
  211. if (val != NULL)
  212. {
  213. struct thread_node *thread = val;
  214. /* How did the signal thread get killed? */
  215. assert (thread != &__timer_signal_thread_rclk);
  216. pthread_mutex_lock (&__timer_mutex);
  217. thread->exists = 0;
  218. /* We are no longer processing a timer event. */
  219. thread->current_timer = 0;
  220. if (list_isempty (&thread->timer_queue))
  221. __timer_thread_dealloc (thread);
  222. else
  223. (void) __timer_thread_start (thread);
  224. pthread_mutex_unlock (&__timer_mutex);
  225. /* Unblock potentially blocked timer_delete(). */
  226. pthread_cond_broadcast (&thread->cond);
  227. }
  228. }
  229. /* Handle a timer which is supposed to go off now. */
  230. static void
  231. thread_expire_timer (struct thread_node *self, struct timer_node *timer)
  232. {
  233. self->current_timer = timer; /* Lets timer_delete know timer is running. */
  234. pthread_mutex_unlock (&__timer_mutex);
  235. switch (__builtin_expect (timer->event.sigev_notify, SIGEV_SIGNAL))
  236. {
  237. case SIGEV_NONE:
  238. break;
  239. case SIGEV_SIGNAL:
  240. #ifdef __NR_rt_sigqueueinfo
  241. {
  242. siginfo_t info;
  243. /* First, clear the siginfo_t structure, so that we don't pass our
  244. stack content to other tasks. */
  245. memset (&info, 0, sizeof (siginfo_t));
  246. /* We must pass the information about the data in a siginfo_t
  247. value. */
  248. info.si_signo = timer->event.sigev_signo;
  249. info.si_code = SI_TIMER;
  250. info.si_pid = timer->creator_pid;
  251. info.si_uid = getuid ();
  252. info.si_value = timer->event.sigev_value;
  253. INLINE_SYSCALL (rt_sigqueueinfo, 3, info.si_pid, info.si_signo, &info);
  254. }
  255. #else
  256. if (pthread_kill (self->captured, timer->event.sigev_signo) != 0)
  257. {
  258. if (pthread_kill (self->id, timer->event.sigev_signo) != 0)
  259. abort ();
  260. }
  261. #endif
  262. break;
  263. case SIGEV_THREAD:
  264. timer->event.sigev_notify_function (timer->event.sigev_value);
  265. break;
  266. default:
  267. assert (! "unknown event");
  268. break;
  269. }
  270. pthread_mutex_lock (&__timer_mutex);
  271. self->current_timer = 0;
  272. pthread_cond_broadcast (&self->cond);
  273. }
  274. /* Thread function; executed by each timer thread. The job of this
  275. function is to wait on the thread's timer queue and expire the
  276. timers in chronological order as close to their scheduled time as
  277. possible. */
  278. static void
  279. __attribute__ ((noreturn))
  280. thread_func (void *arg)
  281. {
  282. struct thread_node *self = arg;
  283. /* Register cleanup handler, in case rogue application terminates
  284. this thread. (This cannot happen to __timer_signal_thread, which
  285. doesn't invoke application callbacks). */
  286. pthread_cleanup_push (thread_cleanup, self);
  287. pthread_mutex_lock (&__timer_mutex);
  288. while (1)
  289. {
  290. struct list_links *first;
  291. struct timer_node *timer = NULL;
  292. /* While the timer queue is not empty, inspect the first node. */
  293. first = list_first (&self->timer_queue);
  294. if (first != list_null (&self->timer_queue))
  295. {
  296. struct timespec now;
  297. timer = timer_links2ptr (first);
  298. /* This assumes that the elements of the list of one thread
  299. are all for the same clock. */
  300. clock_gettime (timer->clock, &now);
  301. while (1)
  302. {
  303. /* If the timer is due or overdue, remove it from the queue.
  304. If it's a periodic timer, re-compute its new time and
  305. requeue it. Either way, perform the timer expiry. */
  306. if (timespec_compare (&now, &timer->expirytime) < 0)
  307. break;
  308. list_unlink_ip (first);
  309. if (__builtin_expect (timer->value.it_interval.tv_sec, 0) != 0
  310. || timer->value.it_interval.tv_nsec != 0)
  311. {
  312. timer->overrun_count = 0;
  313. timespec_add (&timer->expirytime, &timer->expirytime,
  314. &timer->value.it_interval);
  315. while (timespec_compare (&timer->expirytime, &now) < 0)
  316. {
  317. timespec_add (&timer->expirytime, &timer->expirytime,
  318. &timer->value.it_interval);
  319. if (timer->overrun_count < DELAYTIMER_MAX)
  320. ++timer->overrun_count;
  321. }
  322. __timer_thread_queue_timer (self, timer);
  323. }
  324. thread_expire_timer (self, timer);
  325. first = list_first (&self->timer_queue);
  326. if (first == list_null (&self->timer_queue))
  327. break;
  328. timer = timer_links2ptr (first);
  329. }
  330. }
  331. /* If the queue is not empty, wait until the expiry time of the
  332. first node. Otherwise wait indefinitely. Insertions at the
  333. head of the queue must wake up the thread by broadcasting
  334. this condition variable. */
  335. if (timer != NULL)
  336. pthread_cond_timedwait (&self->cond, &__timer_mutex,
  337. &timer->expirytime);
  338. else
  339. pthread_cond_wait (&self->cond, &__timer_mutex);
  340. }
  341. /* This macro will never be executed since the while loop loops
  342. forever - but we have to add it for proper nesting. */
  343. pthread_cleanup_pop (1);
  344. }
  345. /* Enqueue a timer in wakeup order in the thread's timer queue.
  346. Returns 1 if the timer was inserted at the head of the queue,
  347. causing the queue's next wakeup time to change. */
  348. int
  349. __timer_thread_queue_timer (struct thread_node *thread,
  350. struct timer_node *insert)
  351. {
  352. struct list_links *iter;
  353. int athead = 1;
  354. for (iter = list_first (&thread->timer_queue);
  355. iter != list_null (&thread->timer_queue);
  356. iter = list_next (iter))
  357. {
  358. struct timer_node *timer = timer_links2ptr (iter);
  359. if (timespec_compare (&insert->expirytime, &timer->expirytime) < 0)
  360. break;
  361. athead = 0;
  362. }
  363. list_insbefore (iter, &insert->links);
  364. return athead;
  365. }
  366. /* Start a thread and associate it with the given thread node. Global
  367. lock must be held by caller. */
  368. int
  369. __timer_thread_start (struct thread_node *thread)
  370. {
  371. int retval = 1;
  372. assert (!thread->exists);
  373. thread->exists = 1;
  374. if (pthread_create (&thread->id, &thread->attr,
  375. (void *(*) (void *)) thread_func, thread) != 0)
  376. {
  377. thread->exists = 0;
  378. retval = -1;
  379. }
  380. return retval;
  381. }
  382. void
  383. __timer_thread_wakeup (struct thread_node *thread)
  384. {
  385. pthread_cond_broadcast (&thread->cond);
  386. }
  387. /* Compare two pthread_attr_t thread attributes for exact equality.
  388. Returns 1 if they are equal, otherwise zero if they are not equal or
  389. contain illegal values. This version is LinuxThreads-specific for
  390. performance reason. One could use the access functions to get the
  391. values of all the fields of the attribute structure. */
  392. static int
  393. thread_attr_compare (const pthread_attr_t *left, const pthread_attr_t *right)
  394. {
  395. return (left->__detachstate == right->__detachstate
  396. && left->__schedpolicy == right->__schedpolicy
  397. && left->__guardsize == right->__guardsize
  398. && (left->__schedparam.sched_priority
  399. == right->__schedparam.sched_priority)
  400. && left->__inheritsched == right->__inheritsched
  401. && left->__scope == right->__scope
  402. && left->__stacksize == right->__stacksize
  403. && left->__stackaddr_set == right->__stackaddr_set
  404. && (left->__stackaddr_set
  405. || left->__stackaddr == right->__stackaddr));
  406. }
  407. /* Search the list of active threads and find one which has matching
  408. attributes. Global mutex lock must be held by caller. */
  409. struct thread_node *
  410. __timer_thread_find_matching (const pthread_attr_t *desired_attr,
  411. clockid_t desired_clock_id)
  412. {
  413. struct list_links *iter = list_first (&thread_active_list);
  414. while (iter != list_null (&thread_active_list))
  415. {
  416. struct thread_node *candidate = thread_links2ptr (iter);
  417. if (thread_attr_compare (desired_attr, &candidate->attr)
  418. && desired_clock_id == candidate->clock_id)
  419. return candidate;
  420. iter = list_next (iter);
  421. }
  422. return NULL;
  423. }
  424. /* Grab a free timer structure from the global free list. The global
  425. lock must be held by the caller. */
  426. struct timer_node *
  427. __timer_alloc (void)
  428. {
  429. struct list_links *node = list_first (&timer_free_list);
  430. if (node != list_null (&timer_free_list))
  431. {
  432. struct timer_node *timer = timer_links2ptr (node);
  433. list_unlink_ip (node);
  434. timer->inuse = TIMER_INUSE;
  435. timer->refcount = 1;
  436. return timer;
  437. }
  438. return NULL;
  439. }
  440. /* Return a timer structure to the global free list. The global lock
  441. must be held by the caller. */
  442. void
  443. __timer_dealloc (struct timer_node *timer)
  444. {
  445. assert (timer->refcount == 0);
  446. timer->thread = NULL; /* Break association between timer and thread. */
  447. timer->inuse = TIMER_FREE;
  448. list_append (&timer_free_list, &timer->links);
  449. }
  450. /* Thread cancellation handler which unlocks a mutex. */
  451. void
  452. __timer_mutex_cancel_handler (void *arg)
  453. {
  454. pthread_mutex_unlock (arg);
  455. }