From: Mukund Sivaraman Date: Sun, 21 Oct 2012 23:26:07 +0000 (+0530) Subject: [2198] Add a block argument to Mutex::Locker's constructor X-Git-Tag: trac2402_base~10^2~13 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f8fb9a002db020b90f423fd82cf5f7f10a050bc7;p=thirdparty%2Fkea.git [2198] Add a block argument to Mutex::Locker's constructor --- diff --git a/src/lib/util/threads/sync.h b/src/lib/util/threads/sync.h index b6cdc8f582..cdbd5c1b4f 100644 --- a/src/lib/util/threads/sync.h +++ b/src/lib/util/threads/sync.h @@ -89,15 +89,24 @@ public: /// \brief Constructor. /// - /// Locks the mutex. May block for extended period of time. + /// Locks the mutex. May block for extended period of time if + /// \c block is true. /// /// \throw isc::InvalidOperation when OS reports error. This usually /// means an attempt to use the mutex in a wrong way (locking /// a mutex second time from the same thread, for example). - Locker(Mutex& mutex) : + /// \throw AlreadyLocked if \c block is false and the mutex is + /// already locked. + Locker(Mutex& mutex, bool block = true) : mutex_(mutex) { - mutex.lock(); + if (block) { + mutex.lock(); + } else { + if (!mutex.tryLock()) { + isc_throw(AlreadyLocked, "The mutex is already locked"); + } + } } /// \brief Destructor.