SpinLock.h
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef ICL_CORE_THREAD_SPIN_LOCK_H_INCLUDED
24 #define ICL_CORE_THREAD_SPIN_LOCK_H_INCLUDED
25 
26 #include <boost/atomic.hpp>
27 
28 namespace icl_core {
29 namespace thread {
30 
35 class SpinLock
36 {
37 public:
39  : m_state(UNLOCKED)
40  { }
41 
46  bool lock()
47  {
48  while (m_state.exchange(LOCKED, boost::memory_order_acquire) == LOCKED)
49  {
50  // busy-wait
51  }
52  return true;
53  }
54 
60  bool tryLock()
61  {
62  return (m_state.exchange(LOCKED, boost::memory_order_acquire) == LOCKED);
63  }
64 
66  void unlock()
67  {
68  m_state.store(UNLOCKED, boost::memory_order_release);
69  }
70 
71 private:
73  enum LockState
74  {
77  };
78 
80  boost::atomic<LockState> m_state;
81 };
82 
83 }
84 }
85 
86 #endif
LockState
States of the lock.
Definition: SpinLock.h:73
boost::atomic< LockState > m_state
Current lock state.
Definition: SpinLock.h:80
void unlock()
Unlocks the mutex.
Definition: SpinLock.h:66


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58