timer_routines.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #if defined(__NR_rt_sigtimedwait_time64) && defined(__UCLIBC_USE_TIME64__)
  73. int result = INLINE_SYSCALL (rt_sigtimedwait_time64, 4, &ss, &si, NULL,
  74. _NSIG / 8);
  75. #else
  76. int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
  77. _NSIG / 8);
  78. #endif
  79. LIBC_CANCEL_RESET (oldtype);
  80. if (result > 0)
  81. {
  82. if (si.si_code == SI_TIMER)
  83. {
  84. struct timer *tk = (struct timer *) si.si_ptr;
  85. /* Check the timer is still used and will not go away
  86. while we are reading the values here. */
  87. pthread_mutex_lock (&__active_timer_sigev_thread_lock);
  88. struct timer *runp = __active_timer_sigev_thread;
  89. while (runp != NULL)
  90. if (runp == tk)
  91. break;
  92. else
  93. runp = runp->next;
  94. if (runp != NULL)
  95. {
  96. struct thread_start_data *td = malloc (sizeof (*td));
  97. /* There is not much we can do if the allocation fails. */
  98. if (td != NULL)
  99. {
  100. /* This is the signal we are waiting for. */
  101. td->thrfunc = tk->thrfunc;
  102. td->sival = tk->sival;
  103. pthread_t th;
  104. (void) pthread_create (&th, &tk->attr,
  105. timer_sigev_thread, td);
  106. }
  107. }
  108. pthread_mutex_unlock (&__active_timer_sigev_thread_lock);
  109. }
  110. else if (si.si_code == SI_TKILL)
  111. /* The thread is canceled. */
  112. pthread_exit (NULL);
  113. }
  114. }
  115. }
  116. /* Control variable for helper thread creation. */
  117. pthread_once_t __helper_once attribute_hidden;
  118. /* TID of the helper thread. */
  119. pid_t __helper_tid attribute_hidden;
  120. /* Reset variables so that after a fork a new helper thread gets started. */
  121. static void
  122. reset_helper_control (void)
  123. {
  124. __helper_once = PTHREAD_ONCE_INIT;
  125. __helper_tid = 0;
  126. }
  127. void
  128. attribute_hidden
  129. __start_helper_thread (void)
  130. {
  131. /* The helper thread needs only very little resources
  132. and should go away automatically when canceled. */
  133. pthread_attr_t attr;
  134. (void) pthread_attr_init (&attr);
  135. (void) pthread_attr_setstacksize (&attr, __pthread_get_minstack (&attr));
  136. /* Block all signals in the helper thread but SIGSETXID. To do this
  137. thoroughly we temporarily have to block all signals here. The
  138. helper can lose wakeups if SIGCANCEL is not blocked throughout,
  139. but sigfillset omits it SIGSETXID. So, we add SIGCANCEL back
  140. explicitly here. */
  141. sigset_t ss;
  142. sigset_t oss;
  143. sigfillset (&ss);
  144. __sigaddset (&ss, SIGCANCEL);
  145. INTERNAL_SYSCALL_DECL (err);
  146. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
  147. /* Create the helper thread for this timer. */
  148. pthread_t th;
  149. int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
  150. if (res == 0)
  151. /* We managed to start the helper thread. */
  152. __helper_tid = ((struct pthread *) th)->tid;
  153. /* Restore the signal mask. */
  154. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
  155. _NSIG / 8);
  156. /* No need for the attribute anymore. */
  157. (void) pthread_attr_destroy (&attr);
  158. /* We have to make sure that after fork()ing a new helper thread can
  159. be created. */
  160. pthread_atfork (NULL, NULL, reset_helper_control);
  161. }
  162. #endif
  163. #ifndef __ASSUME_POSIX_TIMERS
  164. # include <nptl/sysdeps/pthread/timer_routines.c>
  165. #endif