timer_routines.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <errno.h>
  17. #include <setjmp.h>
  18. #include <signal.h>
  19. #include <stdbool.h>
  20. #include <sysdep.h>
  21. #include <bits/kernel-features.h>
  22. #include <pthreadP.h>
  23. #include "kernel-posix-timers.h"
  24. /* List of active SIGEV_THREAD timers. */
  25. struct timer *__active_timer_sigev_thread;
  26. /* Lock for the __active_timer_sigev_thread. */
  27. pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
  28. struct thread_start_data
  29. {
  30. void (*thrfunc) (sigval_t);
  31. sigval_t sival;
  32. };
  33. #ifdef __NR_timer_create
  34. /* Helper thread to call the user-provided function. */
  35. static void *
  36. timer_sigev_thread (void *arg)
  37. {
  38. /* The parent thread has all signals blocked. This is a bit
  39. surprising for user code, although valid. We unblock all
  40. signals. */
  41. sigset_t ss;
  42. sigemptyset (&ss);
  43. INTERNAL_SYSCALL_DECL (err);
  44. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
  45. struct thread_start_data *td = (struct thread_start_data *) arg;
  46. void (*thrfunc) (sigval_t) = td->thrfunc;
  47. sigval_t sival = td->sival;
  48. /* The TD object was allocated in timer_helper_thread. */
  49. free (td);
  50. /* Call the user-provided function. */
  51. thrfunc (sival);
  52. return NULL;
  53. }
  54. /* Helper function to support starting threads for SIGEV_THREAD. */
  55. static attribute_noreturn void *
  56. timer_helper_thread (void *arg)
  57. {
  58. /* Wait for the SIGTIMER signal, allowing the setXid signal, and
  59. none else. */
  60. sigset_t ss;
  61. sigemptyset (&ss);
  62. __sigaddset (&ss, SIGTIMER);
  63. /* Endless loop of waiting for signals. The loop is only ended when
  64. the thread is canceled. */
  65. while (1)
  66. {
  67. siginfo_t si;
  68. /* sigwaitinfo cannot be used here, since it deletes
  69. SIGCANCEL == SIGTIMER from the set. */
  70. int oldtype = LIBC_CANCEL_ASYNC ();
  71. /* XXX The size argument hopefully will have to be changed to the
  72. real size of the user-level sigset_t. */
  73. int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
  74. _NSIG / 8);
  75. LIBC_CANCEL_RESET (oldtype);
  76. if (result > 0)
  77. {
  78. if (si.si_code == SI_TIMER)
  79. {
  80. struct timer *tk = (struct timer *) si.si_ptr;
  81. /* Check the timer is still used and will not go away
  82. while we are reading the values here. */
  83. pthread_mutex_lock (&__active_timer_sigev_thread_lock);
  84. struct timer *runp = __active_timer_sigev_thread;
  85. while (runp != NULL)
  86. if (runp == tk)
  87. break;
  88. else
  89. runp = runp->next;
  90. if (runp != NULL)
  91. {
  92. struct thread_start_data *td = malloc (sizeof (*td));
  93. /* There is not much we can do if the allocation fails. */
  94. if (td != NULL)
  95. {
  96. /* This is the signal we are waiting for. */
  97. td->thrfunc = tk->thrfunc;
  98. td->sival = tk->sival;
  99. pthread_t th;
  100. (void) pthread_create (&th, &tk->attr,
  101. timer_sigev_thread, td);
  102. }
  103. }
  104. pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
  105. }
  106. else if (si.si_code == SI_TKILL)
  107. /* The thread is canceled. */
  108. pthread_exit (NULL);
  109. }
  110. }
  111. }
  112. /* Control variable for helper thread creation. */
  113. pthread_once_t __helper_once attribute_hidden;
  114. /* TID of the helper thread. */
  115. pid_t __helper_tid attribute_hidden;
  116. /* Reset variables so that after a fork a new helper thread gets started. */
  117. static void
  118. reset_helper_control (void)
  119. {
  120. __helper_once = PTHREAD_ONCE_INIT;
  121. __helper_tid = 0;
  122. }
  123. void
  124. attribute_hidden
  125. __start_helper_thread (void)
  126. {
  127. /* The helper thread needs only very little resources
  128. and should go away automatically when canceled. */
  129. pthread_attr_t attr;
  130. (void) pthread_attr_init (&attr);
  131. (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
  132. /* Block all signals in the helper thread but SIGSETXID. To do this
  133. thoroughly we temporarily have to block all signals here. The
  134. helper can lose wakeups if SIGCANCEL is not blocked throughout,
  135. but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
  136. explicitly here. */
  137. sigset_t ss;
  138. sigset_t oss;
  139. sigfillset (&ss);
  140. /*__sigaddset (&ss, SIGCANCEL); - already done by sigfillset */
  141. INTERNAL_SYSCALL_DECL (err);
  142. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
  143. /* Create the helper thread for this timer. */
  144. pthread_t th;
  145. int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
  146. if (res == 0)
  147. /* We managed to start the helper thread. */
  148. __helper_tid = ((struct pthread *) th)->tid;
  149. /* Restore the signal mask. */
  150. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
  151. _NSIG / 8);
  152. /* No need for the attribute anymore. */
  153. (void) pthread_attr_destroy (&attr);
  154. /* We have to make sure that after fork()ing a new helper thread can
  155. be created. */
  156. pthread_atfork (NULL, NULL, reset_helper_control);
  157. }
  158. #endif
  159. #ifndef __ASSUME_POSIX_TIMERS
  160. # include <nptl/sysdeps/pthread/timer_routines.c>
  161. #endif