aboutsummaryrefslogtreecommitdiff
path: root/linuxthreads
diff options
context:
space:
mode:
Diffstat (limited to 'linuxthreads')
-rw-r--r--linuxthreads/ChangeLog18
-rw-r--r--linuxthreads/cancel.c44
-rw-r--r--linuxthreads/condvar.c324
-rw-r--r--linuxthreads/internals.h40
-rw-r--r--linuxthreads/join.c48
-rw-r--r--linuxthreads/oldsemaphore.c24
-rw-r--r--linuxthreads/pthread.c66
-rw-r--r--linuxthreads/queue.h5
-rw-r--r--linuxthreads/restart.h35
-rw-r--r--linuxthreads/rwlock.c2
-rw-r--r--linuxthreads/semaphore.c54
-rw-r--r--linuxthreads/spinlock.c24
-rw-r--r--linuxthreads/spinlock.h28
13 files changed, 610 insertions, 102 deletions
diff --git a/linuxthreads/ChangeLog b/linuxthreads/ChangeLog
index 37be0e829f..78e8552a6f 100644
--- a/linuxthreads/ChangeLog
+++ b/linuxthreads/ChangeLog
@@ -1,3 +1,21 @@
+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.
+
1999-12-31 Andreas Jaeger <aj@suse.de>
* internals.h: Remove duplicate prototype declarations.
diff --git a/linuxthreads/cancel.c b/linuxthreads/cancel.c
index c45cac97a3..8fd8c1e60f 100644
--- a/linuxthreads/cancel.c
+++ b/linuxthreads/cancel.c
@@ -52,16 +52,54 @@ int pthread_cancel(pthread_t thread)
{
pthread_handle handle = thread_handle(thread);
int pid;
+ int dorestart = 0;
+ pthread_descr th;
+ pthread_extricate_if *pextricate;
__pthread_lock(&handle->h_lock, NULL);
if (invalid_handle(handle, thread)) {
__pthread_unlock(&handle->h_lock);
return ESRCH;
}
- handle->h_descr->p_canceled = 1;
- pid = handle->h_descr->p_pid;
+
+ th = handle->h_descr;
+
+ if (th->p_canceled) {
+ __pthread_unlock(&handle->h_lock);
+ return 0;
+ }
+
+ pextricate = th->p_extricate;
+ th->p_canceled = 1;
+ pid = th->p_pid;
+
+ /* If the thread has registered an extrication interface, then
+ invoke the interface. If it returns 1, then we succeeded in
+ dequeuing the thread from whatever waiting object it was enqueued
+ with. In that case, it is our responsibility to wake it up.
+ And also to set the p_woken_by_cancel flag so the woken thread
+ can tell that it was woken by cancellation. */
+
+ if (pextricate != NULL) {
+ dorestart = pextricate->pu_extricate_func(pextricate->pu_object, th);
+ th->p_woken_by_cancel = dorestart;
+ }
+
__pthread_unlock(&handle->h_lock);
- kill(pid, __pthread_sig_cancel);
+
+ /* If the thread has suspended or is about to, then we unblock it by
+ issuing a restart, instead of a cancel signal. Otherwise we send
+ the cancel signal to unblock the thread from a cancellation point,
+ or to initiate asynchronous cancellation. The restart is needed so
+ we have proper accounting of restarts; suspend decrements the thread's
+ resume count, and restart() increments it. This also means that suspend's
+ handling of the cancel signal is obsolete. */
+
+ if (dorestart)
+ restart(th);
+ else
+ kill(pid, __pthread_sig_cancel);
+
return 0;
}
diff --git a/linuxthreads/condvar.c b/linuxthreads/condvar.c
index 2ea7513c68..87a93a9115 100644
--- a/linuxthreads/condvar.c
+++ b/linuxthreads/condvar.c
@@ -25,6 +25,22 @@
#include "queue.h"
#include "restart.h"
+static int pthread_cond_timedwait_relative_old(pthread_cond_t *,
+ pthread_mutex_t *, const struct timespec *);
+
+static int pthread_cond_timedwait_relative_new(pthread_cond_t *,
+ pthread_mutex_t *, const struct timespec *);
+
+static int (*pthread_cond_tw_rel)(pthread_cond_t *, pthread_mutex_t *,
+ const struct timespec *) = pthread_cond_timedwait_relative_old;
+
+/* initialize this module */
+void __pthread_init_condvar(int rt_sig_available)
+{
+ if (rt_sig_available)
+ pthread_cond_tw_rel = pthread_cond_timedwait_relative_new;
+}
+
int pthread_cond_init(pthread_cond_t *cond,
const pthread_condattr_t *cond_attr)
{
@@ -39,54 +55,125 @@ int pthread_cond_destroy(pthread_cond_t *cond)
return 0;
}
+/* Function called by pthread_cancel to remove the thread from
+ waiting on a condition variable queue. */
+
+static int cond_extricate_func(void *obj, pthread_descr th)
+{
+ volatile pthread_descr self = thread_self();
+ pthread_cond_t *cond = obj;
+ int did_remove = 0;
+
+ __pthread_lock(&cond->__c_lock, self);
+ did_remove = remove_from_queue(&cond->__c_waiting, th);
+ __pthread_unlock(&cond->__c_lock);
+
+ return did_remove;
+}
+
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
volatile pthread_descr self = thread_self();
+ pthread_extricate_if extr;
+ int already_canceled = 0;
+
+ /* Set up extrication interface */
+ extr.pu_object = cond;
+ extr.pu_extricate_func = cond_extricate_func;
+
+ /* Register extrication interface */
+ __pthread_set_own_extricate_if(self, &extr);
+
+ /* Atomically enqueue thread for waiting, but only if it is not
+ canceled. If the thread is canceled, then it will fall through the
+ suspend call below, and then call pthread_exit without
+ having to worry about whether it is still on the condition variable queue.
+ This depends on pthread_cancel setting p_canceled before calling the
+ extricate function. */
__pthread_lock(&cond->__c_lock, self);
- enqueue(&cond->__c_waiting, self);
+ if (!(THREAD_GETMEM(self, p_canceled)
+ && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
+ enqueue(&cond->__c_waiting, self);
+ else
+ already_canceled = 1;
__pthread_unlock(&cond->__c_lock);
+
+ if (already_canceled) {
+ __pthread_set_own_extricate_if(self, 0);
+ pthread_exit(PTHREAD_CANCELED);
+ }
+
pthread_mutex_unlock(mutex);
- suspend_with_cancellation(self);
- pthread_mutex_lock(mutex);
- /* This is a cancellation point */
- if (THREAD_GETMEM(self, p_canceled)
+
+ suspend(self);
+ __pthread_set_own_extricate_if(self, 0);
+
+ /* Check for cancellation again, to provide correct cancellation
+ point behavior */
+
+ if (THREAD_GETMEM(self, p_woken_by_cancel)
&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
- /* Remove ourselves from the waiting queue if we're still on it */
- __pthread_lock(&cond->__c_lock, self);
- remove_from_queue(&cond->__c_waiting, self);
- __pthread_unlock(&cond->__c_lock);
+ THREAD_SETMEM(self, p_woken_by_cancel, 0);
+ pthread_mutex_lock(mutex);
pthread_exit(PTHREAD_CANCELED);
}
+
+ pthread_mutex_lock(mutex);
return 0;
}
+/* The following function is used on kernels that don't have rt signals.
+ SIGUSR1 is used as the restart signal. The different code is needed
+ because that ordinary signal does not queue. */
+
static int
-pthread_cond_timedwait_relative(pthread_cond_t *cond,
+pthread_cond_timedwait_relative_old(pthread_cond_t *cond,
pthread_mutex_t *mutex,
const struct timespec * reltime)
{
volatile pthread_descr self = thread_self();
sigset_t unblock, initial_mask;
- int retsleep;
+ int retsleep, already_canceled, was_signalled;
sigjmp_buf jmpbuf;
+ pthread_extricate_if extr;
+
+requeue_and_wait_again:
- /* Wait on the condition */
+ retsleep = 0;
+ already_canceled = 0;
+ was_signalled = 0;
+
+ /* Set up extrication interface */
+ extr.pu_object = cond;
+ extr.pu_extricate_func = cond_extricate_func;
+
+ /* Register extrication interface */
+ __pthread_set_own_extricate_if(self, &extr);
+
+ /* Enqueue to wait on the condition and check for cancellation. */
__pthread_lock(&cond->__c_lock, self);
- enqueue(&cond->__c_waiting, self);
+ if (!(THREAD_GETMEM(self, p_canceled)
+ && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
+ enqueue(&cond->__c_waiting, self);
+ else
+ already_canceled = 1;
__pthread_unlock(&cond->__c_lock);
+
+ if (already_canceled) {
+ __pthread_set_own_extricate_if(self, 0);
+ pthread_exit(PTHREAD_CANCELED);
+ }
+
pthread_mutex_unlock(mutex);
- continue_waiting:
- /* Set up a longjmp handler for the restart and cancel signals */
- if (sigsetjmp(jmpbuf, 1) == 0) {
- THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
- THREAD_SETMEM(self, p_cancel_jmp, &jmpbuf);
- THREAD_SETMEM(self, p_signal, 0);
- /* Check for cancellation */
- if (THREAD_GETMEM(self, p_canceled)
- && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
- retsleep = -1;
- } else {
+
+ if (atomic_decrement(&self->p_resume_count) == 0) {
+ /* Set up a longjmp handler for the restart signal, unblock
+ the signal and sleep. */
+
+ if (sigsetjmp(jmpbuf, 1) == 0) {
+ THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
+ THREAD_SETMEM(self, p_signal, 0);
/* Unblock the restart signal */
sigemptyset(&unblock);
sigaddset(&unblock, __pthread_sig_restart);
@@ -95,37 +182,180 @@ pthread_cond_timedwait_relative(pthread_cond_t *cond,
retsleep = __libc_nanosleep(reltime, NULL);
/* Block the restart signal again */
sigprocmask(SIG_SETMASK, &initial_mask, NULL);
+ was_signalled = 0;
+ } else {
+ retsleep = -1;
+ was_signalled = 1;
}
- } else {
- retsleep = -1;
+ THREAD_SETMEM(self, p_signal_jmp, NULL);
}
- THREAD_SETMEM(self, p_signal_jmp, NULL);
- THREAD_SETMEM(self, p_cancel_jmp, NULL);
- /* Here, either the condition was signaled (self->p_signal != 0)
- or we got canceled (self->p_canceled != 0)
- or the timeout occurred (retsleep == 0)
- or another interrupt occurred (retsleep == -1) */
- /* This is a cancellation point */
- if (THREAD_GETMEM(self, p_canceled)
+
+ /* Now was_signalled is true if we exited the above code
+ due to the delivery of a restart signal. In that case,
+ we know we have been dequeued and resumed and that the
+ resume count is balanced. Otherwise, there are some
+ cases to consider. First, try to bump up the resume count
+ back to zero. If it goes to 1, it means restart() was
+ invoked on this thread. The signal must be consumed
+ and the count bumped down and everything is cool.
+ Otherwise, no restart was delivered yet, so we remove
+ the thread from the queue. If this succeeds, it's a clear
+ case of timeout. If we fail to remove from the queue, then we
+ must wait for a restart. */
+
+ if (!was_signalled) {
+ if (atomic_increment(&self->p_resume_count) != -1) {
+ __pthread_wait_for_restart_signal(self);
+ atomic_decrement(&self->p_resume_count); /* should be zero now! */
+ } else {
+ int was_on_queue;
+ __pthread_lock(&cond->__c_lock, self);
+ was_on_queue = remove_from_queue(&cond->__c_waiting, self);
+ __pthread_unlock(&cond->__c_lock);
+
+ if (was_on_queue) {
+ __pthread_set_own_extricate_if(self, 0);
+ pthread_mutex_lock(mutex);
+
+ if (retsleep == 0)
+ return ETIMEDOUT;
+ /* Woken by a signal: resume waiting as
+ required by Single Unix Specification. */
+ goto requeue_and_wait_again;
+ }
+
+ suspend(self);
+ }
+ }
+
+ __pthread_set_own_extricate_if(self, 0);
+
+ /* The remaining logic is the same as in other cancellable waits,
+ such as pthread_join sem_wait or pthread_cond wait. */
+
+ if (THREAD_GETMEM(self, p_woken_by_cancel)
&& THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
- __pthread_lock(&cond->__c_lock, self);
- remove_from_queue(&cond->__c_waiting, self);
- __pthread_unlock(&cond->__c_lock);
+ THREAD_SETMEM(self, p_woken_by_cancel, 0);
pthread_mutex_lock(mutex);
pthread_exit(PTHREAD_CANCELED);
}
- /* If not signaled: also remove ourselves and return an error code, but
- only if the timeout has elapsed. If not, just continue waiting. */
- if (THREAD_GETMEM(self, p_signal) == 0) {
- if (retsleep != 0)
- goto continue_waiting;
+
+ pthread_mutex_lock(mutex);
+ return 0;
+}
+
+/* The following function is used on new (late 2.1 and 2.2 and higher) kernels
+ that have rt signals which queue. */
+
+static int
+pthread_cond_timedwait_relative_new(pthread_cond_t *cond,
+ pthread_mutex_t *mutex,
+ const struct timespec * reltime)
+{
+ volatile pthread_descr self = thread_self();
+ sigset_t unblock, initial_mask;
+ int retsleep, already_canceled, was_signalled;
+ sigjmp_buf jmpbuf;
+ pthread_extricate_if extr;
+
+ requeue_and_wait_again:
+
+ retsleep = 0;
+ already_canceled = 0;
+ was_signalled = 0;
+
+ /* Set up extrication interface */
+ extr.pu_object = cond;
+ extr.pu_extricate_func = cond_extricate_func;
+
+ /* Register extrication interface */
+ __pthread_set_own_extricate_if(self, &extr);
+
+ /* Enqueue to wait on the condition and check for cancellation. */
+ __pthread_lock(&cond->__c_lock, self);
+ if (!(THREAD_GETMEM(self, p_canceled)
+ && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
+ enqueue(&cond->__c_waiting, self);
+ else
+ already_canceled = 1;
+ __pthread_unlock(&cond->__c_lock);
+
+ if (already_canceled) {
+ __pthread_set_own_extricate_if(self, 0);
+ pthread_exit(PTHREAD_CANCELED);
+ }
+
+ pthread_mutex_unlock(mutex);
+
+ /* Set up a longjmp handler for the restart signal, unblock
+ the signal and sleep. */
+
+ if (sigsetjmp(jmpbuf, 1) == 0) {
+ THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
+ THREAD_SETMEM(self, p_signal, 0);
+ /* Unblock the restart signal */
+ sigemptyset(&unblock);
+ sigaddset(&unblock, __pthread_sig_restart);
+ sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
+ /* Sleep for the required duration */
+ retsleep = __libc_nanosleep(reltime, NULL);
+ /* Block the restart signal again */
+ sigprocmask(SIG_SETMASK, &initial_mask, NULL);
+ was_signalled = 0;
+ } else {
+ retsleep = -1;
+ was_signalled = 1;
+ }
+ THREAD_SETMEM(self, p_signal_jmp, NULL);
+
+ /* Now was_signalled is true if we exited the above code
+ due to the delivery of a restart signal. In that case,
+ everything is cool. We have been removed from the queue
+ by the other thread, and consumed its signal.
+
+ Otherwise we this thread woke up spontaneously, or due to a signal other
+ than restart. The next thing to do is to try to remove the thread
+ from the queue. This may fail due to a race against another thread
+ trying to do the same. In the failed case, we know we were signalled,
+ and we may also have to consume a restart signal. */
+
+ if (!was_signalled) {
+ int was_on_queue;
+
+ /* __pthread_lock will queue back any spurious restarts that
+ may happen to it. */
+
__pthread_lock(&cond->__c_lock, self);
- remove_from_queue(&cond->__c_waiting, self);
+ was_on_queue = remove_from_queue(&cond->__c_waiting, self);
__pthread_unlock(&cond->__c_lock);
+
+ if (was_on_queue) {
+ __pthread_set_own_extricate_if(self, 0);
+ pthread_mutex_lock(mutex);
+
+ if (retsleep == 0)
+ return ETIMEDOUT;
+ /* Woken by a signal: resume waiting as
+ required by Single Unix Specification. */
+ goto requeue_and_wait_again;
+ }
+
+ /* Eat the outstanding restart() from the signaller */
+ suspend(self);
+ }
+
+ __pthread_set_own_extricate_if(self, 0);
+
+ /* The remaining logic is the same as in other cancellable waits,
+ such as pthread_join sem_wait or pthread_cond wait. */
+
+ if (THREAD_GETMEM(self, p_woken_by_cancel)
+ && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
+ THREAD_SETMEM(self, p_woken_by_cancel, 0);
pthread_mutex_lock(mutex);
- return ETIMEDOUT;
+ pthread_exit(PTHREAD_CANCELED);
}
- /* Otherwise, return normally */
+
pthread_mutex_lock(mutex);
return 0;
}
@@ -144,7 +374,9 @@ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
reltime.tv_sec -= 1;
}
if (reltime.tv_sec < 0) return ETIMEDOUT;
- return pthread_cond_timedwait_relative(cond, mutex, &reltime);
+
+ /* Indirect call through pointer! */
+ return pthread_cond_tw_rel(cond, mutex, &reltime);
}
int pthread_cond_signal(pthread_cond_t *cond)
diff --git a/linuxthreads/internals.h b/linuxthreads/internals.h
index c5fde1f883..8af2fca335 100644
--- a/linuxthreads/internals.h
+++ b/linuxthreads/internals.h
@@ -86,6 +86,27 @@ struct pthread_key_struct {
typedef struct _pthread_descr_struct * pthread_descr;
+/* Callback interface for removing the thread from waiting on an
+ object if it is cancelled while waiting or about to wait.
+ This hold a pointer to the object, and a pointer to a function
+ which ``extricates'' the thread from its enqueued state.
+ The function takes two arguments: pointer to the wait object,
+ and a pointer to the thread. It returns 1 if an extrication
+ actually occured, and hence the thread must also be signalled.
+ It returns 0 if the thread had already been extricated. */
+
+typedef struct _pthread_extricate_struct {
+ void *pu_object;
+ int (*pu_extricate_func)(void *, pthread_descr);
+} pthread_extricate_if;
+
+/* Atomic counter made possible by compare_and_swap */
+
+struct pthread_atomic {
+ long p_count;
+ int p_spinlock;
+};
+
struct _pthread_descr_struct {
pthread_descr p_nextlive, p_prevlive;
/* Double chaining of active threads */
@@ -96,6 +117,7 @@ struct _pthread_descr_struct {
int p_priority; /* Thread priority (== 0 if not realtime) */
struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
int p_signal; /* last signal received */
+ struct pthread_atomic p_resume_count; /* number of times restart() was called on thread */
sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
char p_terminated; /* true if terminated e.g. by pthread_exit */
@@ -108,6 +130,8 @@ struct _pthread_descr_struct {
char p_cancelstate; /* cancellation state */
char p_canceltype; /* cancellation type (deferred/async) */
char p_canceled; /* cancellation request pending */
+ char p_woken_by_cancel; /* cancellation performed wakeup */
+ pthread_extricate_if *p_extricate; /* See above */
int * p_errnop; /* pointer to used errno variable */
int p_errno; /* error returned by last system call */
int * p_h_errnop; /* pointer to used h_errno variable */
@@ -353,6 +377,7 @@ void __pthread_manager_sighandler(int sig);
void __pthread_reset_main_thread(void);
void __fresetlockfiles(void);
void __pthread_manager_adjust_prio(int thread_prio);
+void __pthread_set_own_extricate_if(pthread_descr self, pthread_extricate_if *peif);
extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
size_t __guardsize);
@@ -372,6 +397,21 @@ extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
int *__kind);
extern void __pthread_kill_other_threads_np (void);
+void __pthread_restart_old(pthread_descr th);
+void __pthread_suspend_old(pthread_descr self);
+
+void __pthread_restart_new(pthread_descr th);
+void __pthread_suspend_new(pthread_descr self);
+
+void __pthread_wait_for_restart_signal(pthread_descr self);
+
+void __pthread_init_condvar(int rt_sig_available);
+
+/* Global pointers to old or new suspend functions */
+
+extern void (*__pthread_restart)(pthread_descr);
+extern void (*__pthread_suspend)(pthread_descr);
+
/* Prototypes for the function without cancelation support when the
normal version has it. */
extern int __libc_close (int fd);
diff --git a/linuxthreads/join.c b/linuxthreads/join.c
index 71db541391..5e6b78ab3b 100644
--- a/linuxthreads/join.c
+++ b/linuxthreads/join.c
@@ -79,12 +79,37 @@ void pthread_exit(void * retval)
_exit(0);
}
+/* Function called by pthread_cancel to remove the thread from
+ waiting on a condition variable queue. */
+
+static int join_extricate_func(void *obj, pthread_descr th)
+{
+ volatile pthread_descr self = thread_self();
+ pthread_handle handle = obj;
+ pthread_descr jo;
+ int did_remove = 0;
+
+ __pthread_lock(&handle->h_lock, self);
+ jo = handle->h_descr;
+ did_remove = jo->p_joining != NULL;
+ jo->p_joining = NULL;
+ __pthread_unlock(&handle->h_lock);
+
+ return did_remove;
+}
+
int pthread_join(pthread_t thread_id, void ** thread_return)
{
volatile pthread_descr self = thread_self();
struct pthread_request request;
pthread_handle handle = thread_handle(thread_id);
pthread_descr th;
+ pthread_extricate_if extr;
+ int already_canceled = 0;
+
+ /* Set up extrication interface */
+ extr.pu_object = handle;
+ extr.pu_extricate_func = join_extricate_func;
__pthread_lock(&handle->h_lock, self);
if (invalid_handle(handle, thread_id)) {
@@ -103,13 +128,28 @@ int pthread_join(pthread_t thread_id, void ** thread_return)
}
/* If not terminated yet, suspend ourselves. */
if (! th->p_terminated) {
- th->p_joining = self;
+ /* Register extrication interface */
+ __pthread_set_own_extricate_if(self, &extr);
+ if (!(THREAD_GETMEM(self, p_canceled)
+ && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENA