cpp: Let Monitors share Mutex instances
- Let Monitor objects share a Mutex() instance so that more than one
condition can be implemented on top of a single mutex protecting an
important data structure.
- Make Mutex and Monitor noncopyable
- Add an accessor to Mutex() so the underlying pthread_mutex_t* can be
retrieved for passing to pthread_condwait
- Change Monitor to use the actual Mutex class instead of creating a
naked pthread_mutex_t on its own
- Add new constructors to Monitor
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@920666 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/concurrency/Monitor.h b/lib/cpp/src/concurrency/Monitor.h
index 234bf32..f29119d 100644
--- a/lib/cpp/src/concurrency/Monitor.h
+++ b/lib/cpp/src/concurrency/Monitor.h
@@ -21,6 +21,10 @@
#define _THRIFT_CONCURRENCY_MONITOR_H_ 1
#include "Exception.h"
+#include "Mutex.h"
+
+#include <boost/utility.hpp>
+
namespace apache { namespace thrift { namespace concurrency {
@@ -29,7 +33,12 @@
* notifying condition events requires that the caller own the mutex. Mutex
* lock and unlock operations can be performed independently of condition
* events. This is more or less analogous to java.lang.Object multi-thread
- * operations
+ * operations.
+ *
+ * Note the Monitor can create a new, internal mutex; alternatively, a
+ * separate Mutex can be passed in and the Monitor will re-use it without
+ * taking ownership. It's the user's responsibility to make sure that the
+ * Mutex is not deallocated before the Monitor.
*
* Note that all methods are const. Monitors implement logical constness, not
* bit constness. This allows const methods to call monitor methods without
@@ -37,14 +46,22 @@
*
* @version $Id:$
*/
-class Monitor {
-
+class Monitor : boost::noncopyable {
public:
-
+ /** Creates a new mutex, and takes ownership of it. */
Monitor();
+ /** Uses the provided mutex without taking ownership. */
+ explicit Monitor(Mutex* mutex);
+
+ /** Uses the mutex inside the provided Monitor without taking ownership. */
+ explicit Monitor(Monitor* monitor);
+
+ /** Deallocates the mutex only if we own it. */
virtual ~Monitor();
+ Mutex& mutex() const;
+
virtual void lock() const;
virtual void unlock() const;
@@ -64,18 +81,11 @@
class Synchronized {
public:
-
- Synchronized(const Monitor& value) :
- monitor_(value) {
- monitor_.lock();
- }
-
- ~Synchronized() {
- monitor_.unlock();
- }
+ Synchronized(const Monitor* monitor) : g(monitor->mutex()) { }
+ Synchronized(const Monitor& monitor) : g(monitor.mutex()) { }
private:
- const Monitor& monitor_;
+ Guard g;
};