diff options
Diffstat (limited to 'lib/libc_r/uthread/uthread_mutex.c')
| -rw-r--r-- | lib/libc_r/uthread/uthread_mutex.c | 85 |
1 files changed, 30 insertions, 55 deletions
diff --git a/lib/libc_r/uthread/uthread_mutex.c b/lib/libc_r/uthread/uthread_mutex.c index d3801f1750c0b..4f4aa8af041e6 100644 --- a/lib/libc_r/uthread/uthread_mutex.c +++ b/lib/libc_r/uthread/uthread_mutex.c @@ -53,7 +53,7 @@ pthread_mutex_init(pthread_mutex_t * mutex, /* Check if default mutex attributes: */ if (mutex_attr == NULL || *mutex_attr == NULL) /* Default to a fast mutex: */ - type = PTHREAD_MUTEX_DEFAULT; + type = MUTEX_TYPE_FAST; else if ((*mutex_attr)->m_type >= MUTEX_TYPE_MAX) /* Return an invalid argument error: */ @@ -74,14 +74,12 @@ pthread_mutex_init(pthread_mutex_t * mutex, /* Process according to mutex type: */ switch (type) { /* Fast mutex: */ - case PTHREAD_MUTEX_DEFAULT: - case PTHREAD_MUTEX_NORMAL: - case PTHREAD_MUTEX_ERRORCHECK: + case MUTEX_TYPE_FAST: /* Nothing to do here. */ break; /* Counting mutex: */ - case PTHREAD_MUTEX_RECURSIVE: + case MUTEX_TYPE_COUNTING_FAST: /* Reset the mutex count: */ pmutex->m_data.m_count = 0; break; @@ -176,9 +174,7 @@ pthread_mutex_trylock(pthread_mutex_t * mutex) /* Process according to mutex type: */ switch ((*mutex)->m_type) { /* Fast mutex: */ - case PTHREAD_MUTEX_NORMAL: - case PTHREAD_MUTEX_DEFAULT: - case PTHREAD_MUTEX_ERRORCHECK: + case MUTEX_TYPE_FAST: /* Check if this mutex is not locked: */ if ((*mutex)->m_owner == NULL) { /* Lock the mutex for the running thread: */ @@ -190,7 +186,7 @@ pthread_mutex_trylock(pthread_mutex_t * mutex) break; /* Counting mutex: */ - case PTHREAD_MUTEX_RECURSIVE: + case MUTEX_TYPE_COUNTING_FAST: /* Check if this mutex is locked: */ if ((*mutex)->m_owner != NULL) { /* @@ -243,26 +239,8 @@ pthread_mutex_lock(pthread_mutex_t * mutex) /* Process according to mutex type: */ switch ((*mutex)->m_type) { - /* What SS2 define as a 'normal' mutex. This has to deadlock - on attempts to get a lock you already own. */ - case PTHREAD_MUTEX_NORMAL: - if ((*mutex)->m_owner == _thread_run) { - /* Intetionally deadlock */ - for (;;) - _thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__); - } - goto COMMON_LOCK; - - /* Return error (not OK) on attempting to re-lock */ - case PTHREAD_MUTEX_ERRORCHECK: - if ((*mutex)->m_owner == _thread_run) { - ret = EDEADLK; - break; - } - /* Fast mutexes do not check for any error conditions: */ - case PTHREAD_MUTEX_DEFAULT: - COMMON_LOCK: + case MUTEX_TYPE_FAST: /* * Enter a loop to wait for the mutex to be locked by the * current thread: @@ -279,10 +257,11 @@ pthread_mutex_lock(pthread_mutex_t * mutex) */ _thread_queue_enq(&(*mutex)->m_queue, _thread_run); - /* Wait for the mutex: */ - _thread_kern_sched_state_unlock( - PS_MUTEX_WAIT, &(*mutex)->lock, - __FILE__, __LINE__); + /* Unlock the mutex structure: */ + _SPINUNLOCK(&(*mutex)->lock); + + /* Block signals: */ + _thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__); /* Lock the mutex again: */ _SPINLOCK(&(*mutex)->lock); @@ -291,7 +270,7 @@ pthread_mutex_lock(pthread_mutex_t * mutex) break; /* Counting mutex: */ - case PTHREAD_MUTEX_RECURSIVE: + case MUTEX_TYPE_COUNTING_FAST: /* * Enter a loop to wait for the mutex to be locked by the * current thread: @@ -311,10 +290,11 @@ pthread_mutex_lock(pthread_mutex_t * mutex) */ _thread_queue_enq(&(*mutex)->m_queue, _thread_run); - /* Wait for the mutex: */ - _thread_kern_sched_state_unlock( - PS_MUTEX_WAIT, &(*mutex)->lock, - __FILE__, __LINE__); + /* Unlock the mutex structure: */ + _SPINUNLOCK(&(*mutex)->lock); + + /* Block signals: */ + _thread_kern_sched_state(PS_MUTEX_WAIT, __FILE__, __LINE__); /* Lock the mutex again: */ _SPINLOCK(&(*mutex)->lock); @@ -353,15 +333,12 @@ pthread_mutex_unlock(pthread_mutex_t * mutex) /* Process according to mutex type: */ switch ((*mutex)->m_type) { - /* Default & normal mutexes do not really need to check for - any error conditions: */ - case PTHREAD_MUTEX_NORMAL: - case PTHREAD_MUTEX_DEFAULT: - case PTHREAD_MUTEX_ERRORCHECK: + /* Fast mutexes do not check for any error conditions: */ + case MUTEX_TYPE_FAST: /* Check if the running thread is not the owner of the mutex: */ if ((*mutex)->m_owner != _thread_run) { /* Return an invalid argument error: */ - ret = (*mutex)->m_owner ? EPERM : EINVAL; + ret = EINVAL; } /* * Get the next thread from the queue of threads waiting on @@ -374,26 +351,24 @@ pthread_mutex_unlock(pthread_mutex_t * mutex) break; /* Counting mutex: */ - case PTHREAD_MUTEX_RECURSIVE: + case MUTEX_TYPE_COUNTING_FAST: /* Check if the running thread is not the owner of the mutex: */ if ((*mutex)->m_owner != _thread_run) { /* Return an invalid argument error: */ ret = EINVAL; } /* Check if there are still counts: */ - else if ((*mutex)->m_data.m_count > 1) { + else if ((*mutex)->m_data.m_count) { /* Decrement the count: */ (*mutex)->m_data.m_count--; - } else { - (*mutex)->m_data.m_count = 0; - /* - * Get the next thread from the queue of threads waiting on - * the mutex: - */ - if (((*mutex)->m_owner = _thread_queue_deq(&(*mutex)->m_queue)) != NULL) { - /* Allow the new owner of the mutex to run: */ - PTHREAD_NEW_STATE((*mutex)->m_owner,PS_RUNNING); - } + } + /* + * Get the next thread from the queue of threads waiting on + * the mutex: + */ + else if (((*mutex)->m_owner = _thread_queue_deq(&(*mutex)->m_queue)) != NULL) { + /* Allow the new owner of the mutex to run: */ + PTHREAD_NEW_STATE((*mutex)->m_owner,PS_RUNNING); } break; |
