summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@lucera.com>2016-05-13 16:00:45 +0000
committerGarrett D'Amore <garrett@lucera.com>2016-05-13 19:18:30 +0000
commit7744543a812773a18c37a308721ddfbe11aa9ec2 (patch)
treee0bbbfc93fbc2a19a5701afcaede16eb212b2bd7 /src/utils
parentb05e4bbaeaa91bed122c46183ca1a8999f259122 (diff)
downloadnanomsg-7744543a812773a18c37a308721ddfbe11aa9ec2.tar.gz
fixes #709 Desire debugging mutexes by default.
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mutex.c25
-rw-r--r--src/utils/mutex.h2
2 files changed, 27 insertions, 0 deletions
diff --git a/src/utils/mutex.c b/src/utils/mutex.c
index 293b71c..0db890c 100644
--- a/src/utils/mutex.c
+++ b/src/utils/mutex.c
@@ -23,25 +23,44 @@
#include "mutex.h"
#include "err.h"
+#include <stdlib.h>
+
#ifdef NN_HAVE_WINDOWS
void nn_mutex_init (nn_mutex_t *self)
{
InitializeCriticalSection (&self->mutex);
+ self->owner = 0;
+ self->debug = (getenv("NN_NO_MUTEX_DEBUG") != NULL) ? 0 : 1;
}
void nn_mutex_term (nn_mutex_t *self)
{
+ if (self->debug) {
+ /* Make sure we don't free a locked mutex. */
+ nn_assert(self->owner == 0);
+ }
DeleteCriticalSection (&self->mutex);
}
void nn_mutex_lock (nn_mutex_t *self)
{
EnterCriticalSection (&self->mutex);
+
+ if (self->debug) {
+ /* Make sure we don't recursively enter mutexes. */
+ nn_assert(self->owner == 0);
+ self->owner = GetCurrentThreadId();
+ }
}
void nn_mutex_unlock (nn_mutex_t *self)
{
+ if (self->debug) {
+ /* Make sure that we own the mutex we are releasing. */
+ nn_assert(self->owner == GetCurrentThreadId());
+ self->owner = 0;
+ }
LeaveCriticalSection (&self->mutex);
}
@@ -50,9 +69,15 @@ void nn_mutex_unlock (nn_mutex_t *self)
void nn_mutex_init (nn_mutex_t *self)
{
int rc;
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ if (getenv("NN_NO_MUTEX_DEBUG") == NULL) {
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+ }
rc = pthread_mutex_init (&self->mutex, NULL);
errnum_assert (rc == 0, rc);
+ pthread_mutexattr_destroy(&attr);
}
void nn_mutex_term (nn_mutex_t *self)
diff --git a/src/utils/mutex.h b/src/utils/mutex.h
index 509fcf5..0144050 100644
--- a/src/utils/mutex.h
+++ b/src/utils/mutex.h
@@ -34,6 +34,8 @@ struct nn_mutex {
implementation. */
#ifdef NN_HAVE_WINDOWS
CRITICAL_SECTION mutex;
+ DWORD owner;
+ int debug;
#else
pthread_mutex_t mutex;
#endif