timer_create.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. static void cleanup_fromsig(void *p)
  19. {
  20. pthread_t self = __pthread_self();
  21. __pthread_tsd_run_dtors(self);
  22. self->cancel = 0;
  23. self->cancelbuf = 0;
  24. self->canceldisable = 0;
  25. self->cancelasync = 0;
  26. self->unblock_cancel = 0;
  27. longjmp(p, 1);
  28. }
  29. static void timer_handler(int sig, siginfo_t *si, void *ctx)
  30. {
  31. pthread_t self = __pthread_self();
  32. jmp_buf jb;
  33. void (*notify)(union sigval) = (void (*)(union sigval))self->start;
  34. union sigval val = { .sival_ptr = self->start_arg };
  35. if (!setjmp(jb) && si->si_code == SI_TIMER) {
  36. pthread_cleanup_push(cleanup_fromsig, jb);
  37. notify(val);
  38. pthread_cleanup_pop(1);
  39. }
  40. }
  41. static void install_handler()
  42. {
  43. struct sigaction sa = {
  44. .sa_sigaction = timer_handler,
  45. .sa_flags = SA_SIGINFO | SA_RESTART
  46. };
  47. __libc_sigaction(SIGTIMER, &sa, 0);
  48. __syscall(SYS_rt_sigprocmask, SIG_UNBLOCK,
  49. SIGTIMER_SET, 0, _NSIG/8);
  50. }
  51. static void *start(void *arg)
  52. {
  53. pthread_t self = __pthread_self();
  54. struct start_args *args = arg;
  55. sigset_t set;
  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. self->result = (void *)-1;
  61. sigfillset(&set);
  62. pthread_sigmask(SIG_BLOCK, &set, 0);
  63. pthread_barrier_wait(&args->b);
  64. __wait(&self->delete_timer, 0, 0, 1);
  65. __syscall(SYS_timer_delete, self->result);
  66. return 0;
  67. }
  68. int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res)
  69. {
  70. static pthread_once_t once = PTHREAD_ONCE_INIT;
  71. pthread_t td;
  72. pthread_attr_t attr;
  73. int r;
  74. struct start_args args;
  75. struct ksigevent ksev, *ksevp=0;
  76. int timerid;
  77. switch (evp ? evp->sigev_notify : SIGEV_SIGNAL) {
  78. case SIGEV_NONE:
  79. case SIGEV_SIGNAL:
  80. if (evp) {
  81. ksev.sigev_value = evp->sigev_value;
  82. ksev.sigev_signo = evp->sigev_signo;
  83. ksev.sigev_notify = evp->sigev_notify;
  84. ksev.sigev_tid = 0;
  85. ksevp = &ksev;
  86. }
  87. if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0)
  88. return -1;
  89. *res = (void *)(intptr_t)timerid;
  90. break;
  91. case SIGEV_THREAD:
  92. pthread_once(&once, install_handler);
  93. if (evp->sigev_notify_attributes)
  94. attr = *evp->sigev_notify_attributes;
  95. else
  96. pthread_attr_init(&attr);
  97. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  98. pthread_barrier_init(&args.b, 0, 2);
  99. args.sev = evp;
  100. r = pthread_create(&td, &attr, start, &args);
  101. if (r) {
  102. errno = r;
  103. return -1;
  104. }
  105. ksev.sigev_value.sival_ptr = 0;
  106. ksev.sigev_signo = SIGTIMER;
  107. ksev.sigev_notify = 4; /* SIGEV_THREAD_ID */
  108. ksev.sigev_tid = td->tid;
  109. r = syscall(SYS_timer_create, clk, &ksev, &timerid);
  110. pthread_barrier_wait(&args.b);
  111. if (r < 0) {
  112. pthread_cancel(td);
  113. return -1;
  114. }
  115. td->result = (void *)(intptr_t)timerid;
  116. *res = td;
  117. break;
  118. default:
  119. errno = EINVAL;
  120. return -1;
  121. }
  122. return 0;
  123. }