3 #include "pthread_impl.h"
6 union sigval sigev_value
;
17 static void dummy_1(pthread_t self
)
20 weak_alias(dummy_1
, __pthread_tsd_run_dtors
);
24 static void cleanup_fromsig(void *p
)
26 pthread_t self
= __pthread_self();
27 __pthread_tsd_run_dtors(self
);
30 self
->canceldisable
= 0;
31 self
->cancelasync
= 0;
32 self
->unblock_cancel
= 0;
37 static void timer_handler(int sig
, siginfo_t
*si
, void *ctx
)
39 pthread_t self
= __pthread_self();
41 void (*notify
)(union sigval
) = (void (*)(union sigval
))self
->start
;
42 union sigval val
= { .sival_ptr
= self
->start_arg
};
44 if (!setjmp(jb
) && si
->si_code
== SI_TIMER
) {
45 pthread_cleanup_push(cleanup_fromsig
, jb
);
47 pthread_cleanup_pop(1);
51 static void install_handler()
53 struct sigaction sa
= {
54 .sa_sigaction
= timer_handler
,
55 .sa_flags
= SA_SIGINFO
| SA_RESTART
57 __libc_sigaction(SIGTIMER
, &sa
, 0);
60 static void *start(void *arg
)
62 pthread_t self
= __pthread_self();
63 struct start_args
*args
= arg
;
66 /* Reuse no-longer-needed thread structure fields to avoid
67 * needing the timer address in the signal handler. */
68 self
->start
= (void *(*)(void *))args
->sev
->sigev_notify_function
;
69 self
->start_arg
= args
->sev
->sigev_value
.sival_ptr
;
71 pthread_barrier_wait(&args
->b
);
72 if ((id
= self
->timer_id
) >= 0) {
73 __syscall(SYS_rt_sigprocmask
, SIG_UNBLOCK
,
74 SIGTIMER_SET
, 0, _NSIG
/8);
75 __wait(&self
->timer_id
, 0, id
, 1);
76 __syscall(SYS_timer_delete
, id
);
81 int timer_create(clockid_t clk
, struct sigevent
*restrict evp
, timer_t
*restrict res
)
83 static pthread_once_t once
= PTHREAD_ONCE_INIT
;
87 struct start_args args
;
88 struct ksigevent ksev
, *ksevp
=0;
92 switch (evp
? evp
->sigev_notify
: SIGEV_SIGNAL
) {
96 ksev
.sigev_value
= evp
->sigev_value
;
97 ksev
.sigev_signo
= evp
->sigev_signo
;
98 ksev
.sigev_notify
= evp
->sigev_notify
;
102 if (syscall(SYS_timer_create
, clk
, ksevp
, &timerid
) < 0)
104 *res
= (void *)(intptr_t)timerid
;
107 pthread_once(&once
, install_handler
);
108 if (evp
->sigev_notify_attributes
)
109 attr
= *evp
->sigev_notify_attributes
;
111 pthread_attr_init(&attr
);
112 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
113 pthread_barrier_init(&args
.b
, 0, 2);
116 __block_app_sigs(&set
);
117 r
= pthread_create(&td
, &attr
, start
, &args
);
118 __restore_sigs(&set
);
124 ksev
.sigev_value
.sival_ptr
= 0;
125 ksev
.sigev_signo
= SIGTIMER
;
126 ksev
.sigev_notify
= 4; /* SIGEV_THREAD_ID */
127 ksev
.sigev_tid
= td
->tid
;
128 if (syscall(SYS_timer_create
, clk
, &ksev
, &timerid
) < 0)
130 td
->timer_id
= timerid
;
131 pthread_barrier_wait(&args
.b
);
132 if (timerid
< 0) return -1;
133 *res
= (void *)(INTPTR_MIN
| (uintptr_t)td
>>1);