summaryrefslogtreecommitdiff
path: root/linuxthreads/semaphore.c
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2000-01-05 02:09:12 +0000
committerUlrich Drepper <drepper@redhat.com>2000-01-05 02:09:12 +0000
commit1d2fc9b3c59d0e83e04139ddf633731264b76ea2 (patch)
treec738cf2a40851dc25be2c252ba5dbb7f335b5e14 /linuxthreads/semaphore.c
parentf19f2b34439145daf300bf12789bbc61c8d4db28 (diff)
downloadglibc-1d2fc9b3c59d0e83e04139ddf633731264b76ea2.tar.gz
Redesigned how cancellation unblocks a thread from internal cancellation points (sem_wait, pthread_join, pthread_cond_{wait,timedwait}). Cancellation won't eat a signal in any of these functions (*required* by POSIX and Single Unix Spec!).
2000-01-03 Kaz Kylheku <kaz@ashi.footprints.net> Redesigned how cancellation unblocks a thread from internal cancellation points (sem_wait, pthread_join, pthread_cond_{wait,timedwait}). Cancellation won't eat a signal in any of these functions (*required* by POSIX and Single Unix Spec!). * condvar.c: spontaneous wakeup on pthread_cond_timedwait won't eat a simultaneous condition variable signal (not required by POSIX or Single Unix Spec, but nice). * spinlock.c: __pthread_lock queues back any received restarts that don't belong to it instead of assuming ownership of lock upon any restart; fastlock can no longer be acquired by two threads simultaneously. * restart.h: restarts queue even on kernels that don't have queued real time signals (2.0, early 2.1), thanks to atomic counter, avoiding a rare race condition in pthread_cond_timedwait.
Diffstat (limited to 'linuxthreads/semaphore.c')
-rw-r--r--linuxthreads/semaphore.c54
1 files changed, 45 insertions, 9 deletions
diff --git a/linuxthreads/semaphore.c b/linuxthreads/semaphore.c
index cfad97aced..0297b3a1e3 100644
--- a/linuxthreads/semaphore.c
+++ b/linuxthreads/semaphore.c
@@ -38,9 +38,31 @@ int __new_sem_init(sem_t *sem, int pshared, unsigned int value)
return 0;
}
+/* Function called by pthread_cancel to remove the thread from
+ waiting inside __new_sem_wait. */
+
+static int new_sem_extricate_func(void *obj, pthread_descr th)
+{
+ volatile pthread_descr self = thread_self();
+ sem_t *sem = obj;
+ int did_remove = 0;
+
+ __pthread_lock((struct _pthread_fastlock *) &sem->__sem_lock, self);
+ did_remove = remove_from_queue(&sem->__sem_waiting, th);
+ __pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
+
+ return did_remove;
+}
+
int __new_sem_wait(sem_t * sem)
{
volatile pthread_descr self = thread_self();
+ pthread_extricate_if extr;
+ int already_canceled = 0;
+
+ /* Set up extrication interface */
+ extr.pu_object = sem;
+ extr.pu_extricate_func = new_sem_extricate_func;
__pthread_lock((struct _pthread_fastlock *) &sem->__sem_lock, self);
if (sem->__sem_value > 0) {
@@ -48,17 +70,31 @@ int __new_sem_wait(sem_t * sem)
__pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
return 0;
}
- enqueue(&sem->__sem_waiting, self);
- /* Wait for sem_post or cancellation */
+ /* Register extrication interface */
+ __pthread_set_own_extricate_if(self, &extr);
+ /* Enqueue only if not already cancelled. */
+ if (!(THREAD_GETMEM(self, p_canceled)
+ && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
+ enqueue(&sem->__sem_waiting, self);
+ else
+ already_canceled = 1;
__pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
- suspend_with_cancellation(self);
- /* This is a cancellation point */
- if (THREAD_GETMEM(self, p_canceled)
+
+ if (already_canceled) {
+ __pthread_set_own_extricate_if(self, 0);
+ pthread_exit(PTHREAD_CANCELED);
+ }
+
+ /* Wait for sem_post or cancellation, or fall through if already canceled */
+ suspend(self);
+ __pthread_set_own_extricate_if(self, 0);
+
+ /* Terminate only if the wakeup came from cancellation. */
+ /* Otherwise ignore cancellation because we got the semaphore. */
+
+ if (THREAD_GETMEM(self, p_woken_by_cancel)
&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
- /* Remove ourselves from the waiting list if we're still on it */
- __pthread_lock((struct _pthread_fastlock *) &sem->__sem_lock, self);
- remove_from_queue(&sem->__sem_waiting, self);
- __pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
+ THREAD_SETMEM(self, p_woken_by_cancel, 0);
pthread_exit(PTHREAD_CANCELED);
}
/* We got the semaphore */