From 0337d817b128c648d97a79f42b303421b5b76386 Mon Sep 17 00:00:00 2001 From: rpj Date: Mon, 7 Jan 2002 07:10:46 +0000 Subject: * 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. --- mutex.c | 66 ++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 32 insertions(+), 34 deletions(-) (limited to 'mutex.c') diff --git a/mutex.c b/mutex.c index e62647e..7a232c5 100644 --- a/mutex.c +++ b/mutex.c @@ -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); -- cgit v1.2.3