xtd 0.2.0
Loading...
Searching...
No Matches
wait_handle.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include "timeout.hpp"
7#include "../abstract.hpp"
8#include "../as.hpp"
9#include "../size_object.hpp"
10#include "../time_span.hpp"
11#include "../types.hpp"
12#include <limits>
13#include <vector>
14
16namespace xtd {
18 namespace threading {
20 class registered_wait_handle;
21 class thread;
23
53 friend class registered_wait_handle;
54 friend class thread;
55 public:
57
63 static const intptr invalid_handle;
64
67 static constexpr size_t wait_timeout = size_object::max_value;
69
71
74 wait_handle() = default;
76
80
82
86 virtual intptr handle() const noexcept = 0;
89 virtual void handle(intptr value) = 0;
91
93
97 virtual void close();
98
103 virtual bool wait_one();
104
111 virtual bool wait_one(int32 milliseconds_timeout);
112
119 virtual bool wait_one(const time_span& timeout);
121
123
132 static bool signal_and_wait(wait_handle& to_signal, wait_handle& to_wait);
133
142 static bool signal_and_wait(wait_handle& to_signal, wait_handle& to_wait, int32 milliseconds_timeout);
143
152 static bool signal_and_wait(wait_handle& to_signal, wait_handle& to_wait, const time_span& timeout);
153
160 template<typename collection_t>
161 static bool wait_all(const collection_t& wait_handles) {return wait_all(wait_handles, timeout::infinite);}
162
171 template<typename collection_t>
172 static bool wait_all(const collection_t& wait_handles, int32 milliseconds_timeout) {
173 std::vector<wait_handle*> wait_handle_pointers;
174 for (auto& item : wait_handles)
175 wait_handle_pointers.push_back(const_cast<wait_handle*>(as<wait_handle>(&item)));
176 return wait_all(wait_handle_pointers, milliseconds_timeout);
177 }
178
187 template<typename collection_t>
188 static bool wait_all(const collection_t& wait_handles, const time_span& timeout) {return wait_all(wait_handles, as<int32>(timeout.total_milliseconds_duration().count()));}
189
197 template<typename collection_t>
198 static size_t wait_any(const collection_t& wait_handles) {return wait_any(wait_handles, timeout::infinite);}
199
208 template<typename collection_t>
209 static size_t wait_any(const collection_t& wait_handles, int32 milliseconds_timeout) {
210 std::vector<wait_handle*> wait_handle_pointers;
211 for (auto& item : wait_handles)
212 wait_handle_pointers.push_back(const_cast<wait_handle*>(as<wait_handle>(&item)));
213 return wait_any(wait_handle_pointers, milliseconds_timeout);
214 }
215
224 template<typename collection_t>
225 static size_t wait_any(const collection_t& wait_handles, const time_span& timeout) {return wait_any(wait_handles, as<int32>(timeout.total_milliseconds_duration().count()));}
227
229 template <typename... items_t>
230 static size_t wait_all(items_t... items) {return wait_all(timeout::infinite, items...);}
231 template <typename... items_t>
232 static size_t wait_all(const time_span& timeout, items_t... items) {return wait_all(as<int32>(timeout.total_milliseconds()), items...);}
233 template <typename... items_t>
234 static size_t wait_all(int32 milliseconds_timeout, items_t... items) {
235 std::vector<wait_handle*> wait_handle_pointers;
236 fill_wait_handle_pointers(wait_handle_pointers, items...);
237 return wait_all(wait_handle_pointers, milliseconds_timeout);
238 }
239 template<typename item_t>
240 static bool wait_all(const std::initializer_list<item_t>& wait_handles) {return wait_all(wait_handles, timeout::infinite);}
241 template<typename item_t>
242 static bool wait_all(const std::initializer_list<item_t>& wait_handles, int32 milliseconds_timeout) {
243 std::vector<wait_handle*> wait_handle_pointers;
244 for (auto& item : wait_handles)
245 wait_handle_pointers.push_back(const_cast<wait_handle*>(as<wait_handle>(&item)));
246 return wait_all(wait_handle_pointers, milliseconds_timeout);
247 }
248 template<typename item_t>
249 static bool wait_all(const std::initializer_list<item_t>& wait_handles, const time_span& timeout) {return wait_all(wait_handles, as<int32>(timeout.total_milliseconds_duration().count()));}
250 static bool wait_all(const std::initializer_list<xtd::sptr<wait_handle>>& wait_handles);
251 static bool wait_all(const std::initializer_list<xtd::sptr<wait_handle>>& wait_handles, int32 milliseconds_timeout);
252 static bool wait_all(const std::initializer_list<xtd::sptr<wait_handle>>& wait_handles, const time_span& timeout);
253 static bool wait_all(const std::initializer_list<xtd::uptr<wait_handle>>& wait_handles);
254 static bool wait_all(const std::initializer_list<xtd::uptr<wait_handle>>& wait_handles, int32 milliseconds_timeout);
255 static bool wait_all(const std::initializer_list<xtd::uptr<wait_handle>>& wait_handles, const time_span& timeout);
256 static bool wait_all(const std::vector<xtd::sptr<wait_handle>>& wait_handles);
257 static bool wait_all(const std::vector<xtd::sptr<wait_handle>>& wait_handles, int32 milliseconds_timeout);
258 static bool wait_all(const std::vector<xtd::sptr<wait_handle>>& wait_handles, const time_span& timeout);
259 static bool wait_all(const std::vector<xtd::uptr<wait_handle>>& wait_handles);
260 static bool wait_all(const std::vector<xtd::uptr<wait_handle>>& wait_handles, int32 milliseconds_timeout);
261 static bool wait_all(const std::vector<xtd::uptr<wait_handle>>& wait_handles, const time_span& timeout);
262 static bool wait_all(const std::vector<wait_handle*>& wait_handles, int32 milliseconds_timeout);
263
264 template <typename... items_t>
265 static size_t wait_any(items_t... items) {return wait_any(timeout::infinite, items...);}
266 template <typename... items_t>
267 static size_t wait_any(const time_span& timeout, items_t... items) {return wait_any(as<int32>(timeout.total_milliseconds()), items...);}
268 template <typename... items_t>
269 static size_t wait_any(int32 milliseconds_timeout, items_t... items) {
270 std::vector<wait_handle*> wait_handle_pointers;
271 fill_wait_handle_pointers(wait_handle_pointers, items...);
272 return wait_any(wait_handle_pointers, milliseconds_timeout);
273 }
274 template<typename item_t>
275 static size_t wait_any(const std::initializer_list<item_t>& wait_handles) {return wait_any(wait_handles, timeout::infinite);}
276 template<typename item_t>
277 static size_t wait_any(const std::initializer_list<item_t>& wait_handles, int32 milliseconds_timeout) {
278 std::vector<wait_handle*> wait_handle_pointers;
279 for (auto& item : wait_handles)
280 wait_handle_pointers.push_back(const_cast<wait_handle*>(as<wait_handle>(&item)));
281 return wait_any(wait_handle_pointers, milliseconds_timeout);
282 }
283 template<typename item_t>
284 static size_t wait_any(const std::initializer_list<item_t>& wait_handles, const time_span& timeout) {return wait_any(wait_handles, as<int32>(timeout.total_milliseconds_duration().count()));}
285 static size_t wait_any(const std::initializer_list<xtd::sptr<wait_handle>>& wait_handles);
286 static size_t wait_any(const std::initializer_list<xtd::sptr<wait_handle>>& wait_handles, int32 milliseconds_timeout);
287 static size_t wait_any(const std::initializer_list<xtd::sptr<wait_handle>>& wait_handles, const time_span& timeout);
288 static size_t wait_any(const std::initializer_list<xtd::uptr<wait_handle>>& wait_handles);
289 static size_t wait_any(const std::initializer_list<xtd::uptr<wait_handle>>& wait_handles, int32 milliseconds_timeout);
290 static size_t wait_any(const std::initializer_list<xtd::uptr<wait_handle>>& wait_handles, const time_span& timeout);
291 static size_t wait_any(const std::vector<xtd::sptr<wait_handle>>& wait_handles);
292 static size_t wait_any(const std::vector<xtd::sptr<wait_handle>>& wait_handles, int32 milliseconds_timeout);
293 static size_t wait_any(const std::vector<xtd::sptr<wait_handle>>& wait_handles, const time_span& timeout);
294 static size_t wait_any(const std::vector<xtd::uptr<wait_handle>>& wait_handles);
295 static size_t wait_any(const std::vector<xtd::uptr<wait_handle>>& wait_handles, int32 milliseconds_timeout);
296 static size_t wait_any(const std::vector<xtd::uptr<wait_handle>>& wait_handles, const time_span& timeout);
297 static size_t wait_any(const std::vector<wait_handle*>& wait_handles, int32 milliseconds_timeout);
299
300 protected:
302
307 virtual bool signal() = 0;
308
315 virtual bool wait(int32 milliseconds_timeout) = 0;
317
318 private:
319 template <typename item_t, typename... items_t>
320 static void fill_wait_handle_pointers(std::vector<wait_handle*>& wait_handle_pointers, item_t& first, items_t&... rest) {
321 wait_handle_pointers.push_back(const_cast<wait_handle*>(as<wait_handle>(&first)));
322 fill_wait_handle_pointers(wait_handle_pointers, rest...);
323 }
324 template <typename item_t>
325 static void fill_wait_handle_pointers(std::vector<wait_handle*>& wait_handle_pointers, item_t& item) {
326 wait_handle_pointers.push_back(const_cast<wait_handle*>(as<wait_handle>(&item)));
327 }
328 };
329 }
330}
A synchronization primitive that can also be used for interprocess synchronization.
Definition registered_wait_handle.hpp:29
Creates and controls a thread, sets its priority, and gets its status.
Definition thread.hpp:43
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.hpp:33
Encapsulates operating system specific objects that wait for exclusive access to shared resources.
Definition wait_handle.hpp:52
static const intptr invalid_handle
Represents an invalid native operating system handle. This field is read-only.
Definition wait_handle.hpp:63
static bool wait_all(const collection_t &wait_handles)
Waits for all the elements in the specified collection to receive a signal.
Definition wait_handle.hpp:161
static size_t wait_any(const collection_t &wait_handles, int32 milliseconds_timeout)
Waits for any of the elements in the specified collection to receive a signal, using a 32-bit signed ...
Definition wait_handle.hpp:209
wait_handle()=default
Initializes a new instance of the xtd::threading::wait_handle class.
static bool wait_all(const collection_t &wait_handles, int32 milliseconds_timeout)
Waits for all the elements in the specified collection to receive a signal, using an int32 value to m...
Definition wait_handle.hpp:172
virtual bool wait(int32 milliseconds_timeout)=0
wait ownership of the specified mutex object.
virtual intptr handle() const noexcept=0
Gets the native operating system handle.
virtual bool signal()=0
Releases ownership of the specified wait_handle object.
static bool wait_all(const collection_t &wait_handles, const time_span &timeout)
Waits for all the elements in the specified collection to receive a signal, using a xtd::time_span va...
Definition wait_handle.hpp:188
static size_t wait_any(const collection_t &wait_handles)
Waits for any of the elements in the specified collection to receive a signal.
Definition wait_handle.hpp:198
static size_t wait_any(const collection_t &wait_handles, const time_span &timeout)
Waits for any of the elements in the specified collection to receive a signal, using a xtd::time_span...
Definition wait_handle.hpp:225
#define abstract_
This keyword is used to represents an abstract class.
Definition abstract.hpp:25
#define core_export_
Define shared library export.
Definition core_export.hpp:13
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
std::unique_ptr< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
intmax_t intptr
Represent a pointer or a handle.
Definition intptr.hpp:23
std::shared_ptr< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:57
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Represents a time interval.
Definition time_span.hpp:29
double total_milliseconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
Contains xtd::threading::timeout class.