diff options
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | mutex.c | 20 | ||||
-rw-r--r-- | spin.c | 47 |
3 files changed, 50 insertions, 28 deletions
@@ -1,3 +1,14 @@ +2001-10-15 Ross Johnson <rpj@setup1.ise.canberra.edu.au> + + * 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 <rpj@setup1.ise.canberra.edu.au> * spin.c (pthread_spin_unlock): Was not returning @@ -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 @@ -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, |