SpinLock.h
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
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       // busy-wait
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


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:04