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. --- ChangeLog | 11 +++++++++++ mutex.c | 20 +++++++++----------- spin.c | 47 ++++++++++++++++++++++++++++++----------------- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7c5ee62..f9762d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2001-10-15 Ross Johnson + + * 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. + 2001-10-12 Ross Johnson * spin.c (pthread_spin_unlock): Was not returning diff --git a/mutex.c b/mutex.c index 49a1a18..be039ac 100644 --- a/mutex.c +++ b/mutex.c @@ -93,14 +93,6 @@ pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) return EINVAL; } - mx = (pthread_mutex_t) calloc(1, sizeof(*mx)); - - if (mx == NULL) - { - result = ENOMEM; - goto FAIL0; - } - if (attr != NULL && *attr != NULL && (*attr)->pshared == PTHREAD_PROCESS_SHARED @@ -118,16 +110,22 @@ pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) #error ERROR [__FILE__, line __LINE__]: Process shared mutexes are not supported yet. - #else - result = ENOSYS; - goto FAIL0; + return ENOSYS; #endif /* _POSIX_THREAD_PROCESS_SHARED */ } + mx = (pthread_mutex_t) calloc(1, sizeof(*mx)); + + if (mx == NULL) + { + result = ENOMEM; + goto FAIL0; + } + mx->lock_idx = PTW32_MUTEX_LOCK_IDX_INIT; mx->recursive_count = 0; mx->kind = (attr == NULL || *attr == NULL 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