SpiecsEngine
 
Loading...
Searching...
No Matches
Semaphore.h
Go to the documentation of this file.
1/**
2* @file Semaphore.h
3* @brief The Semaphore Class Definitions and Implementation.
4* @author Spices.
5*/
6
7#pragma once
8#include "Core/Core.h"
9
10namespace Spices {
11
12 /**
13 * @brief std::semaphore Implementation.
14 */
16 {
17 public:
18
19 /**
20 * @brief Constructor Function.
21 * @param[in] initSign initial sign.
22 */
23 semaphore(int initSign = 0) : m_Sign(initSign) {}
24
25 /**
26 * @brief Destructor Function.
27 */
28 virtual ~semaphore() = default;
29
30 /**
31 * @brief Sign this semaphore with specific value.
32 * @param[in] sign .
33 */
34 void Sign(int sign)
35 {
36 std::unique_lock<std::mutex> lock(m_Mutex);
37
38 m_Sign = sign;
39 m_Cond.notify_one();
40 }
41
42 /**
43 * @brief Increase this semaphore sign.
44 */
45 void Increase()
46 {
47 std::unique_lock<std::mutex> lock(m_Mutex);
48
49 ++m_Sign;
50 m_Cond.notify_one();
51 }
52
53 /**
54 * @brief Wait this semaphore until it owns equal sign with given.
55 * @param[in] sign .
56 */
57 void Wait(int sign)
58 {
59 if (m_Sign.load() != sign)
60 {
61 std::unique_lock<std::mutex> lock(m_Mutex);
62
63 m_Cond.wait(lock, [&]() { return m_Sign.load() == sign; });
64 }
65 }
66
67 /**
68 * @brief Get this semaphore current sign.
69 */
70 const int GetSign() const { return m_Sign.load(); }
71
72 private:
73
74 /**
75 * @brief Sign of this semaphore.
76 */
77 std::atomic_int m_Sign;
78
79 /**
80 * @brief Mutex of this semaphore.
81 */
82 std::mutex m_Mutex;
83
84 /**
85 * @brief condition variable of this semaphore.
86 */
87 std::condition_variable m_Cond;
88 };
89}
#define SPICES_PROFILE_ZONE
ThreadInitCallback m_ThreadInitCallback
ThreadInitCallback.
EventLoop * GetNextLoop()
Get next thread EventLoop.
InetAddress m_ListenAddress
ListenAddress.
std::vector< EventLoop * > GetAllLoops()
Get all thread EventLoop.
semaphore m_IsThreadsPrepared
ThreadsPrepared condition.
void ThreadFunc(Thread<> *thread)
Thread Function.
EventLoopThreadPool(const InetAddress &listenAddress)
Constructor Function. Create Specific ThreadPool.
void Start(int initThreadSize=3, ThreadInitCallback cb=nullptr)
Start Run this thread pool.
std::vector< EventLoop * > m_Loops
All threads EventLoop collection.
const std::string & Name() const
Get ThreadPool Name.
virtual ~EventLoopThreadPool() override=default
Destructor Function.
ThreadPool of Multiple threading EventLoop.
static EventLoop *& GetInst(InetAddress *address=nullptr)
Get EventLoop Instance. @reutrn Returns EventLoop Instance.
Wrapper of Instance/Delete ThreadCache in thread.
Definition EventLoop.h:180
void Loop()
Start Event Loop.
Definition EventLoop.cpp:48
Wrapper of Poller and wakeup socket to acceptor(SubLoop).
Definition EventLoop.h:28
This class is Wrapper of current socket address.
Definition InetAddress.h:22
static bool SetThreadName(const std::string &name)
Set Thread name.
Thread Static Function Library.
Thread Function Object.
std::mutex m_Mutex
Mutex of this semaphore.
Definition Semaphore.h:82
std::atomic_int m_Sign
Sign of this semaphore.
Definition Semaphore.h:77
semaphore(int initSign=0)
Constructor Function.
Definition Semaphore.h:23
void Sign(int sign)
Sign this semaphore with specific value.
Definition Semaphore.h:34
void Increase()
Increase this semaphore sign.
Definition Semaphore.h:45
std::condition_variable m_Cond
condition variable of this semaphore.
Definition Semaphore.h:87
void Wait(int sign)
Wait this semaphore until it owns equal sign with given.
Definition Semaphore.h:57
virtual ~semaphore()=default
Destructor Function.
const int GetSign() const
Get this semaphore current sign.
Definition Semaphore.h:70
std::semaphore Implementation.
Definition Semaphore.h:16