forked from awwit/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.cpp
More file actions
101 lines (80 loc) · 1.62 KB
/
Event.cpp
File metadata and controls
101 lines (80 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Event.h"
namespace HttpServer
{
Event::Event(const bool _signaled, const bool _manually): signaled(_signaled), manually(_manually)
{
}
void Event::wait()
{
if (false == this->signaled.load() )
{
std::unique_lock<std::mutex> lck(this->mtx);
do
{
this->cv.wait(lck);
}
while (false == this->signaled.load() );
}
if (false == this->manually)
{
this->signaled.store(false);
}
}
bool Event::wait_for(const std::chrono::milliseconds &ms)
{
bool is_timeout = false;
if (false == this->signaled.load() )
{
std::unique_lock<std::mutex> lck(this->mtx);
is_timeout = false == this->cv.wait_for(lck, ms, [this] { return this->notifed(); } );
}
if (false == this->manually)
{
this->signaled.store(false);
}
return is_timeout;
}
bool Event::wait_until(const std::chrono::high_resolution_clock::time_point &tp)
{
bool is_timeout = false;
if (false == this->signaled.load() )
{
std::unique_lock<std::mutex> lck(this->mtx);
do
{
if (std::cv_status::timeout == this->cv.wait_until(lck, tp) )
{
is_timeout = true;
break;
}
}
while (false == this->signaled.load() );
}
if (false == this->manually)
{
this->signaled.store(false);
}
return is_timeout;
}
void Event::notify()
{
this->signaled.store(true);
this->cv.notify_all();
}
void Event::notify(const size_t threadsCount)
{
this->signaled.store(true);
for (size_t i = 0; i < threadsCount; ++i)
{
this->cv.notify_one();
}
}
void Event::reset()
{
this->signaled.store(false);
}
bool Event::notifed() const
{
return this->signaled.load();
}
};