timer_create.c 2.9 KB

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