1 #ifndef _LOCKABLE_H 2 #define _LOCKABLE_H 3 4 #include <mutex> 5 #include <condition_variable> 6 7 namespace acng 8 { 9 10 typedef std::mutex acmutex; 11 12 struct base_with_mutex 13 { 14 std::mutex m_obj_mutex; 15 }; 16 17 // little adapter for more convenient use 18 struct lockguard { 19 std::lock_guard<std::mutex> _guard; lockguardlockguard20 lockguard(std::mutex& mx) : _guard(mx) {} lockguardlockguard21 lockguard(std::mutex* mx) : _guard(*mx) {} lockguardlockguard22 lockguard(base_with_mutex& mbase) : _guard(mbase.m_obj_mutex) {} lockguardlockguard23 lockguard(base_with_mutex* mbase) : _guard(mbase->m_obj_mutex) {} 24 }; 25 26 struct lockuniq { 27 std::unique_lock<std::mutex> _guard; lockuniqlockuniq28 lockuniq(std::mutex& mx) : _guard(mx) {} lockuniqlockuniq29 lockuniq(base_with_mutex& mbase) : _guard(mbase.m_obj_mutex) {} lockuniqlockuniq30 lockuniq(base_with_mutex* mbase) : _guard(mbase->m_obj_mutex) {} unLocklockuniq31 void unLock() { _guard.unlock();} reLocklockuniq32 void reLock() { _guard.lock(); } reLockSafelockuniq33 void reLockSafe() { if(!_guard.owns_lock()) _guard.lock(); } 34 }; 35 36 struct base_with_condition : public base_with_mutex 37 { 38 std::condition_variable m_obj_cond; notifyAllbase_with_condition39 void notifyAll() { m_obj_cond.notify_all(); } waitbase_with_condition40 void wait(lockuniq& uli) { m_obj_cond.wait(uli._guard); } 41 bool wait_until(lockuniq& uli, time_t nUTCsecs, long msec); 42 bool wait_for(lockuniq& uli, long secs, long msec); 43 }; 44 45 #define setLockGuard std::lock_guard<std::mutex> local_helper_lockguard(m_obj_mutex); 46 47 } 48 49 #endif 50