diff options
| author | Garrett D'Amore <garrett@damore.org> | 2017-06-22 13:09:59 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2017-06-22 13:09:59 -0700 |
| commit | 82973b2d8063656679f424469631041232762deb (patch) | |
| tree | d8b9c1e2b34c1381cba857e0e4cc2a2fca2ddbc6 /src | |
| parent | fd141b3eeb7bb8046568a121095f835a0f49965d (diff) | |
| download | nng-82973b2d8063656679f424469631041232762deb.tar.gz nng-82973b2d8063656679f424469631041232762deb.tar.bz2 nng-82973b2d8063656679f424469631041232762deb.zip | |
Additional mutex debugging support.
Diffstat (limited to 'src')
| -rw-r--r-- | src/platform/posix/posix_impl.h | 5 | ||||
| -rw-r--r-- | src/platform/posix/posix_thread.c | 14 |
2 files changed, 14 insertions, 5 deletions
diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index dea09fa1..1030b6de 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -47,6 +47,7 @@ extern int nni_plat_devnull; // open descriptor on /dev/null struct nni_plat_mtx { int init; + pthread_t owner; pthread_mutex_t mtx; }; @@ -57,8 +58,8 @@ struct nni_plat_thr { }; struct nni_plat_cv { - pthread_cond_t cv; - pthread_mutex_t * mtx; + pthread_cond_t cv; + nni_plat_mtx * mtx; }; #endif diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c index 0d57e7ef..d3b6c1e1 100644 --- a/src/platform/posix/posix_thread.c +++ b/src/platform/posix/posix_thread.c @@ -79,6 +79,7 @@ nni_plat_mtx_lock(nni_plat_mtx *mtx) if ((rv = pthread_mutex_lock(&mtx->mtx)) != 0) { nni_panic("pthread_mutex_lock: %s", strerror(rv)); } + mtx->owner = pthread_self(); } @@ -87,9 +88,11 @@ nni_plat_mtx_unlock(nni_plat_mtx *mtx) { int rv; + NNI_ASSERT(mtx->owner == pthread_self()); if ((rv = pthread_mutex_unlock(&mtx->mtx)) != 0) { nni_panic("pthread_mutex_unlock: %s", strerror(rv)); } + mtx->owner = 0; } @@ -108,7 +111,7 @@ nni_plat_cv_init(nni_plat_cv *cv, nni_plat_mtx *mtx) nni_panic("pthread_cond_init: %s", strerror(rv)); } } - cv->mtx = &mtx->mtx; + cv->mtx = mtx; return (0); } @@ -129,9 +132,11 @@ nni_plat_cv_wait(nni_plat_cv *cv) { int rv; - if ((rv = pthread_cond_wait(&cv->cv, cv->mtx)) != 0) { + NNI_ASSERT(cv->mtx->owner == pthread_self()); + if ((rv = pthread_cond_wait(&cv->cv, &cv->mtx->mtx)) != 0) { nni_panic("pthread_cond_wait: %s", strerror(rv)); } + cv->mtx->owner = pthread_self(); } @@ -141,11 +146,14 @@ nni_plat_cv_until(nni_plat_cv *cv, nni_time until) struct timespec ts; int rv; + NNI_ASSERT(cv->mtx->owner == pthread_self()); + // Our caller has already guaranteed a sane value for until. ts.tv_sec = until / 1000000; ts.tv_nsec = (until % 1000000) * 1000; - rv = pthread_cond_timedwait(&cv->cv, cv->mtx, &ts); + rv = pthread_cond_timedwait(&cv->cv, &cv->mtx->mtx, &ts); + cv->mtx->owner = pthread_self(); if (rv == ETIMEDOUT) { return (NNG_ETIMEDOUT); } else if (rv != 0) { |
