From a250b09c42f9a261da78ecec6226d722e27eea67 Mon Sep 17 00:00:00 2001 From: rpj Date: Fri, 27 Jun 2003 15:52:49 +0000 Subject: Free the spinlock struct in ALL appropriate cases. --- pthread_spin_destroy.c | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'pthread_spin_destroy.c') diff --git a/pthread_spin_destroy.c b/pthread_spin_destroy.c index dbe6713..129e5eb 100644 --- a/pthread_spin_destroy.c +++ b/pthread_spin_destroy.c @@ -42,6 +42,7 @@ int pthread_spin_destroy(pthread_spinlock_t *lock) { register pthread_spinlock_t s; + int result = 0; if (lock == NULL || *lock == NULL) { @@ -52,34 +53,28 @@ pthread_spin_destroy(pthread_spinlock_t *lock) { if (s->interlock == PTW32_SPIN_USE_MUTEX) { - return pthread_mutex_destroy(&(s->u.mutex)); + result = pthread_mutex_destroy(&(s->u.mutex)); } - - if ( (PTW32_INTERLOCKED_LONG) PTW32_SPIN_UNLOCKED == - ptw32_interlocked_compare_exchange((PTW32_INTERLOCKED_LPLONG) &(s->interlock), - (PTW32_INTERLOCKED_LONG) PTW32_OBJECT_INVALID, - (PTW32_INTERLOCKED_LONG) PTW32_SPIN_UNLOCKED)) + else if ( (PTW32_INTERLOCKED_LONG) PTW32_SPIN_UNLOCKED != + ptw32_interlocked_compare_exchange((PTW32_INTERLOCKED_LPLONG) &(s->interlock), + (PTW32_INTERLOCKED_LONG) PTW32_OBJECT_INVALID, + (PTW32_INTERLOCKED_LONG) PTW32_SPIN_UNLOCKED)) { + result = EINVAL; + } + + if (0 == result) + { /* - * The spinlock isn't held by another thread so other threads that have - * just entered another spin_* routine will get PTW32_OBJECT_INVALID - * and so return EINVAL. This will not be so if the memory freed below is - * re-allocated and initialised before that happens. - * - * We are relying on the application to be responsible for ensuring that - * all other threads have finished with the spinlock before destroying it. + * We are relying on the application to ensure that all other threads + * have finished with the spinlock before destroying it. */ *lock = NULL; (void) free(s); - return 0; - } - - return EINVAL; + } } else { - int result = 0; - /* * See notes in ptw32_spinlock_check_need_init() above also. */ @@ -108,6 +103,7 @@ pthread_spin_destroy(pthread_spinlock_t *lock) } LeaveCriticalSection(&ptw32_spinlock_test_init_lock); - return(result); } + + return(result); } -- cgit v1.2.3