timer_create.c 3.1 KB

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