{
struct __pthread *wakeup;
sem_t *sem;
+ int cancel_wake;
};
static void
If it was already unblocked, it's not queued any more. */
unblock = wakeup->prevp != NULL;
if (unblock)
- __pthread_dequeue (wakeup);
+ {
+ __pthread_dequeue (wakeup);
+ ctx->cancel_wake = 1;
+ }
__pthread_spin_unlock (&sem->__lock);
if (unblock)
struct cancel_ctx ctx;
ctx.wakeup = self;
ctx.sem = sem;
+ ctx.cancel_wake = 0;
/* Test for a pending cancellation request, switch to deferred mode for
safer resource handling, and prepare the hook to call in case we're
__pthread_mutex_unlock (&self->cancel_lock);
if (cancelled)
- __pthread_exit (PTHREAD_CANCELED);
+ {
+ if (ret == 0 && ctx.cancel_wake == 0)
+ /* We were cancelled while waking up with a token, put it back. */
+ sem_post (sem);
+
+ __pthread_exit (PTHREAD_CANCELED);
+ }
return ret;
}