Author: kpvdr
Date: 2011-04-25 08:48:04 -0400 (Mon, 25 Apr 2011)
New Revision: 4452
Modified:
store/trunk/cpp/lib/jrnl2/ScopedLock.hpp
Log:
Additions to jrnl2 simple locks collection
Modified: store/trunk/cpp/lib/jrnl2/ScopedLock.hpp
===================================================================
--- store/trunk/cpp/lib/jrnl2/ScopedLock.hpp 2011-04-12 14:55:07 UTC (rev 4451)
+++ store/trunk/cpp/lib/jrnl2/ScopedLock.hpp 2011-04-25 12:48:04 UTC (rev 4452)
@@ -48,13 +48,11 @@
protected:
mutable pthread_mutex_t _m;
public:
- inline ScopedMutex()
- {
- PTHREAD_CHK(::pthread_mutex_init(&_m, 0),
"::pthread_mutex_init", "smutex", "smutex");
+ inline ScopedMutex() {
+ PTHREAD_CHK(::pthread_mutex_init(&_m, 0),
"::pthread_mutex_init", "ScopedMutex", "ScopedMutex");
}
- inline ~ScopedMutex()
- {
- PTHREAD_CHK(::pthread_mutex_destroy(&_m),
"::pthread_mutex_destroy", "smutex", "~smutex");
+ inline ~ScopedMutex() {
+ PTHREAD_CHK(::pthread_mutex_destroy(&_m),
"::pthread_mutex_destroy", "ScopedMutex", "~ScopedMutex");
}
inline pthread_mutex_t* get() const { return &_m; }
};
@@ -67,20 +65,18 @@
public:
ScopedMutexContainer(const ScopedMutex& sm) : _sm(sm) {}
virtual ~ScopedMutexContainer() {}
- inline const ScopedMutex& get() const { return _sm; }
+ inline pthread_mutex_t* get() const { return _sm.get(); }
};
// Ultra-simple scoped lock class, auto-releases mutex when it goes out-of-scope
class ScopedLock : public ScopedMutexContainer
{
public:
- inline ScopedLock(const ScopedMutex& sm) : ScopedMutexContainer(sm)
- {
- PTHREAD_CHK(::pthread_mutex_lock(_sm.get()),
"::pthread_mutex_lock", "slock", "slock");
+ inline ScopedLock(const ScopedMutex& sm) : ScopedMutexContainer(sm) {
+ PTHREAD_CHK(::pthread_mutex_lock(_sm.get()),
"::pthread_mutex_lock", "ScopedLock", "ScopedLock");
}
- virtual inline ~ScopedLock()
- {
- PTHREAD_CHK(::pthread_mutex_unlock(_sm.get()),
"::pthread_mutex_unlock", "slock", "~slock");
+ virtual inline ~ScopedLock() {
+ PTHREAD_CHK(::pthread_mutex_unlock(_sm.get()),
"::pthread_mutex_unlock", "ScopedLock", "~ScopedLock");
}
};
@@ -90,20 +86,42 @@
protected:
bool _lockedFlag;
public:
- inline ScopedTryLock(const ScopedMutex& sm) : ScopedMutexContainer(sm),
_lockedFlag(false)
- {
+ inline ScopedTryLock(const ScopedMutex& sm) : ScopedMutexContainer(sm),
_lockedFlag(false) {
int ret = ::pthread_mutex_trylock(_sm.get());
_lockedFlag = (ret == 0); // check if lock obtained
- if (!_lockedFlag && ret != EBUSY) PTHREAD_CHK(ret,
"::pthread_mutex_trylock", "stlock", "stlock");
+ if (!_lockedFlag && ret != EBUSY) PTHREAD_CHK(ret,
"::pthread_mutex_trylock", "ScopedTryLock",
"ScopedTryLock");
}
- virtual inline ~ScopedTryLock()
- {
+ virtual inline ~ScopedTryLock() {
if (_lockedFlag)
- PTHREAD_CHK(::pthread_mutex_unlock(_sm.get()),
"::pthread_mutex_unlock", "stlock", "~stlock");
+ PTHREAD_CHK(::pthread_mutex_unlock(_sm.get()),
"::pthread_mutex_unlock", "ScopedTryLock",
"~ScopedTryLock");
}
inline bool isLocked() const { return _lockedFlag; }
};
+ // Ultra-simple scoped condition variable class
+ class ScopedConditionVariable
+ {
+ protected:
+ mutable pthread_cond_t _cv;
+ public:
+ inline ScopedConditionVariable() {
+ PTHREAD_CHK(::pthread_cond_init(&_cv, 0), "pthread_cond_init",
"ScopedConditionVariable", "ScopedConditionVariable");
+ }
+ inline ~ScopedConditionVariable() {
+ PTHREAD_CHK(::pthread_cond_destroy(&_cv),
"pthread_cond_destroy", "ScopedConditionVariable",
"~ScopedConditionVariable");
+ }
+ inline void wait(ScopedLock& sl) {
+ PTHREAD_CHK(::pthread_cond_wait(&_cv, sl.get()),
"pthread_cond_wait", "ScopedConditionVariable", "wait");
+ }
+ inline void notify_one() {
+ PTHREAD_CHK(::pthread_cond_signal(&_cv), "pthread_cond_signal",
"ScopedConditionVariable", "notify_one");
+ }
+ inline void notify_all() {
+ PTHREAD_CHK(::pthread_cond_broadcast(&_cv),
"pthread_cond_broadcast", "ScopedConditionVariable",
"notify_all");
+ }
+ };
+
+
} // namespace journal2
} // namespace mrg