sttcl  v0.9c
STTCL C++ template state machine framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SttclMutex.h
Go to the documentation of this file.
1 
25 #ifndef STTCLMUTEX_H_
26 #define STTCLMUTEX_H_
27 
28 #include "SttclTime.h"
29 
30 #ifndef STTCL_DEFAULT_MUTEXIMPL
31 #if defined(STTCL_POSIX_THREADS) or defined(STTCL_POSIX_IMPL)
32 #include "../PosixThreads/SttclPosixMutex.h"
33 #define STTCL_DEFAULT_MUTEXIMPL sttcl::internal::posix_impl::SttclPosixMutex
34 #elif defined(STTCL_BOOST_THREADS) or defined(STTCL_BOOST_IMPL)
35 #include "../BoostThreads/SttclBoostMutex.h"
36 #define STTCL_DEFAULT_MUTEXIMPL sttcl::internal::boost_impl::SttclBoostMutex
37 #elif defined(STTCL_CX11_THREADS) or defined(STTCL_CX11_IMPL)
38 #include "../C++11Threads/SttclCx11Mutex.h"
39 #define STTCL_DEFAULT_MUTEXIMPL sttcl::internal::cx11_impl::SttclCx11Mutex
40 #endif
41 
42 #ifndef STTCL_DEFAULT_MUTEXIMPL
43 #error "You need to define a default mutex implementation for sttcl"
44 #endif
45 #endif
46 
47 namespace sttcl
48 {
49 
50 namespace internal
51 {
52 
59 template
60 < class Impl = STTCL_DEFAULT_MUTEXIMPL
61 , class TimeDurationType = TimeDuration<STTCL_DEFAULT_TIMEDURATIONIMPL>
62 >
64 : public Impl
65 {
66 public:
70  SttclMutex() : Impl() {}
75 
79  void lock()
80  {
81  static_cast<Impl*>(this)->lock();
82  }
83 
92  bool try_lock(const TimeDurationType& timeout = TimeDurationType::Zero)
93  {
94  return static_cast<Impl*>(this)->try_lock(timeout);
95  }
96 
100  void unlock()
101  {
102  static_cast<Impl*>(this)->unlock();
103  }
104 };
105 
110 template<class Lockable>
112 {
113 public:
118  AutoLocker(Lockable& argLockableRef)
119  : ref(argLockableRef)
120  {
121  ref.lock();
122  }
123 
128  {
129  ref.unlock();
130  }
131 private:
132  Lockable& ref;
133 };
134 
135 }
136 }
137 
138 #endif /* STTCLMUTEX_H_ */