summaryrefslogtreecommitdiffstats
path: root/src/lib/util/threads/sync.cc
diff options
context:
space:
mode:
authorMukund Sivaraman <muks@isc.org>2012-10-18 15:32:36 +0200
committerMukund Sivaraman <muks@isc.org>2012-10-18 15:32:36 +0200
commit2a6f0764001c25acb40eae27d7a2f7691d4c767b (patch)
treeddb78910e49c34671fc5a7b1e1829665ae1dd6ba /src/lib/util/threads/sync.cc
parent[2236] Add a --enable-debug configure flag (diff)
downloadkea-2a6f0764001c25acb40eae27d7a2f7691d4c767b.tar.xz
kea-2a6f0764001c25acb40eae27d7a2f7691d4c767b.zip
[2236] Use ENABLE_DEBUG within the lock implementation
Diffstat (limited to 'src/lib/util/threads/sync.cc')
-rw-r--r--src/lib/util/threads/sync.cc60
1 files changed, 40 insertions, 20 deletions
diff --git a/src/lib/util/threads/sync.cc b/src/lib/util/threads/sync.cc
index c98a7a6b06..9c15f0378c 100644
--- a/src/lib/util/threads/sync.cc
+++ b/src/lib/util/threads/sync.cc
@@ -31,12 +31,16 @@ namespace thread {
class Mutex::Impl {
public:
- Impl() :
- locked_count(0)
+ Impl()
+#ifdef ENABLE_DEBUG
+ : locked_count(0)
+#endif // ENABLE_DEBUG
{}
+
pthread_mutex_t mutex;
- // Only in debug mode
+#ifdef ENABLE_DEBUG
size_t locked_count;
+#endif // ENABLE_DEBUG
};
namespace {
@@ -93,12 +97,17 @@ Mutex::Mutex() :
Mutex::~Mutex() {
if (impl_ != NULL) {
const int result = pthread_mutex_destroy(&impl_->mutex);
+
+#ifdef ENABLE_DEBUG
const bool locked = impl_->locked_count != 0;
+#endif // ENABLE_DEBUG
+
delete impl_;
// We don't want to throw from the destructor. Also, if this ever
// fails, something is really screwed up a lot.
assert(result == 0);
+#ifdef ENABLE_DEBUG
// We should not try to destroy a locked mutex, bad threaded monsters
// could get loose if we ever do and it is also forbidden by pthreads.
@@ -106,9 +115,12 @@ Mutex::~Mutex() {
// pthread_mutex_destroy should check for it already. But it seems
// there are systems that don't check it.
assert(!locked);
+#endif // ENABLE_DEBUG
}
}
+#ifdef ENABLE_DEBUG
+
void
Mutex::postLockAction() {
// This assertion would fail only in non-debugging mode, in which case
@@ -119,16 +131,6 @@ Mutex::postLockAction() {
}
void
-Mutex::lock() {
- assert(impl_ != NULL);
- const int result = pthread_mutex_lock(&impl_->mutex);
- if (result != 0) {
- isc_throw(isc::InvalidOperation, std::strerror(result));
- }
- postLockAction(); // Only in debug mode
-}
-
-void
Mutex::preUnlockAction(bool throw_ok) {
if (impl_->locked_count == 0) {
if (throw_ok) {
@@ -141,20 +143,35 @@ Mutex::preUnlockAction(bool throw_ok) {
--impl_->locked_count;
}
+bool
+Mutex::locked() const {
+ return (impl_->locked_count != 0);
+}
+
+#endif // ENABLE_DEBUG
+
+void
+Mutex::lock() {
+ assert(impl_ != NULL);
+ const int result = pthread_mutex_lock(&impl_->mutex);
+ if (result != 0) {
+ isc_throw(isc::InvalidOperation, std::strerror(result));
+ }
+#ifdef ENABLE_DEBUG
+ postLockAction(); // Only in debug mode
+#endif // ENABLE_DEBUG
+}
+
void
Mutex::unlock() {
assert(impl_ != NULL);
+#ifdef ENABLE_DEBUG
preUnlockAction(false); // Only in debug mode. Ensure no throw.
+#endif // ENABLE_DEBUG
const int result = pthread_mutex_unlock(&impl_->mutex);
assert(result == 0); // This should never be possible
}
-// TODO: Disable in non-debug build
-bool
-Mutex::locked() const {
- return (impl_->locked_count != 0);
-}
-
class CondVar::Impl {
public:
Impl() {
@@ -187,10 +204,13 @@ CondVar::~CondVar() {
void
CondVar::wait(Mutex& mutex) {
+#ifdef ENABLE_DEBUG
mutex.preUnlockAction(true); // Only in debug mode
const int result = pthread_cond_wait(&impl_->cond_, &mutex.impl_->mutex);
mutex.postLockAction(); // Only in debug mode
-
+#else
+ const int result = pthread_cond_wait(&impl_->cond_, &mutex.impl_->mutex);
+#endif
// pthread_cond_wait should normally succeed unless mutex is completely
// broken.
if (result != 0) {