timer_create.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <time.h>
  2. #include <setjmp.h>
  3. #include "pthread_impl.h"
  4. struct ksigevent {
  5. union sigval sigev_value;
  6. int sigev_signo;
  7. int sigev_notify;
  8. int sigev_tid;
  9. };
  10. struct start_args {
  11. pthread_barrier_t b;
  12. struct sigevent *sev;
  13. };
  14. static void dummy_1(pthread_t self)
  15. {
  16. }
  17. weak_alias(dummy_1, __pthread_tsd_run_dtors);
  18. void __reset_tls();
  19. static void cleanup_fromsig(void *p)
  20. {
  21. pthread_t self = __pthread_self();
  22. __pthread_tsd_run_dtors(self);
  23. self->cancel = 0;
  24. self->cancelbuf = 0;
  25. self->canceldisable = 0;
  26. self->cancelasync = 0;
  27. self->unblock_cancel = 0;
  28. __reset_tls();
  29. longjmp(p, 1);
  30. }
  31. static void timer_handler(int sig, siginfo_t *si, void *ctx)
  32. {
  33. pthread_t self = __pthread_self();
  34. jmp_buf jb;
  35. void (*notify)(union sigval) = (void (*)(union sigval))self->start;
  36. union sigval val = { .sival_ptr = self->start_arg };
  37. if (!setjmp(jb) && si->si_code == SI_TIMER) {
  38. pthread_cleanup_push(cleanup_fromsig, jb);
  39. notify(val);
  40. pthread_cleanup_pop(1);
  41. }
  42. }
  43. static void install_handler()
  44. {
  45. struct sigaction sa = {
  46. .sa_sigaction = timer_handler,
  47. .sa_flags = SA_SIGINFO | SA_RESTART
  48. };
  49. __libc_sigaction(SIGTIMER, &sa, 0);
  50. }
  51. static void *start(void *arg)
  52. {
  53. pthread_t self = __pthread_self();
  54. struct start_args *args = arg;
  55. int id;
  56. /* Reuse no-longer-needed thread structure fields to avoid
  57. * needing the timer address in the signal handler. */
  58. self->start = (void *(*)(void *))args->sev->sigev_notify_function;
  59. self->start_arg = args->sev->sigev_value.sival_ptr;
  60. pthread_barrier_wait(&args->b);
  61. if ((id = self->timer_id) >= 0) {
  62. __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
  63. SIGTIMER_SET, 0, _NSIG/8);
  64. __wait(&self->timer_id, 0, id, 1);
  65. __syscall(SYS_timer_delete, id);
  66. }
  67. return 0;
  68. }
  69. int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
  70. {
  71. static pthread_once_t once = PTHREAD_ONCE_INIT;
  72. pthread_t td;
  73. pthread_attr_t attr;
  74. int r;
  75. struct start_args args;
  76. struct ksigevent ksev, *ksevp=0;
  77. int timerid;
  78. sigset_t set;
  79. switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
  80. case SIGEV_NONE:
  81. case SIGEV_SIGNAL:
  82. if (evp) {
  83. ksev.sigev_value = evp->sigev_value;
  84. ksev.sigev_signo = evp->sigev_signo;
  85. ksev.sigev_notify = evp->sigev_notify;
  86. ksev.sigev_tid = 0;
  87. ksevp = &ksev;
  88. }
  89. if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
  90. return -1;
  91. *res = (void *)(intptr_t)timerid;
  92. break;
  93. case SIGEV_THREAD:
  94. pthread_once(&once, install_handler);
  95. if (evp->sigev_notify_attributes)
  96. attr = *evp->sigev_notify_attributes;
  97. else
  98. pthread_attr_init(&attr);
  99. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  100. pthread_barrier_init(&args.b, 0, 2);
  101. args.sev = evp;
  102. __block_app_sigs(&set);
  103. r = pthread_create(&td, &attr, start, &args);
  104. __restore_sigs(&set);
  105. if (r) {
  106. errno = r;
  107. return -1;
  108. }
  109. ksev.sigev_value.sival_ptr = 0;
  110. ksev.sigev_signo = SIGTIMER;
  111. ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
  112. ksev.sigev_tid = td->tid;
  113. if (syscall(SYS_timer_create, clk, &ksev, &timerid) < 0)
  114. timerid = -1;
  115. td->timer_id = timerid;
  116. pthread_barrier_wait(&args.b);
  117. if (timerid < 0) return -1;
  118. *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1);
  119. break;
  120. default:
  121. errno = EINVAL;
  122. return -1;
  123. }
  124. return 0;
  125. }