From 5d56aa9cb2bdbe0a18bafbdbb6eb8cf6a46beb79 Mon Sep 17 00:00:00 2001 From: deischen Date: Fri, 18 Apr 2003 05:04:16 +0000 Subject: Revamp libpthread so that it has a chance of working in an SMP environment. This includes support for multiple KSEs and KSEGs. The ability to create more than 1 KSE via pthread_setconcurrency() is in the works as well as support for PTHREAD_SCOPE_SYSTEM threads. Those should come shortly. There are still some known issues which davidxu and I are working on, but it'll make it easier for us by committing what we have. This library now passes all of the ACE tests that libc_r passes with the exception of one. It also seems to work OK with KDE including konqueror, kwrite, etc. I haven't been able to get mozilla to run due to lack of java plugin, so I'd be interested to see how it works with that. Reviewed by: davidxu --- lib/libpthread/thread/thr_sem.c | 72 ++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 34 deletions(-) (limited to 'lib/libpthread/thread/thr_sem.c') diff --git a/lib/libpthread/thread/thr_sem.c b/lib/libpthread/thread/thr_sem.c index 70a9721..d6021a8 100644 --- a/lib/libpthread/thread/thr_sem.c +++ b/lib/libpthread/thread/thr_sem.c @@ -32,7 +32,9 @@ #include #include #include +#include "namespace.h" #include +#include "un-namespace.h" #include "thr_private.h" #define _SEM_CHECK_VALIDITY(sem) \ @@ -88,15 +90,15 @@ _sem_init(sem_t *sem, int pshared, unsigned int value) /* * Initialize the semaphore. */ - if (pthread_mutex_init(&(*sem)->lock, NULL) != 0) { + if (_pthread_mutex_init(&(*sem)->lock, NULL) != 0) { free(*sem); errno = ENOSPC; retval = -1; goto RETURN; } - if (pthread_cond_init(&(*sem)->gtzero, NULL) != 0) { - pthread_mutex_destroy(&(*sem)->lock); + if (_pthread_cond_init(&(*sem)->gtzero, NULL) != 0) { + _pthread_mutex_destroy(&(*sem)->lock); free(*sem); errno = ENOSPC; retval = -1; @@ -109,7 +111,7 @@ _sem_init(sem_t *sem, int pshared, unsigned int value) retval = 0; RETURN: - return retval; + return (retval); } int @@ -120,71 +122,72 @@ _sem_destroy(sem_t *sem) _SEM_CHECK_VALIDITY(sem); /* Make sure there are no waiters. */ - pthread_mutex_lock(&(*sem)->lock); + _pthread_mutex_lock(&(*sem)->lock); if ((*sem)->nwaiters > 0) { - pthread_mutex_unlock(&(*sem)->lock); + _pthread_mutex_unlock(&(*sem)->lock); errno = EBUSY; retval = -1; goto RETURN; } - pthread_mutex_unlock(&(*sem)->lock); + _pthread_mutex_unlock(&(*sem)->lock); - pthread_mutex_destroy(&(*sem)->lock); - pthread_cond_destroy(&(*sem)->gtzero); + _pthread_mutex_destroy(&(*sem)->lock); + _pthread_cond_destroy(&(*sem)->gtzero); (*sem)->magic = 0; free(*sem); retval = 0; RETURN: - return retval; + return (retval); } sem_t * _sem_open(const char *name, int oflag, ...) { errno = ENOSYS; - return SEM_FAILED; + return (SEM_FAILED); } int _sem_close(sem_t *sem) { errno = ENOSYS; - return -1; + return (-1); } int _sem_unlink(const char *name) { errno = ENOSYS; - return -1; + return (-1); } int _sem_wait(sem_t *sem) { - int retval; + struct pthread *curthread = _get_curthread(); + int retval; - _thread_enter_cancellation_point(); + _thr_enter_cancellation_point(curthread); _SEM_CHECK_VALIDITY(sem); - pthread_mutex_lock(&(*sem)->lock); + _pthread_mutex_lock(&(*sem)->lock); while ((*sem)->count == 0) { (*sem)->nwaiters++; - pthread_cond_wait(&(*sem)->gtzero, &(*sem)->lock); + _pthread_cond_wait(&(*sem)->gtzero, &(*sem)->lock); (*sem)->nwaiters--; } (*sem)->count--; - pthread_mutex_unlock(&(*sem)->lock); + _pthread_mutex_unlock(&(*sem)->lock); retval = 0; RETURN: - _thread_leave_cancellation_point(); - return retval; + _thr_leave_cancellation_point(curthread); + return (retval); } int @@ -194,7 +197,7 @@ _sem_trywait(sem_t *sem) _SEM_CHECK_VALIDITY(sem); - pthread_mutex_lock(&(*sem)->lock); + _pthread_mutex_lock(&(*sem)->lock); if ((*sem)->count > 0) { (*sem)->count--; @@ -204,37 +207,38 @@ _sem_trywait(sem_t *sem) retval = -1; } - pthread_mutex_unlock(&(*sem)->lock); + _pthread_mutex_unlock(&(*sem)->lock); RETURN: - return retval; + return (retval); } int _sem_post(sem_t *sem) { - int retval; + kse_critical_t crit; + int retval; _SEM_CHECK_VALIDITY(sem); /* * sem_post() is required to be safe to call from within signal - * handlers. Thus, we must defer signals. + * handlers. Thus, we must enter a critical region. */ - _thread_kern_sig_defer(); + crit = _kse_critical_enter(); - pthread_mutex_lock(&(*sem)->lock); + _pthread_mutex_lock(&(*sem)->lock); (*sem)->count++; if ((*sem)->nwaiters > 0) - pthread_cond_signal(&(*sem)->gtzero); + _pthread_cond_signal(&(*sem)->gtzero); - pthread_mutex_unlock(&(*sem)->lock); + _pthread_mutex_unlock(&(*sem)->lock); - _thread_kern_sig_undefer(); + _kse_critical_leave(crit); retval = 0; RETURN: - return retval; + return (retval); } int @@ -244,11 +248,11 @@ _sem_getvalue(sem_t *sem, int *sval) _SEM_CHECK_VALIDITY(sem); - pthread_mutex_lock(&(*sem)->lock); + _pthread_mutex_lock(&(*sem)->lock); *sval = (int)(*sem)->count; - pthread_mutex_unlock(&(*sem)->lock); + _pthread_mutex_unlock(&(*sem)->lock); retval = 0; RETURN: - return retval; + return (retval); } -- cgit v1.1