From: JINMEI Tatuya Date: Mon, 15 Oct 2012 20:31:04 +0000 (-0700) Subject: [2332] Make sure preUnlockAction() doesn't throw when it shouldn't. X-Git-Tag: trac2402_base~19^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7f94f671e0da9c98cabcc38433852732294bee4a;p=thirdparty%2Fkea.git [2332] Make sure preUnlockAction() doesn't throw when it shouldn't. adding a parameter to control that. --- diff --git a/src/lib/util/threads/lock.cc b/src/lib/util/threads/lock.cc index 13d0a0d671..3f4d92ada6 100644 --- a/src/lib/util/threads/lock.cc +++ b/src/lib/util/threads/lock.cc @@ -128,9 +128,14 @@ Mutex::lock() { } void -Mutex::preUnlockAction() { +Mutex::preUnlockAction(bool throw_ok) { if (impl_->locked_count == 0) { - isc_throw(isc::InvalidOperation, "Unlock attempt for unlocked mutex"); + if (throw_ok) { + isc_throw(isc::InvalidOperation, + "Unlock attempt for unlocked mutex"); + } else { + assert(false); + } } --impl_->locked_count; } @@ -138,7 +143,7 @@ Mutex::preUnlockAction() { void Mutex::unlock() { assert(impl_ != NULL); - preUnlockAction(); // Only in debug mode + preUnlockAction(false); // Only in debug mode. Ensure no throw. const int result = pthread_mutex_unlock(&impl_->mutex); assert(result == 0); // This should never be possible } @@ -181,7 +186,7 @@ CondVar::~CondVar() { void CondVar::wait(Mutex& mutex) { - mutex.preUnlockAction(); // Only in debug mode + mutex.preUnlockAction(true); // Only in debug mode const int result = pthread_cond_wait(&impl_->cond_, &mutex.impl_->mutex); mutex.postLockAction(); // Only in debug mode diff --git a/src/lib/util/threads/lock.h b/src/lib/util/threads/lock.h index 16b386fb7d..25247c17f8 100644 --- a/src/lib/util/threads/lock.h +++ b/src/lib/util/threads/lock.h @@ -116,7 +116,12 @@ private: // Commonly called before releasing the lock, checking and updating // internal state for debug. - void preUnlockAction(); + // + // If throw_ok is true, it throws \c isc::InvalidOperation when the check + // fails; otherwise it aborts the process. This parameter must be set + // to false if the call to this shouldn't result in an exception (e.g. + // when called from a destructor). + void preUnlockAction(bool throw_ok); class Impl; Impl* impl_;