From d0abe46d41d3c8db1772337e4160ca200b9dbcff Mon Sep 17 00:00:00 2001 From: rpj Date: Mon, 15 Oct 2001 08:47:10 +0000 Subject: * spin.c (pthread_spin_lock): PTHREAD_SPINLOCK_INITIALIZER was causing a program fault. (pthread_spin_init): Could have alloced memory without freeing under some error conditions. * mutex.c (pthread_mutex_init): Move memory allocation of mutex struct after checking for PROCESS_SHARED. --- spin.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) (limited to 'spin.c') diff --git a/spin.c b/spin.c index 6688219..b35b9f0 100644 --- a/spin.c +++ b/spin.c @@ -81,6 +81,7 @@ int pthread_spin_init(pthread_spinlock_t *lock, int pshared) { pthread_spinlock_t s; + int cpus = 0; int result = 0; if (lock == NULL) @@ -88,19 +89,12 @@ pthread_spin_init(pthread_spinlock_t *lock, int pshared) return EINVAL; } - s = (pthread_spinlock_t) calloc(1, sizeof(*s)); - - if (s == NULL) - { - return ENOMEM; - } - - if (0 != pthread_getprocessors_np(&(s->u.cpus))) + if (0 != pthread_getprocessors_np(&cpus)) { - s->u.cpus = 1; + cpus = 1; } - if (s->u.cpus > 1) + if (cpus > 1) { if (pshared == PTHREAD_PROCESS_SHARED) { @@ -116,16 +110,24 @@ pthread_spin_init(pthread_spinlock_t *lock, int pshared) #error ERROR [__FILE__, line __LINE__]: Process shared spin locks are not supported yet. - #else - result = ENOSYS; - goto FAIL0; + return ENOSYS; #endif /* _POSIX_THREAD_PROCESS_SHARED */ } + s = (pthread_spinlock_t) calloc(1, sizeof(*s)); + + if (s == NULL) + { + return ENOMEM; + } + + if (cpus > 1) + { + s->u.cpus = cpus; s->interlock = PTW32_SPIN_UNLOCKED; } else @@ -142,10 +144,19 @@ pthread_spin_init(pthread_spinlock_t *lock, int pshared) s->interlock = PTW32_SPIN_USE_MUTEX; } } + (void) pthread_mutexattr_destroy(&ma); + } + + if (0 == result) + { + *lock = s; + } + else + { + (void) free(s); + *lock = NULL; } -FAIL0: - *lock = (0 == result ? s : NULL); return(result); } @@ -218,9 +229,9 @@ pthread_spin_destroy(pthread_spinlock_t *lock) int pthread_spin_lock(pthread_spinlock_t *lock) { - register pthread_spinlock_t s = *lock; + register pthread_spinlock_t s; - if (s == PTHREAD_SPINLOCK_INITIALIZER) + if (*lock == PTHREAD_SPINLOCK_INITIALIZER) { int result; @@ -230,6 +241,8 @@ pthread_spin_lock(pthread_spinlock_t *lock) } } + s = *lock; + while ( (_LONG) PTW32_SPIN_LOCKED == InterlockedCompareExchange((_LPLONG) &(s->interlock), (_LONG) PTW32_SPIN_LOCKED, -- cgit v1.2.3