Newton Dynamics  4.00
dSemaphore.h
1 /* Copyright (c) <2003-2019> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef _D_SEMAPHORE_H_
23 #define _D_SEMAPHORE_H_
24 
25 #include "dCoreStdafx.h"
26 
29 {
30  public:
32  D_CORE_API dSemaphore();
33 
35  D_CORE_API ~dSemaphore();
36 
38  D_CORE_API dInt32 GetCount() const;
39 
47  D_CORE_API bool Wait();
48 
51  D_CORE_API void Signal();
52 
54  D_CORE_API void Terminate();
55 
56 #ifndef D_USE_THREAD_EMULATION
57  private:
58  mutable std::mutex m_mutex;
59  std::condition_variable m_condition;
60  dInt32 m_count;
61  dAtomic<bool> m_terminate;
62 #endif
63 };
64 
65 #endif
dSemaphore::Wait
D_CORE_API bool Wait()
Synchronize with another threads.
Definition: dSemaphore.cpp:49
dSemaphore::Signal
D_CORE_API void Signal()
Notify a thread blocked by member function Wait to wake and test m_counter again.
Definition: dSemaphore.cpp:65
dSemaphore::~dSemaphore
D_CORE_API ~dSemaphore()
Destroy semaphore.
Definition: dSemaphore.cpp:35
dAtomic< bool >
dSemaphore::GetCount
D_CORE_API dInt32 GetCount() const
Returns counter counter value.
Definition: dSemaphore.cpp:39
dSemaphore
Generic counting semaphore for thread synchronization.
Definition: dSemaphore.h:29
dSemaphore::dSemaphore
D_CORE_API dSemaphore()
Create and initialize counter to zero.
Definition: dSemaphore.cpp:25
dSemaphore::Terminate
D_CORE_API void Terminate()
Notify a waiting thread on member function Wait that is time to exit the thread loop.
Definition: dSemaphore.cpp:74