mirror of
https://github.com/fluencelabs/musl
synced 2025-07-04 00:51:59 +00:00
greatly improve SIGEV_THREAD timers
calling pthread_exit from, or pthread_cancel on, the timer callback thread will no longer destroy the timer.
This commit is contained in:
@ -42,6 +42,7 @@ struct pthread {
|
|||||||
void *pending;
|
void *pending;
|
||||||
} robust_list;
|
} robust_list;
|
||||||
int unblock_cancel;
|
int unblock_cancel;
|
||||||
|
int delete_timer;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct __timer {
|
struct __timer {
|
||||||
|
@ -13,21 +13,24 @@ struct start_args {
|
|||||||
struct sigevent *sev;
|
struct sigevent *sev;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void cleanup_fromsig(void *p)
|
||||||
|
{
|
||||||
|
pthread_t self = __pthread_self();
|
||||||
|
self->cancel = 0;
|
||||||
|
self->cancelbuf = 0;
|
||||||
|
longjmp(p, 1);
|
||||||
|
}
|
||||||
|
|
||||||
void __sigtimer_handler(pthread_t self)
|
void __sigtimer_handler(pthread_t self)
|
||||||
{
|
{
|
||||||
int st;
|
jmp_buf jb;
|
||||||
void (*notify)(union sigval) = (void (*)(union sigval))self->start;
|
void (*notify)(union sigval) = (void (*)(union sigval))self->start;
|
||||||
union sigval val = { .sival_ptr = self->start_arg };
|
union sigval val = { .sival_ptr = self->start_arg };
|
||||||
|
|
||||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &st);
|
if (setjmp(jb)) return;
|
||||||
|
pthread_cleanup_push(cleanup_fromsig, jb);
|
||||||
notify(val);
|
notify(val);
|
||||||
pthread_setcancelstate(st, 0);
|
pthread_cleanup_pop(0);
|
||||||
}
|
|
||||||
|
|
||||||
static void cleanup(void *p)
|
|
||||||
{
|
|
||||||
pthread_t self = p;
|
|
||||||
__syscall(SYS_timer_delete, self->result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *start(void *arg)
|
static void *start(void *arg)
|
||||||
@ -41,12 +44,9 @@ static void *start(void *arg)
|
|||||||
self->start_arg = args->sev->sigev_value.sival_ptr;
|
self->start_arg = args->sev->sigev_value.sival_ptr;
|
||||||
self->result = (void *)-1;
|
self->result = (void *)-1;
|
||||||
|
|
||||||
pthread_cleanup_push(cleanup, self);
|
|
||||||
pthread_barrier_wait(&args->b);
|
pthread_barrier_wait(&args->b);
|
||||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
__wait(&self->delete_timer, 0, 0, 1);
|
||||||
/* Loop on async-signal-safe cancellation point */
|
__syscall(SYS_timer_delete, self->result);
|
||||||
for (;;) sleep(1000000000);
|
|
||||||
pthread_cleanup_pop(0);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
|
|
||||||
int timer_delete(timer_t t)
|
int timer_delete(timer_t t)
|
||||||
{
|
{
|
||||||
if ((uintptr_t)t >= 0x100000) return pthread_cancel(t);
|
if ((uintptr_t)t >= 0x100000) {
|
||||||
|
pthread_t td = t;
|
||||||
|
td->delete_timer = 1;
|
||||||
|
__wake(&td->delete_timer, 1, 1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
return __syscall(SYS_timer_delete, (long)t);
|
return __syscall(SYS_timer_delete, (long)t);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user