timer_routines.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Copyright (C) 2003, 2004, 2005 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. #ifdef __NR_timer_create
  25. /* Helper thread to call the user-provided function. */
  26. static void *
  27. timer_sigev_thread (void *arg)
  28. {
  29. /* The parent thread has all signals blocked. This is a bit
  30. surprising for user code, although valid. We unblock all
  31. signals. */
  32. sigset_t ss;
  33. sigemptyset (&ss);
  34. INTERNAL_SYSCALL_DECL (err);
  35. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, NULL, _NSIG / 8);
  36. struct timer *tk = (struct timer *) arg;
  37. /* Call the user-provided function. */
  38. tk->thrfunc (tk->sival);
  39. return NULL;
  40. }
  41. /* Helper function to support starting threads for SIGEV_THREAD. */
  42. static void *
  43. timer_helper_thread (void *arg)
  44. {
  45. /* Wait for the SIGTIMER signal, allowing the setXid signal, and
  46. none else. */
  47. sigset_t ss;
  48. sigemptyset (&ss);
  49. __sigaddset (&ss, SIGTIMER);
  50. #ifdef SIGSETXID
  51. __sigaddset (&ss, SIGSETXID);
  52. #endif
  53. /* Endless loop of waiting for signals. The loop is only ended when
  54. the thread is canceled. */
  55. while (1)
  56. {
  57. siginfo_t si;
  58. /* sigwaitinfo cannot be used here, since it deletes
  59. SIGCANCEL == SIGTIMER from the set. */
  60. int oldtype = LIBC_CANCEL_ASYNC ();
  61. /* XXX The size argument hopefully will have to be changed to the
  62. real size of the user-level sigset_t. */
  63. int result = INLINE_SYSCALL (rt_sigtimedwait, 4, &ss, &si, NULL,
  64. _NSIG / 8);
  65. LIBC_CANCEL_RESET (oldtype);
  66. if (result > 0)
  67. {
  68. if (si.si_code == SI_TIMER)
  69. {
  70. struct timer *tk = (struct timer *) si.si_ptr;
  71. /* That the signal we are waiting for. */
  72. pthread_t th;
  73. (void) pthread_create (&th, &tk->attr, timer_sigev_thread, tk);
  74. }
  75. else if (si.si_code == SI_TKILL)
  76. /* The thread is canceled. */
  77. pthread_exit (NULL);
  78. }
  79. }
  80. }
  81. /* Control variable for helper thread creation. */
  82. pthread_once_t __helper_once attribute_hidden;
  83. /* TID of the helper thread. */
  84. pid_t __helper_tid attribute_hidden;
  85. /* Reset variables so that after a fork a new helper thread gets started. */
  86. static void
  87. reset_helper_control (void)
  88. {
  89. __helper_once = PTHREAD_ONCE_INIT;
  90. __helper_tid = 0;
  91. }
  92. void
  93. attribute_hidden
  94. __start_helper_thread (void)
  95. {
  96. /* The helper thread needs only very little resources
  97. and should go away automatically when canceled. */
  98. pthread_attr_t attr;
  99. (void) pthread_attr_init (&attr);
  100. (void) pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
  101. /* Block all signals in the helper thread but SIGSETXID. To do this
  102. thoroughly we temporarily have to block all signals here. The
  103. helper can lose wakeups if SIGCANCEL is not blocked throughout,
  104. but sigfillset omits it. So, we add it back explicitly here. */
  105. sigset_t ss;
  106. sigset_t oss;
  107. sigfillset (&ss);
  108. __sigaddset (&ss, SIGCANCEL);
  109. #ifdef SIGSETXID
  110. __sigdelset (&ss, SIGSETXID);
  111. #endif
  112. INTERNAL_SYSCALL_DECL (err);
  113. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &ss, &oss, _NSIG / 8);
  114. /* Create the helper thread for this timer. */
  115. pthread_t th;
  116. int res = pthread_create (&th, &attr, timer_helper_thread, NULL);
  117. if (res == 0)
  118. /* We managed to start the helper thread. */
  119. __helper_tid = ((struct pthread *) th)->tid;
  120. /* Restore the signal mask. */
  121. INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, &oss, NULL,
  122. _NSIG / 8);
  123. /* No need for the attribute anymore. */
  124. (void) pthread_attr_destroy (&attr);
  125. /* We have to make sure that after fork()ing a new helper thread can
  126. be created. */
  127. pthread_atfork (NULL, NULL, reset_helper_control);
  128. }
  129. #endif
  130. #ifndef __ASSUME_POSIX_TIMERS
  131. # include <nptl/sysdeps/pthread/timer_routines.c>
  132. #endif