timer_routines.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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
  14. not, see <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <setjmp.h>
  17. #include <signal.h>
  18. #include <stdbool.h>
  19. #include <sysdep.h>
  20. #include <bits/kernel-features.h>
  21. #include <pthreadP.h>
  22. #include "kernel-posix-timers.h"
  23. /* List of active SIGEV_THREAD timers. */
  24. struct timer *__active_timer_sigev_thread;
  25. /* Lock for the __active_timer_sigev_thread. */
  26. pthread_mutex_t __active_timer_sigev_thread_lock = PTHREAD_MUTEX_INITIALIZER;
  27. struct thread_start_data
  28. {
  29. void (*thrfunc) (sigval_t);
  30. sigval_t sival;
  31. };
  32. #ifdef __NR_timer_create
  33. /* Helper thread to call the user-provided function. */
  34. static void *
  35. timer_sigev_thread (void *arg)
  36. {
  37. /* The parent thread has all signals blocked. This is a bit
  38. surprising for user code, although valid. We unblock all
  39. signals. */
  40. sigset_t ss;
  41. __sigemptyset (&ss);
  42. INTERNAL_SYSCALL_DECL (err);
  43. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
  44. struct thread_start_data *td = (struct thread_start_data *) arg;
  45. void (*thrfunc) (sigval_t) = td->thrfunc;
  46. sigval_t sival = td->sival;
  47. /* The TD object was allocated in timer_helper_thread. */
  48. free (td);
  49. /* Call the user-provided function. */
  50. thrfunc (sival);
  51. return NULL;
  52. }
  53. /* Helper function to support starting threads for SIGEV_THREAD. */
  54. static attribute_noreturn void *
  55. timer_helper_thread (void *arg)
  56. {
  57. /* Wait for the SIGTIMER signal, allowing the setXid signal, and
  58. none else. */
  59. sigset_t ss;
  60. __sigemptyset (&ss);
  61. __sigaddset (&ss, SIGTIMER);
  62. /* Endless loop of waiting for signals. The loop is only ended when
  63. the thread is canceled. */
  64. while (1)
  65. {
  66. siginfo_t si;
  67. /* sigwaitinfo cannot be used here, since it deletes
  68. SIGCANCEL == SIGTIMER from the set. */
  69. int oldtype = LIBC_CANCEL_ASYNC ();
  70. /* XXX The size argument hopefully will have to be changed to the
  71. real size of the user-level sigset_t. */
  72. int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
  73. _NSIG / 8);
  74. LIBC_CANCEL_RESET (oldtype);
  75. if (result > 0)
  76. {
  77. if (si.si_code == SI_TIMER)
  78. {
  79. struct timer *tk = (struct timer *) si.si_ptr;
  80. /* Check the timer is still used and will not go away
  81. while we are reading the values here. */
  82. pthread_mutex_lock (&__active_timer_sigev_thread_lock);
  83. struct timer *runp = __active_timer_sigev_thread;
  84. while (runp != NULL)
  85. if (runp == tk)
  86. break;
  87. else
  88. runp = runp->next;
  89. if (runp != NULL)
  90. {
  91. struct thread_start_data *td = malloc (sizeof (*td));
  92. /* There is not much we can do if the allocation fails. */
  93. if (td != NULL)
  94. {
  95. /* This is the signal we are waiting for. */
  96. td->thrfunc = tk->thrfunc;
  97. td->sival = tk->sival;
  98. pthread_t th;
  99. (void) pthread_create (&th, &tk->attr,
  100. timer_sigev_thread, td);
  101. }
  102. }
  103. pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
  104. }
  105. else if (si.si_code == SI_TKILL)
  106. /* The thread is canceled. */
  107. pthread_exit (NULL);
  108. }
  109. }
  110. }
  111. /* Control variable for helper thread creation. */
  112. pthread_once_t __helper_once attribute_hidden;
  113. /* TID of the helper thread. */
  114. pid_t __helper_tid attribute_hidden;
  115. /* Reset variables so that after a fork a new helper thread gets started. */
  116. static void
  117. reset_helper_control (void)
  118. {
  119. __helper_once = PTHREAD_ONCE_INIT;
  120. __helper_tid = 0;
  121. }
  122. void
  123. attribute_hidden
  124. __start_helper_thread (void)
  125. {
  126. /* The helper thread needs only very little resources
  127. and should go away automatically when canceled. */
  128. pthread_attr_t attr;
  129. (void) pthread_attr_init (&attr);
  130. (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
  131. /* Block all signals in the helper thread but SIGSETXID. To do this
  132. thoroughly we temporarily have to block all signals here. The
  133. helper can lose wakeups if SIGCANCEL is not blocked throughout,
  134. but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
  135. explicitly here. */
  136. sigset_t ss;
  137. sigset_t oss;
  138. sigfillset (&ss);
  139. __sigaddset (&ss, SIGCANCEL);
  140. INTERNAL_SYSCALL_DECL (err);
  141. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
  142. /* Create the helper thread for this timer. */
  143. pthread_t th;
  144. int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
  145. if (res == 0)
  146. /* We managed to start the helper thread. */
  147. __helper_tid = ((struct pthread *) th)->tid;
  148. /* Restore the signal mask. */
  149. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
  150. _NSIG / 8);
  151. /* No need for the attribute anymore. */
  152. (void) pthread_attr_destroy (&attr);
  153. /* We have to make sure that after fork()ing a new helper thread can
  154. be created. */
  155. pthread_atfork (NULL, NULL, reset_helper_control);
  156. }
  157. #endif
  158. #ifndef __ASSUME_POSIX_TIMERS
  159. # include <nptl/sysdeps/pthread/timer_routines.c>
  160. #endif