4 #include "pthread_impl.h"
8 union sigval sigev_value
;
22 weak_alias(dummy_0
, __pthread_tsd_run_dtors
);
24 static void cleanup_fromsig(void *p
)
26 pthread_t self
= __pthread_self();
27 __pthread_tsd_run_dtors();
30 self
->canceldisable
= 0;
31 self
->cancelasync
= 0;
36 static void *start(void *arg
)
38 pthread_t self
= __pthread_self();
39 struct start_args
*args
= arg
;
42 void (*notify
)(union sigval
) = args
->sev
->sigev_notify_function
;
43 union sigval val
= args
->sev
->sigev_value
;
45 pthread_barrier_wait(&args
->b
);
50 while (sigwaitinfo(SIGTIMER_SET
, &si
) < 0);
51 if (si
.si_code
== SI_TIMER
&& !setjmp(jb
)) {
52 pthread_cleanup_push(cleanup_fromsig
, jb
);
54 pthread_cleanup_pop(1);
56 if (self
->timer_id
< 0) break;
58 __syscall(SYS_timer_delete
, self
->timer_id
& INT_MAX
);
62 int timer_create(clockid_t clk
, struct sigevent
*restrict evp
, timer_t
*restrict res
)
64 static volatile int init
= 0;
68 struct start_args args
;
69 struct ksigevent ksev
, *ksevp
=0;
73 switch (evp
? evp
->sigev_notify
: SIGEV_SIGNAL
) {
78 ksev
.sigev_value
= evp
->sigev_value
;
79 ksev
.sigev_signo
= evp
->sigev_signo
;
80 ksev
.sigev_notify
= evp
->sigev_notify
;
81 if (evp
->sigev_notify
== SIGEV_THREAD_ID
)
82 ksev
.sigev_tid
= evp
->sigev_notify_thread_id
;
87 if (syscall(SYS_timer_create
, clk
, ksevp
, &timerid
) < 0)
89 *res
= (void *)(intptr_t)timerid
;
93 struct sigaction sa
= { .sa_handler
= SIG_DFL
};
94 __libc_sigaction(SIGTIMER
, &sa
, 0);
97 if (evp
->sigev_notify_attributes
)
98 attr
= *evp
->sigev_notify_attributes
;
100 pthread_attr_init(&attr
);
101 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
102 pthread_barrier_init(&args
.b
, 0, 2);
105 __block_app_sigs(&set
);
106 __syscall(SYS_rt_sigprocmask
, SIG_BLOCK
, SIGTIMER_SET
, 0, _NSIG
/8);
107 r
= pthread_create(&td
, &attr
, start
, &args
);
108 __restore_sigs(&set
);
114 ksev
.sigev_value
.sival_ptr
= 0;
115 ksev
.sigev_signo
= SIGTIMER
;
116 ksev
.sigev_notify
= SIGEV_THREAD_ID
;
117 ksev
.sigev_tid
= td
->tid
;
118 if (syscall(SYS_timer_create
, clk
, &ksev
, &timerid
) < 0) {
122 td
->timer_id
= timerid
;
123 pthread_barrier_wait(&args
.b
);
124 if (timerid
< 0) return -1;
125 *res
= (void *)(INTPTR_MIN
| (uintptr_t)td
>>1);