summaryrefslogtreecommitdiff
path: root/mutex.c
diff options
context:
space:
mode:
authorrpj <rpj>2000-08-18 14:17:28 +0000
committerrpj <rpj>2000-08-18 14:17:28 +0000
commit170d8032b2fd55450e4533357f4de118b619a8e8 (patch)
treebb13635e1804f7570546baca70cfdbf47fc28846 /mutex.c
parentcb1dbf17abe4884c5d8223b7606aec6b11e69a73 (diff)
2000-08-18 Ross Johnson <rpj@special.ise.canberra.edu.au>
* mutex.c (pthread_mutex_destroy): Check that the mutex isn't held; invalidate the mutex as early as possible to avoid contention; not perfect - FIXME! * rwlock.c (pthread_rwlock_init): Remove redundant assignment to "rw". (pthread_rwlock_destroy): Invalidate the rwlock before freeing up any of it's resources - to avoid contention. * private.c (ptw32_tkAssocCreate): Change assoc->lock to use a dynamically initialised mutex - only consumes a W32 mutex or critical section when first used, not before. * mutex.c (pthread_mutex_init): Remove redundant assignment to "mx". (pthread_mutexattr_destroy): Set attribute to NULL before freeing it's memory - to avoid contention.
Diffstat (limited to 'mutex.c')
-rw-r--r--mutex.c63
1 files changed, 42 insertions, 21 deletions
diff --git a/mutex.c b/mutex.c
index 3e0e484..42c0844 100644
--- a/mutex.c
+++ b/mutex.c
@@ -96,8 +96,6 @@ pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
return EINVAL;
}
- mx = *mutex;
-
mx = (pthread_mutex_t) calloc(1, sizeof(*mx));
if (mx == NULL)
@@ -213,20 +211,40 @@ pthread_mutex_destroy(pthread_mutex_t *mutex)
{
mx = *mutex;
- if (mx->mutex == 0)
- {
- DeleteCriticalSection(&mx->cs);
- }
- else
- {
- result = (CloseHandle (mx->mutex) ? 0 : EINVAL);
- }
-
- if (result == 0)
+ if ((result = pthread_mutex_trylock(&mx)) == 0)
{
- mx->mutex = 0;
- free(mx);
+ /*
+ * FIXME!!!
+ * The mutex isn't held by another thread but we could still
+ * be too late invalidating the mutex below. Yet we can't do it
+ * earlier in case another thread needs to unlock the mutex
+ * that it's holding.
+ */
*mutex = NULL;
+
+ pthread_mutex_unlock(&mx);
+
+ if (mx->mutex == 0)
+ {
+ DeleteCriticalSection(&mx->cs);
+ }
+ else
+ {
+ result = (CloseHandle (mx->mutex) ? 0 : EINVAL);
+ }
+
+ if (result == 0)
+ {
+ mx->mutex = 0;
+ free(mx);
+ }
+ else
+ {
+ /*
+ * Restore the mutex before we return the error.
+ */
+ *mutex = mx;
+ }
}
}
else
@@ -291,16 +309,17 @@ pthread_mutexattr_init (pthread_mutexattr_t * attr)
* ------------------------------------------------------
*/
{
- pthread_mutexattr_t attr_result;
+ pthread_mutexattr_t ma;
int result = 0;
- attr_result = (pthread_mutexattr_t) calloc (1, sizeof (*attr_result));
+ ma = (pthread_mutexattr_t) calloc (1, sizeof (*ma));
- result = (attr_result == NULL)
- ? ENOMEM
- : 0;
+ if (ma == NULL)
+ {
+ result = ENOMEM;
+ }
- *attr = attr_result;
+ *attr = ma;
return (result);
@@ -343,9 +362,11 @@ pthread_mutexattr_destroy (pthread_mutexattr_t * attr)
}
else
{
- free (*attr);
+ pthread_mutexattr_t ma = *attr;
*attr = NULL;
+ free (ma);
+
result = 0;
}