Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00022
00023 #ifndef ICL_CORE_THREAD_SPIN_LOCK_H_INCLUDED
00024 #define ICL_CORE_THREAD_SPIN_LOCK_H_INCLUDED
00025
00026 #include <boost/atomic.hpp>
00027
00028 namespace icl_core {
00029 namespace thread {
00030
00035 class SpinLock
00036 {
00037 public:
00038 SpinLock()
00039 : m_state(UNLOCKED)
00040 { }
00041
00046 bool lock()
00047 {
00048 while (m_state.exchange(LOCKED, boost::memory_order_acquire) == LOCKED)
00049 {
00050
00051 }
00052 return true;
00053 }
00054
00060 bool tryLock()
00061 {
00062 return (m_state.exchange(LOCKED, boost::memory_order_acquire) == LOCKED);
00063 }
00064
00066 void unlock()
00067 {
00068 m_state.store(UNLOCKED, boost::memory_order_release);
00069 }
00070
00071 private:
00073 enum LockState
00074 {
00075 LOCKED,
00076 UNLOCKED
00077 };
00078
00080 boost::atomic<LockState> m_state;
00081 };
00082
00083 }
00084 }
00085
00086 #endif