3 #include "pthread_impl.h"
6 union sigval sigev_value
;
20 weak_alias(dummy_0
, __pthread_tsd_run_dtors
);
22 static void cleanup_fromsig(void *p
)
24 pthread_t self
= __pthread_self();
25 __pthread_tsd_run_dtors(self
);
28 self
->canceldisable
= 0;
29 self
->cancelasync
= 0;
30 self
->unblock_cancel
= 0;
35 static void timer_handler(int sig
, siginfo_t
*si
, void *ctx
)
37 pthread_t self
= __pthread_self();
39 void (*notify
)(union sigval
) = (void (*)(union sigval
))self
->start
;
40 union sigval val
= { .sival_ptr
= self
->start_arg
};
42 if (!setjmp(jb
) && si
->si_code
== SI_TIMER
) {
43 pthread_cleanup_push(cleanup_fromsig
, jb
);
45 pthread_cleanup_pop(1);
49 static void install_handler()
51 struct sigaction sa
= {
52 .sa_sigaction
= timer_handler
,
53 .sa_flags
= SA_SIGINFO
| SA_RESTART
55 __libc_sigaction(SIGTIMER
, &sa
, 0);
58 static void *start(void *arg
)
60 pthread_t self
= __pthread_self();
61 struct start_args
*args
= arg
;
64 /* Reuse no-longer-needed thread structure fields to avoid
65 * needing the timer address in the signal handler. */
66 self
->start
= (void *(*)(void *))args
->sev
->sigev_notify_function
;
67 self
->start_arg
= args
->sev
->sigev_value
.sival_ptr
;
69 pthread_barrier_wait(&args
->b
);
70 if ((id
= self
->timer_id
) >= 0) {
71 __syscall(SYS_rt_sigprocmask
, SIG_UNBLOCK
,
72 SIGTIMER_SET
, 0, _NSIG
/8);
73 __wait(&self
->timer_id
, 0, id
, 1);
74 __syscall(SYS_timer_delete
, id
);
79 int timer_create(clockid_t clk
, struct sigevent
*restrict evp
, timer_t
*restrict res
)
81 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
85 struct start_args args
;
86 struct ksigevent ksev
, *ksevp
=0;
90 switch (evp
? evp
->sigev_notify
: SIGEV_SIGNAL
) {
94 ksev
.sigev_value
= evp
->sigev_value
;
95 ksev
.sigev_signo
= evp
->sigev_signo
;
96 ksev
.sigev_notify
= evp
->sigev_notify
;
100 if (syscall(SYS_timer_create
, clk
, ksevp
, &timerid
) < 0)
102 *res
= (void *)(intptr_t)timerid
;
105 pthread_once(&once
, install_handler
);
106 if (evp
->sigev_notify_attributes
)
107 attr
= *evp
->sigev_notify_attributes
;
109 pthread_attr_init(&attr
);
110 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
111 pthread_barrier_init(&args
.b
, 0, 2);
114 __block_app_sigs(&set
);
115 r
= pthread_create(&td
, &attr
, start
, &args
);
116 __restore_sigs(&set
);
122 ksev
.sigev_value
.sival_ptr
= 0;
123 ksev
.sigev_signo
= SIGTIMER
;
124 ksev
.sigev_notify
= 4; /* SIGEV_THREAD_ID */
125 ksev
.sigev_tid
= td
->tid
;
126 if (syscall(SYS_timer_create
, clk
, &ksev
, &timerid
) < 0)
128 td
->timer_id
= timerid
;
129 pthread_barrier_wait(&args
.b
);
130 if (timerid
< 0) return -1;
131 *res
= (void *)(INTPTR_MIN
| (uintptr_t)td
>>1);