1//
2// Event.cpp
3//
4// Library: Foundation
5// Package: Threading
6// Module: Event
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/Event.h"
16
17
18namespace Poco {
19
20
21Event::Event(EventType type): _cond(),
22 _mutex(),
23 _state(false),
24 _autoreset(type == EVENT_AUTORESET)
25{
26}
27
28
29Event::Event(bool autoReset): _cond(),
30 _mutex(),
31 _state(false),
32 _autoreset(autoReset)
33{
34}
35
36
37Event::~Event()
38{
39}
40
41
42void Event::set()
43{
44 try
45 {
46 std::unique_lock<std::mutex> lock(_mutex);
47 _state = true;
48 if (_autoreset)
49 _cond.notify_one();
50 else
51 _cond.notify_all();
52 }
53 catch (std::system_error &e)
54 {
55 throw SystemException(e.what());
56 }
57}
58
59
60void Event::reset()
61{
62 try
63 {
64 std::unique_lock<std::mutex> lock(_mutex);
65 _state = false;
66 }
67 catch (std::system_error &e)
68 {
69 throw SystemException(e.what());
70 }
71}
72
73
74void Event::wait()
75{
76 try
77 {
78 std::unique_lock<std::mutex> lock(_mutex);
79 _cond.wait(lock, [this]() { return this->_state.load(); });
80 if (_autoreset) _state = false;
81 }
82 catch (std::system_error &e)
83 {
84 throw SystemException(e.what());
85 }
86}
87
88
89bool Event::waitImpl(long milliseconds)
90{
91 try
92 {
93 std::unique_lock<std::mutex> lock(_mutex);
94 bool ret = _cond.wait_for(lock, std::chrono::milliseconds(milliseconds), [this]() { return this->_state.load(); });
95 if (ret && _autoreset) _state = false;
96 return ret;
97 }
98 catch (std::system_error &e)
99 {
100 throw SystemException(e.what());
101 }
102}
103
104
105} // namespace Poco
106