Diligent Engine API Reference
LockHelper.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
26 #include <thread>
27 #include <mutex>
28 #include <condition_variable>
29 
30 #include "Atomics.h"
31 
32 namespace ThreadingTools
33 {
34 
35 class LockFlag
36 {
37 public:
38  enum {LOCK_FLAG_UNLOCKED = 0, LOCK_FLAG_LOCKED = 1};
39  LockFlag(Atomics::Long InitFlag = LOCK_FLAG_UNLOCKED)
40  {
41  //m_Flag.store(InitFlag);
42  m_Flag = InitFlag;
43  }
44 
45  operator Atomics::Long()const{return m_Flag;}
46 
47 private:
48  friend class LockHelper;
49  Atomics::AtomicLong m_Flag;
50 };
51 
52 class LockHelper
53 {
54 public:
55 
56  LockHelper() :
57  m_pLockFlag(nullptr)
58  {
59  }
60  LockHelper(LockFlag &LockFlag) :
61  m_pLockFlag(nullptr)
62  {
63  Lock(LockFlag);
64  }
65 
66  LockHelper( LockHelper &&LockHelper ) :
67  m_pLockFlag( std::move(LockHelper.m_pLockFlag) )
68  {
69  LockHelper.m_pLockFlag = nullptr;
70  }
71 
72  const LockHelper& operator = (LockHelper &&LockHelper)
73  {
74  m_pLockFlag = std::move( LockHelper.m_pLockFlag );
75  LockHelper.m_pLockFlag = nullptr;
76  return *this;
77  }
78 
79  ~LockHelper()
80  {
81  Unlock();
82  }
83 
84  static bool UnsafeTryLock(LockFlag &LockFlag)
85  {
86  return Atomics::AtomicCompareExchange( LockFlag.m_Flag,
87  static_cast<Atomics::Long>( LockFlag::LOCK_FLAG_LOCKED ),
88  static_cast<Atomics::Long>( LockFlag::LOCK_FLAG_UNLOCKED) ) == LockFlag::LOCK_FLAG_UNLOCKED;
89  }
90 
91  bool TryLock(LockFlag &LockFlag)
92  {
93  if( UnsafeTryLock( LockFlag) )
94  {
95  m_pLockFlag = &LockFlag;
96  return true;
97  }
98  else
99  return false;
100  }
101 
102  static void UnsafeLock(LockFlag &LockFlag)
103  {
104  while( !UnsafeTryLock( LockFlag ) )
105  std::this_thread::yield();
106  }
107 
108  void Lock(LockFlag &LockFlag)
109  {
110  VERIFY( m_pLockFlag == NULL, "Object already locked" );
111  // Wait for the flag to become unlocked and lock it
112  while( !TryLock( LockFlag ) )
113  std::this_thread::yield();
114  }
115 
116  static void UnsafeUnlock(LockFlag &LockFlag)
117  {
118  LockFlag.m_Flag = LockFlag::LOCK_FLAG_UNLOCKED;
119  }
120 
121  void Unlock()
122  {
123  if( m_pLockFlag )
124  UnsafeUnlock(*m_pLockFlag);
125  m_pLockFlag = NULL;
126  }
127 
128 private:
129  LockFlag *m_pLockFlag;
130  LockHelper( const LockHelper &LockHelper );
131  const LockHelper& operator = ( const LockHelper &LockHelper );
132 };
133 
134 class Signal
135 {
136 public:
137  Signal() {}
138 
139  // http://en.cppreference.com/w/cpp/thread/condition_variable
140  void Trigger()
141  {
142  // The thread that intends to modify the variable has to
143  // * acquire a std::mutex (typically via std::lock_guard)
144  // * perform the modification while the lock is held
145  // * execute notify_one or notify_all on the std::condition_variable (the lock does not need to be held for notification)
146  {
147  // std::condition_variable works only with std::unique_lock<std::mutex>
148  std::lock_guard<std::mutex> Lock(m_Mutex);
149  m_bIsTriggered = true;
150  }
151  // Unlocking is done before notifying, to avoid waking up the waiting
152  // thread only to block again (see notify_one for details)
153  m_CondVar.notify_one();
154  }
155 
156  void Wait()
157  {
158  // Any thread that intends to wait on std::condition_variable has to
159  // * acquire a std::unique_lock<std::mutex>, on the SAME MUTEX as used to protect the shared variable
160  // * execute wait, wait_for, or wait_until. The wait operations atomically release the mutex
161  // and suspend the execution of the thread.
162  // * When the condition variable is notified, a timeout expires, or a spurious wakeup occurs,
163  // the thread is awakened, and the mutex is atomically reacquired:
164  // - The thread should then check the condition and resume waiting if the wake up was spurious.
165  std::unique_lock<std::mutex> Lock(m_Mutex);
166  if (!m_bIsTriggered)
167  {
168  m_CondVar.wait(Lock, [&] {return m_bIsTriggered; });
169  }
170  }
171 
172  void Reset()
173  {
174  std::lock_guard<std::mutex> Lock(m_Mutex);
175  m_bIsTriggered = false;
176  }
177 
178  volatile bool IsTriggered()const { return m_bIsTriggered; }
179 
180 private:
181 
182  std::mutex m_Mutex;
183  std::condition_variable m_CondVar;
184  volatile bool m_bIsTriggered = false;
185 
186  Signal(const Signal&) = delete;
187  Signal& operator = (const Signal&) = delete;
188 };
189 
190 }
Definition: AdvancedMath.h:316
Definition: LockHelper.h:32