@@ -10,73 +10,92 @@ namespace HttpServer
1010
1111 void Event::wait ()
1212 {
13- std::unique_lock<std::mutex> lck (mtx);
14-
15- while (false == signaled)
13+ if (false == this ->signaled .load () )
1614 {
17- cv.wait (lck);
15+ std::unique_lock<std::mutex> lck (this ->mtx );
16+
17+ do
18+ {
19+ this ->cv .wait (lck);
20+ }
21+ while (false == this ->signaled .load () );
1822 }
1923
20- if (false == manually)
24+ if (false == this -> manually )
2125 {
22- signaled = false ;
26+ this -> signaled . store ( false ) ;
2327 }
2428 }
2529
2630 bool Event::wait_for (const std::chrono::milliseconds &ms)
2731 {
28- std::unique_lock<std::mutex> lck (mtx) ;
32+ bool is_timeout = false ;
2933
30- auto const status = cv.wait_for (lck, ms);
34+ if (false == this ->signaled .load () )
35+ {
36+ std::unique_lock<std::mutex> lck (this ->mtx );
37+
38+ is_timeout = false == this ->cv .wait_for (lck, ms, [this ] { return this ->notifed (); } );
39+ }
3140
32- if (false == manually)
41+ if (false == this -> manually )
3342 {
34- signaled = false ;
43+ this -> signaled . store ( false ) ;
3544 }
3645
37- return std::cv_status::timeout == status ;
46+ return is_timeout ;
3847 }
3948
4049 bool Event::wait_until (const std::chrono::high_resolution_clock::time_point &tp)
4150 {
42- std::unique_lock<std::mutex> lck (mtx);
51+ bool is_timeout = false ;
52+
53+ if (false == this ->signaled .load () )
54+ {
55+ std::unique_lock<std::mutex> lck (this ->mtx );
4356
44- auto const status = cv.wait_until (lck, tp);
57+ do
58+ {
59+ if (std::cv_status::timeout == this ->cv .wait_until (lck, tp) )
60+ {
61+ is_timeout = true ;
62+ break ;
63+ }
64+ }
65+ while (false == this ->signaled .load () );
66+ }
4567
46- if (false == manually)
68+ if (false == this -> manually )
4769 {
48- signaled = false ;
70+ this -> signaled . store ( false ) ;
4971 }
5072
51- return std::cv_status::timeout == status ;
73+ return is_timeout ;
5274 }
5375
5476 void Event::notify ()
5577 {
56- signaled = true ;
57- cv.notify_all ();
78+ this -> signaled . store ( true ) ;
79+ this -> cv .notify_all ();
5880 }
5981
6082 void Event::notify (const size_t threadsCount)
6183 {
62- if (threadsCount)
63- {
64- signaled = true ;
84+ this ->signaled .store (true );
6585
66- for (size_t i = 0 ; i < threadsCount; ++i)
67- {
68- cv.notify_one ();
69- }
86+ for (size_t i = 0 ; i < threadsCount; ++i)
87+ {
88+ this ->cv .notify_one ();
7089 }
7190 }
7291
7392 void Event::reset ()
7493 {
75- signaled = false ;
94+ this -> signaled . store ( false ) ;
7695 }
7796
7897 bool Event::notifed () const
7998 {
80- return signaled;
99+ return this -> signaled . load () ;
81100 }
82101};
0 commit comments