diff options
author | deischen <deischen@FreeBSD.org> | 2000-11-09 05:08:26 +0000 |
---|---|---|
committer | deischen <deischen@FreeBSD.org> | 2000-11-09 05:08:26 +0000 |
commit | 69c0393f8e4b9bd522c1e9afe2f481e040dcbac3 (patch) | |
tree | a9d526e7ccca466ce303b1c6202f807942d274db /lib/libkse/thread/thr_mutex.c | |
parent | e1d8dafb1c246b5ecfd3bf52b6e9ff947e171314 (diff) | |
download | FreeBSD-src-69c0393f8e4b9bd522c1e9afe2f481e040dcbac3.zip FreeBSD-src-69c0393f8e4b9bd522c1e9afe2f481e040dcbac3.tar.gz |
Don't needlessly poll file descriptors when there are no
file descriptors needing to be polled (Doh!). Reported
by Dan Nelson <dnelson@emsphone.com>.
Don't install and start the scheduling timer until the
first thread is created. This prevents the overhead of
having a periodic scheduling signal in a single threaded
program. Reported by Dan Nelson <dnelson@emsphone.com>.
Allow builtin longjmps out of application installed
signal handlers without the need perform any post-handler
cleanup:
o Change signal handling to save the threads interrupted
context on the stack. The threads current context is
now always stored in the same place (in the pthread).
If and when a signal handler returns, the interrupted
context is copied back to the storage area in the pthread.
o Before calling invoking a signal handler for a thread,
back the thread out of any internal waiting queues
(mutex, CV, join, etc) to which it belongs.
Rework uthread_info.c a bit to make it easier to change
the format of a thread dump.
Use an alternal signal stack for the thread library's
signal handler. This allows us to fiddle with the main
threads stack without fear of it being in use.
Reviewed by: jasone
Diffstat (limited to 'lib/libkse/thread/thr_mutex.c')
-rw-r--r-- | lib/libkse/thread/thr_mutex.c | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/lib/libkse/thread/thr_mutex.c b/lib/libkse/thread/thr_mutex.c index ec6c0f6..f3649db 100644 --- a/lib/libkse/thread/thr_mutex.c +++ b/lib/libkse/thread/thr_mutex.c @@ -406,13 +406,29 @@ pthread_mutex_lock(pthread_mutex_t * mutex) _thread_init(); if (mutex == NULL) - ret = EINVAL; + return (EINVAL); /* * If the mutex is statically initialized, perform the dynamic * initialization: */ - else if (*mutex != NULL || (ret = init_static(mutex)) == 0) { + if ((*mutex == NULL) && + ((ret = init_static(mutex)) != 0)) + return (ret); + + /* Reset the interrupted flag: */ + _thread_run->interrupted = 0; + + /* + * Enter a loop waiting to become the mutex owner. We need a + * loop in case the waiting thread is interrupted by a signal + * to execute a signal handler. It is not (currently) possible + * to remain in the waiting queue while running a handler. + * Instead, the thread is interrupted and backed out of the + * waiting queue prior to executing the signal handler. + */ + while (((*mutex)->m_owner != _thread_run) && (ret == 0) && + (_thread_run->interrupted == 0)) { /* * Defer signals to protect the scheduling queues from * access by the signal handler: @@ -432,9 +448,6 @@ pthread_mutex_lock(pthread_mutex_t * mutex) _MUTEX_INIT_LINK(*mutex); } - /* Reset the interrupted flag: */ - _thread_run->interrupted = 0; - /* Process according to mutex type: */ switch ((*mutex)->m_protocol) { /* Default POSIX mutex: */ @@ -624,12 +637,12 @@ pthread_mutex_lock(pthread_mutex_t * mutex) * necessary: */ _thread_kern_sig_undefer(); - - if (_thread_run->interrupted != 0 && - _thread_run->continuation != NULL) - _thread_run->continuation((void *) _thread_run); } + if (_thread_run->interrupted != 0 && + _thread_run->continuation != NULL) + _thread_run->continuation((void *) _thread_run); + /* Return the completion status: */ return (ret); } @@ -1381,7 +1394,7 @@ _mutex_lock_backout(pthread_t pthread) * access by the signal handler: */ _thread_kern_sig_defer(); - if (pthread->state == PS_MUTEX_WAIT) { + if ((pthread->flags & PTHREAD_FLAGS_IN_MUTEXQ) != 0) { mutex = pthread->data.mutex; /* Lock the mutex structure: */ @@ -1390,7 +1403,7 @@ _mutex_lock_backout(pthread_t pthread) mutex_queue_remove(mutex, pthread); /* This thread is no longer waiting for the mutex: */ - mutex->m_owner->data.mutex = NULL; + pthread->data.mutex = NULL; /* Unlock the mutex structure: */ _SPINUNLOCK(&mutex->lock); |