cpp: non-blocking add for ThreadManager
It's rare for the ThreadManager mutex to be contended, but it is
possible. For nonblocking applications, it is necessary to have a
strict timeout for the lock acquisition. With this change, that timeout
is enforced. Also add timeout parameters to Mutex::lock and
Guard::Guard to support this feature.
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@920679 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/concurrency/Mutex.cpp b/lib/cpp/src/concurrency/Mutex.cpp
index 5d33c11..67d7a2c 100644
--- a/lib/cpp/src/concurrency/Mutex.cpp
+++ b/lib/cpp/src/concurrency/Mutex.cpp
@@ -18,6 +18,7 @@
*/
#include "Mutex.h"
+#include "Util.h"
#include <assert.h>
#include <pthread.h>
@@ -50,6 +51,12 @@
bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); }
+ bool timedlock(int64_t milliseconds) const {
+ struct timespec ts;
+ Util::toTimespec(ts, milliseconds);
+ return (0 == pthread_mutex_timedlock(&pthread_mutex_, &ts));
+ }
+
void unlock() const { pthread_mutex_unlock(&pthread_mutex_); }
void* getUnderlyingImpl() const { return (void*) &pthread_mutex_; }
@@ -67,6 +74,8 @@
bool Mutex::trylock() const { return impl_->trylock(); }
+bool Mutex::timedlock(int64_t ms) const { return impl_->timedlock(ms); }
+
void Mutex::unlock() const { impl_->unlock(); }
void Mutex::DEFAULT_INITIALIZER(void* arg) {