care for copy operations

This commit is contained in:
Fischlurch 2008-12-28 02:05:51 +01:00
parent 33ae97b50d
commit 00a5e7028d

View file

@ -77,9 +77,12 @@ extern "C" {
#include "lib/reccondition.h" #include "lib/reccondition.h"
} }
#include <boost/noncopyable.hpp>
#include <pthread.h>
#include <cerrno> #include <cerrno>
#include <ctime> #include <ctime>
using boost::noncopyable;
namespace lib { namespace lib {
@ -158,6 +161,11 @@ namespace lib {
using MTX::__enter; using MTX::__enter;
using MTX::__leave; using MTX::__leave;
~Mutex () { }
Mutex () { }
Mutex (const Mutex&); ///< noncopyable...
const Mutex& operator= (const Mutex&);
public: public:
void void
acquire() acquire()
@ -283,6 +291,8 @@ namespace lib {
/** /**
* Object Monitor for synchronisation and waiting. * Object Monitor for synchronisation and waiting.
* Implemented by a (wrapped) set of sync primitives,
* which are default constructible and noncopyable.
*/ */
template<class IMPL> template<class IMPL>
class Monitor class Monitor
@ -294,6 +304,11 @@ namespace lib {
Monitor() {} Monitor() {}
~Monitor() {} ~Monitor() {}
/** allow copy, without interfering with the identity of IMPL */
Monitor (Monitor const& ref) : IMPL(), timeout_(ref.timeout_) { }
const Monitor& operator= (Monitor const& ref) { timeout_ = ref.timeout_; }
void acquireLock() { IMPL::acquire(); } void acquireLock() { IMPL::acquire(); }
void releaseLock() { IMPL::release(); } void releaseLock() { IMPL::release(); }
@ -371,13 +386,13 @@ namespace lib {
public: public:
class Lock class Lock
: private noncopyable
{ {
Monitor& mon_; Monitor& mon_;
public: public:
template<class X> template<class X>
Lock(X* it) : mon_(getMonitor(it)){ mon_.acquireLock(); } Lock(X* it) : mon_(getMonitor(it)){ mon_.acquireLock(); }
Lock(Monitor& m) : mon_(m) { mon_.acquireLock(); }
~Lock() { mon_.releaseLock(); } ~Lock() { mon_.releaseLock(); }
void notify() { mon_.signal(false);} void notify() { mon_.signal(false);}
@ -386,11 +401,12 @@ namespace lib {
template<typename C> template<typename C>
bool wait (C& cond, ulong time=0) { return mon_.wait(cond,time);} bool wait (C& cond, ulong time=0) { return mon_.wait(cond,time);}
bool isTimedWait() { return mon_.isTimedWait(); }
/** convenience shortcut: /** convenience shortcut:
* Locks and immediately enters wait state, * Locks and immediately enters wait state,
* observing a condition defined as member function. * observing a condition defined as member function. */
*/
template<class X> template<class X>
Lock(X* it, bool (X::*method)(void)) Lock(X* it, bool (X::*method)(void))
: mon_(getMonitor(it)) : mon_(getMonitor(it))
@ -398,6 +414,11 @@ namespace lib {
mon_.acquireLock(); mon_.acquireLock();
mon_.wait(*it,method); mon_.wait(*it,method);
} }
protected:
/** for creating a ClassLock */
Lock(Monitor& m) : mon_(m)
{ mon_.acquireLock(); }
}; };