summaryrefslogtreecommitdiffstats
path: root/lib/libpthread/thread/thr_mutex.c
diff options
context:
space:
mode:
authorjb <jb@FreeBSD.org>1998-04-29 09:59:34 +0000
committerjb <jb@FreeBSD.org>1998-04-29 09:59:34 +0000
commit6c9ee23acc144ec10f869bbd9b872379224a8938 (patch)
treeb0024d273ef0465a33cf00f2b90524d004b9c0b1 /lib/libpthread/thread/thr_mutex.c
parenta44088edc8056e79e7c0b3b27ea2c5c3355368e9 (diff)
downloadFreeBSD-src-6c9ee23acc144ec10f869bbd9b872379224a8938.zip
FreeBSD-src-6c9ee23acc144ec10f869bbd9b872379224a8938.tar.gz
Change signal model to match POSIX (i.e. one set of signal handlers
for the process, not a separate set for each thread). By default, the process now only has signal handlers installed for SIGVTALRM, SIGINFO and SIGCHLD. The thread kernel signal handler is installed for other signals on demand. This means that SIG_IGN and SIG_DFL processing is now left to the kernel, not the thread kernel. Change the signal dispatch to no longer use a signal thread, and call the signal handler using the stack of the thread that has the signal pending. Change the atomic lock method to use test-and-set asm code with a yield if blocked. This introduces separate locks for each type of object instead of blocking signals to prevent a context switch. It was this blocking of signals that caused the performance degradation the people have noted. This is a *big* change!
Diffstat (limited to 'lib/libpthread/thread/thr_mutex.c')
-rw-r--r--lib/libpthread/thread/thr_mutex.c112
1 files changed, 47 insertions, 65 deletions
diff --git a/lib/libpthread/thread/thr_mutex.c b/lib/libpthread/thread/thr_mutex.c
index 9f92de9..fb737cf 100644
--- a/lib/libpthread/thread/thr_mutex.c
+++ b/lib/libpthread/thread/thr_mutex.c
@@ -49,28 +49,26 @@ pthread_mutex_init(pthread_mutex_t * mutex,
ret = EINVAL;
} else {
/* Check if default mutex attributes: */
- if (mutex_attr == NULL || *mutex_attr == NULL) {
+ if (mutex_attr == NULL || *mutex_attr == NULL)
/* Default to a fast mutex: */
type = MUTEX_TYPE_FAST;
- } else if ((*mutex_attr)->m_type >= MUTEX_TYPE_MAX) {
+
+ else if ((*mutex_attr)->m_type >= MUTEX_TYPE_MAX)
/* Return an invalid argument error: */
ret = EINVAL;
- } else {
+ else
/* Use the requested mutex type: */
type = (*mutex_attr)->m_type;
- }
/* Check no errors so far: */
if (ret == 0) {
- if ((pmutex = (pthread_mutex_t) malloc(sizeof(struct pthread_mutex))) == NULL) {
+ if ((pmutex = (pthread_mutex_t)
+ malloc(sizeof(struct pthread_mutex))) == NULL)
ret = ENOMEM;
- } else {
+ else {
/* Reset the mutex flags: */
pmutex->m_flags = 0;
- /* Block signals: */
- _thread_kern_sig_block(&status);
-
/* Process according to mutex type: */
switch (type) {
/* Fast mutex: */
@@ -96,14 +94,12 @@ pthread_mutex_init(pthread_mutex_t * mutex,
pmutex->m_flags |= MUTEX_FLAGS_INITED;
pmutex->m_owner = NULL;
pmutex->m_type = type;
+ pmutex->access_lock = 0;
*mutex = pmutex;
} else {
free(pmutex);
*mutex = NULL;
}
-
- /* Unblock signals: */
- _thread_kern_sig_unblock(status);
}
}
}
@@ -114,42 +110,25 @@ pthread_mutex_init(pthread_mutex_t * mutex,
int
pthread_mutex_destroy(pthread_mutex_t * mutex)
{
- int ret = 0;
- int status;
+ int ret = 0;
- if (mutex == NULL || *mutex == NULL) {
+ if (mutex == NULL || *mutex == NULL)
ret = EINVAL;
- } else {
- /* Block signals: */
- _thread_kern_sig_block(&status);
-
- /* Process according to mutex type: */
- switch ((*mutex)->m_type) {
- /* Fast mutex: */
- case MUTEX_TYPE_FAST:
- /* Nothing to do here. */
- break;
-
- /* Counting mutex: */
- case MUTEX_TYPE_COUNTING_FAST:
- /* Reset the mutex count: */
- (*mutex)->m_data.m_count = 0;
- break;
-
- /* Trap undefined mutex types: */
- default:
- /* Return an invalid argument error: */
- ret = EINVAL;
- break;
- }
-
- /* Clean up the mutex in case that others want to use it: */
- _thread_queue_init(&(*mutex)->m_queue);
- (*mutex)->m_owner = NULL;
- (*mutex)->m_flags = 0;
-
- /* Unblock signals: */
- _thread_kern_sig_unblock(status);
+ else {
+ /* Lock the mutex structure: */
+ _spinlock(&(*mutex)->access_lock);
+
+ /*
+ * Free the memory allocated for the mutex
+ * structure:
+ */
+ free(*mutex);
+
+ /*
+ * Leave the caller's pointer NULL now that
+ * the mutex has been destroyed:
+ */
+ *mutex = NULL;
}
/* Return the completion status: */
@@ -160,7 +139,6 @@ int
pthread_mutex_trylock(pthread_mutex_t * mutex)
{
int ret = 0;
- int status;
if (mutex == NULL)
ret = EINVAL;
@@ -171,8 +149,8 @@ pthread_mutex_trylock(pthread_mutex_t * mutex)
*/
else if (*mutex != NULL ||
(ret = pthread_mutex_init(mutex,NULL)) == 0) {
- /* Block signals: */
- _thread_kern_sig_block(&status);
+ /* Lock the mutex structure: */
+ _spinlock(&(*mutex)->access_lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@@ -216,8 +194,8 @@ pthread_mutex_trylock(pthread_mutex_t * mutex)
break;
}
- /* Unblock signals: */
- _thread_kern_sig_unblock(status);
+ /* Unlock the mutex structure: */
+ _atomic_unlock(&(*mutex)->access_lock);
}
/* Return the completion status: */
@@ -228,7 +206,6 @@ int
pthread_mutex_lock(pthread_mutex_t * mutex)
{
int ret = 0;
- int status;
if (mutex == NULL)
ret = EINVAL;
@@ -239,8 +216,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
*/
else if (*mutex != NULL ||
(ret = pthread_mutex_init(mutex,NULL)) == 0) {
- /* Block signals: */
- _thread_kern_sig_block(&status);
+ /* Lock the mutex structure: */
+ _spinlock(&(*mutex)->access_lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@@ -262,11 +239,14 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
*/
_thread_queue_enq(&(*mutex)->m_queue, _thread_run);
+ /* Unlock the mutex structure: */
+ _atomic_unlock(&(*mutex)->access_lock);
+
/* Block signals: */
_thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__);
- /* Block signals: */
- _thread_kern_sig_block(NULL);
+ /* Lock the mutex again: */
+ _spinlock(&(*mutex)->access_lock);
}
}
break;
@@ -292,11 +272,14 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
*/
_thread_queue_enq(&(*mutex)->m_queue, _thread_run);
+ /* Unlock the mutex structure: */
+ _atomic_unlock(&(*mutex)->access_lock);
+
/* Block signals: */
_thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__);
- /* Block signals: */
- _thread_kern_sig_block(NULL);
+ /* Lock the mutex again: */
+ _spinlock(&(*mutex)->access_lock);
}
}
@@ -311,8 +294,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex)
break;
}
- /* Unblock signals: */
- _thread_kern_sig_unblock(status);
+ /* Unlock the mutex structure: */
+ _atomic_unlock(&(*mutex)->access_lock);
}
/* Return the completion status: */
@@ -323,13 +306,12 @@ int
pthread_mutex_unlock(pthread_mutex_t * mutex)
{
int ret = 0;
- int status;
if (mutex == NULL || *mutex == NULL) {
ret = EINVAL;
} else {
- /* Block signals: */
- _thread_kern_sig_block(&status);
+ /* Lock the mutex structure: */
+ _spinlock(&(*mutex)->access_lock);
/* Process according to mutex type: */
switch ((*mutex)->m_type) {
@@ -379,8 +361,8 @@ pthread_mutex_unlock(pthread_mutex_t * mutex)
break;
}
- /* Unblock signals: */
- _thread_kern_sig_unblock(status);
+ /* Unlock the mutex structure: */
+ _atomic_unlock(&(*mutex)->access_lock);
}
/* Return the completion status: */
OpenPOWER on IntegriCloud