1 | // |
2 | // Event.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Threading |
6 | // Module: Event |
7 | // |
8 | // Definition of the Event class. |
9 | // |
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_Event_INCLUDED |
18 | #define Foundation_Event_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/Exception.h" |
23 | #include <condition_variable> |
24 | #include <mutex> |
25 | #include <atomic> |
26 | |
27 | |
28 | namespace Poco { |
29 | |
30 | |
31 | class Foundation_API Event |
32 | /// An Event is a synchronization object that |
33 | /// allows one thread to signal one or more |
34 | /// other threads that a certain event |
35 | /// has happened. |
36 | /// Usually, one thread signals an event, |
37 | /// while one or more other threads wait |
38 | /// for an event to become signalled. |
39 | { |
40 | public: |
41 | enum EventType |
42 | { |
43 | EVENT_MANUALRESET, /// Manual reset event |
44 | EVENT_AUTORESET /// Auto-reset event |
45 | }; |
46 | |
47 | explicit Event(EventType type = EVENT_AUTORESET); |
48 | /// Creates the event. If type is EVENT_AUTORESET, |
49 | /// the event is automatically reset after |
50 | /// a wait() successfully returns. |
51 | |
52 | //@ deprecated |
53 | explicit Event(bool autoReset); |
54 | /// Please use Event::Event(EventType) instead. |
55 | |
56 | ~Event(); |
57 | /// Destroys the event. |
58 | |
59 | void set(); |
60 | /// Signals the event. If autoReset is true, |
61 | /// only one thread waiting for the event |
62 | /// can resume execution. |
63 | /// If autoReset is false, all waiting threads |
64 | /// can resume execution. |
65 | |
66 | void wait(); |
67 | /// Waits for the event to become signalled. |
68 | |
69 | void wait(long milliseconds); |
70 | /// Waits for the event to become signalled. |
71 | /// Throws a TimeoutException if the event |
72 | /// does not become signalled within the specified |
73 | /// time interval. |
74 | |
75 | bool tryWait(long milliseconds); |
76 | /// Waits for the event to become signalled. |
77 | /// Returns true if the event |
78 | /// became signalled within the specified |
79 | /// time interval, false otherwise. |
80 | |
81 | void reset(); |
82 | /// Resets the event to unsignalled state. |
83 | |
84 | private: |
85 | Event(const Event&); |
86 | Event& operator = (const Event&); |
87 | |
88 | bool waitImpl(long milliseconds); |
89 | |
90 | std::condition_variable _cond; |
91 | std::mutex _mutex; |
92 | std::atomic<bool> _state; |
93 | bool _autoreset; |
94 | }; |
95 | |
96 | |
97 | // |
98 | // inlines |
99 | // |
100 | |
101 | inline void Event::wait(long milliseconds) |
102 | { |
103 | if (!waitImpl(milliseconds)) throw TimeoutException(); |
104 | } |
105 | |
106 | |
107 | inline bool Event::tryWait(long milliseconds) |
108 | { |
109 | return waitImpl(milliseconds); |
110 | } |
111 | |
112 | |
113 | } // namespace Poco |
114 | |
115 | |
116 | #endif // Foundation_Event_INCLUDED |
117 | |