diff options
Diffstat (limited to 'lib/libc_r/uthread/uthread_mutex.c')
-rw-r--r-- | lib/libc_r/uthread/uthread_mutex.c | 112 |
1 files changed, 47 insertions, 65 deletions
diff --git a/lib/libc_r/uthread/uthread_mutex.c b/lib/libc_r/uthread/uthread_mutex.c index 9f92de9..fb737cf 100644 --- a/lib/libc_r/uthread/uthread_mutex.c +++ b/lib/libc_r/uthread/uthread_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: */ |