diff options
author | rpj <rpj> | 2002-01-07 07:10:46 +0000 |
---|---|---|
committer | rpj <rpj> | 2002-01-07 07:10:46 +0000 |
commit | 0337d817b128c648d97a79f42b303421b5b76386 (patch) | |
tree | 3a0204c23010279d7c0ef7f7a229205ca628295a /mutex.c | |
parent | 6922362c66bbfaa3ac9b7bb6be24368d790d28d6 (diff) |
* mutex.c (pthread_mutex_init): Remove critical
section calls.
(pthread_mutex_destroy): Likewise.
(pthread_mutex_unlock): Likewise.
(pthread_mutex_trylock): Likewise; recursive mutexes
now increment the lock count rather than return EBUSY;
errorcheck mutexes return EDEADLCK rather than
EBUSY. This behaviour is consistent with the Solaris
pthreads implementation.
* implement.h (pthread_mutex_t_): Remove critical
section element - no longer needed.
Diffstat (limited to 'mutex.c')
-rw-r--r-- | mutex.c | 66 |
1 files changed, 32 insertions, 34 deletions
@@ -135,12 +135,10 @@ pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) ? PTHREAD_MUTEX_DEFAULT : (*attr)->kind); mx->ownerThread = NULL; - InitializeCriticalSection( &mx->try_lock_cs ); mx->wait_sema = CreateSemaphore( NULL, 0, 1, NULL ); if( NULL == mx->wait_sema ) { - DeleteCriticalSection( &mx->try_lock_cs ); result = EAGAIN; } @@ -203,7 +201,6 @@ pthread_mutex_destroy(pthread_mutex_t *mutex) if (result == 0) { - DeleteCriticalSection( &mx->try_lock_cs ); CloseHandle( mx->wait_sema ); free(mx); } @@ -702,16 +699,12 @@ pthread_mutex_unlock(pthread_mutex_t *mutex) { mx->ownerThread = NULL; - EnterCriticalSection( &mx->try_lock_cs ); - - if( InterlockedDecrement( &mx->lock_idx ) >= 0 ) - { - /* Someone is waiting on that mutex */ - ReleaseSemaphore( mx->wait_sema, 1, NULL ); - } - - LeaveCriticalSection( &mx->try_lock_cs ); - } + if( InterlockedDecrement( &mx->lock_idx ) >= 0 ) + { + /* Someone is waiting on that mutex */ + ReleaseSemaphore( mx->wait_sema, 1, NULL ); + } + } } else { @@ -752,30 +745,35 @@ pthread_mutex_trylock(pthread_mutex_t *mutex) if (result == 0) { - /* Try to lock only if mutex seems available */ - if( PTW32_MUTEX_LOCK_IDX_INIT == mx->lock_idx ) - { - EnterCriticalSection( &mx->try_lock_cs ); - if( 0 == InterlockedIncrement( &mx->lock_idx ) ) - { - mx->recursive_count = 1; - mx->ownerThread = (mx->kind != PTHREAD_MUTEX_FAST_NP - ? pthread_self() - : (pthread_t) PTW32_MUTEX_OWNER_ANONYMOUS); - } - else - { - (void) InterlockedDecrement( &mx->lock_idx ); - result = EBUSY; - } + if( PTW32_MUTEX_LOCK_IDX_INIT == ptw32_InterlockedCompareExchange( + &mx->lock_idx, 0, PTW32_MUTEX_LOCK_IDX_INIT ) ) + { + mx->recursive_count = 1; + mx->ownerThread = (mx->kind != PTHREAD_MUTEX_FAST_NP + ? pthread_self() + : (pthread_t) PTW32_MUTEX_OWNER_ANONYMOUS); + } - LeaveCriticalSection( &mx->try_lock_cs ); - } else - { - result = EBUSY; - } + { + if( mx->kind != PTHREAD_MUTEX_FAST_NP && + pthread_equal( mx->ownerThread, pthread_self() ) ) + { + if( mx->kind == PTHREAD_MUTEX_RECURSIVE_NP ) + { + mx->recursive_count++; + } + else + { + result = EDEADLK; + } + } + else + { + result = EBUSY; + } + } } return(result); |