summaryrefslogtreecommitdiff
path: root/spin.c
diff options
context:
space:
mode:
authorrpj <rpj>2001-10-15 08:47:10 +0000
committerrpj <rpj>2001-10-15 08:47:10 +0000
commitd0abe46d41d3c8db1772337e4160ca200b9dbcff (patch)
tree03bdc6e8c816a999ef66d6a05a52166dba7f7cb8 /spin.c
parent50d2dd36c9a30460b32a4c9a3b4b2c456a255467 (diff)
* 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.
Diffstat (limited to 'spin.c')
-rw-r--r--spin.c47
1 files changed, 30 insertions, 17 deletions
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,