xtd 0.2.0
Loading...
Searching...
No Matches
monitor.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "timeout.hpp"
7#include "../core_export.hpp"
9#include "../static.hpp"
10#include "../time_span.hpp"
11#include "../types.hpp"
12#include "../as.hpp"
13#include "../is.hpp"
14#include <unordered_map>
15#include <utility>
16
18namespace xtd {
20 namespace threading {
22 class lock;
24
130 class condition_variable;
131 class critical_section;
132 struct item;
133 using item_collection = std::unordered_map<intptr, item>;
134 using object_ptr = std::pair<intptr, bool>;
135 struct static_data;
136
137 public:
139
144 template<class object_t>
145 static auto enter(const object_t& obj) -> void {
146 auto lock_taken = false;
147 enter_ptr(get_ptr(obj), lock_taken);
148 }
149
151 template<class type_t>
152 static auto enter(const type_t* str) -> void {enter(string(str));}
154
160 template<class object_t>
161 static auto enter(const object_t& obj, bool& lock_taken) -> void {enter_ptr(get_ptr(obj), lock_taken);}
162
164 template<class type_t>
165 static auto enter(const type_t* str, bool& lock_taken) -> void {enter(string(str), lock_taken);}
167
172 template<class object_t>
173 static auto exit(const object_t& obj) -> void {exit_ptr(get_ptr(obj));}
174
176 template<class type_t>
177 static auto exit(const type_t* str) -> void {exit(string(str));}
179
185 template<class object_t>
186 static auto is_entered(const object_t& obj) -> bool {return is_entered_ptr(get_ptr(obj));}
187
189 template<class type_t>
190 static auto is_entered(const type_t* str) -> bool {return is_entered(string(str));}
192
202 template<class object_t>
203 static auto pulse(const object_t& obj) -> void {pulse_ptr(get_ptr(obj));}
204
206 template<class type_t>
207 static auto pulse(const type_t* str) -> void {pulse(string(str));}
209
218 template<class object_t>
219 static auto pulse_all(const object_t& obj) -> void {pulse_all_ptr(get_ptr(obj));}
220
222 template<class type_t>
223 static auto pulse_all(const type_t* str) -> void {pulse_all(string(str));}
225
231 template<class object_t>
232 static auto try_enter(const object_t& obj) noexcept -> bool {
233 auto lock_taken = false;
234 return try_enter_ptr(get_ptr(obj), timeout::infinite, lock_taken);
235 }
236
238 template<class type_t>
239 static auto try_enter(const type_t* str) -> bool {return try_enter(string(str));}
241
249 template<class object_t>
250 [[nodiscard]] static auto try_enter(const object_t& obj, bool& lock_taken) noexcept -> bool {return try_enter_ptr(get_ptr(obj), timeout::infinite, lock_taken);}
251
253 template<class type_t>
254 [[nodiscard]] static auto try_enter(const type_t* str, bool& lock_taken) -> bool {return try_enter(string(str), lock_taken);}
256
262 template<class object_t>
263 [[nodiscard]] static auto try_enter(const object_t& obj, int32 milliseconds_timeout) noexcept -> bool {
264 auto lock_taken = false;
265 return try_enter_ptr(get_ptr(obj), milliseconds_timeout, lock_taken);
266 }
267
269 template<class type_t>
270 [[nodiscard]] static auto try_enter(const type_t* str, int32 milliseconds_timeout) -> bool {return try_enter(string(str), milliseconds_timeout);}
272
280 template<class object_t>
281 [[nodiscard]] static auto try_enter(const object_t& obj, int32 milliseconds_timeout, bool& lock_taken) noexcept -> bool {return try_enter_ptr(get_ptr(obj), milliseconds_timeout, lock_taken);}
282
284 template<class type_t>
285 [[nodiscard]] static auto try_enter(const type_t* str, int32 milliseconds_timeout, bool& lock_taken) -> bool {return try_enter(string(str), milliseconds_timeout, lock_taken);}
287
293 template<class object_t>
294 [[nodiscard]] static auto try_enter(const object_t& obj, int64 milliseconds_timeout) noexcept -> bool {
295 auto lock_taken = false;
296 return try_enter_ptr(get_ptr(obj), static_cast<int32>(milliseconds_timeout), lock_taken);
297 }
298
300 template<class type_t>
301 [[nodiscard]] static auto try_enter(const type_t* str, int64 milliseconds_timeout) -> bool {return try_enter(string(str), milliseconds_timeout);}
303
311 template<class object_t>
312 [[nodiscard]] static auto try_enter(const object_t& obj, int64 milliseconds_timeout, bool& lock_taken) noexcept -> bool {return try_enter_ptr(get_ptr(obj), static_cast<int32>(milliseconds_timeout), lock_taken);}
313
315 template<class type_t>
316 [[nodiscard]] static bool try_enter(const type_t* str, int64 milliseconds_timeout, bool& lock_taken) {return try_enter(string(str), milliseconds_timeout, lock_taken);}
318
324 template<class object_t>
325 [[nodiscard]] static auto try_enter(const object_t& obj, const time_span& timeout) noexcept -> bool {
326 auto lock_taken = false;
327 return try_enter_ptr(get_ptr(obj), timeout.total_milliseconds_duration().count(), lock_taken);
328 }
329
331 template<class type_t>
332 [[nodiscard]] static auto try_enter(const type_t* str, const time_span& timeout) -> bool {return try_enter(string(str), timeout);}
334
342 template<class object_t>
343 [[nodiscard]] static auto try_enter(const object_t& obj, const time_span& timeout, bool& lock_taken) noexcept -> bool {return try_enter_ptr(get_ptr(obj), timeout.total_milliseconds_duration().count(), lock_taken);}
344
359 template<class object_t>
360 static auto wait(const object_t& obj, int32 milliseconds_timeout) -> bool {return wait_ptr(get_ptr(obj), milliseconds_timeout);}
361
376 template<class object_t>
377 static auto wait(const object_t& obj, const time_span& timeout) -> bool {return wait_ptr(get_ptr(obj), as<int32>(timeout.total_milliseconds()));}
378
392 template<class object_t>
393 static auto wait(const object_t& obj) -> bool {return wait_ptr(get_ptr(obj), timeout::infinite);}
395
397 template<class type_t>
398 [[nodiscard]] static auto try_enter(const type_t* str, const time_span& timeout, bool& lock_taken) -> bool {return try_enter(string(str), timeout, lock_taken);}
400
401 private:
402 friend class xtd::threading::lock;
403
404 static auto get_static_data() -> static_data&;
405
406 template<class object_t>
407 [[nodiscard]] static auto get_ptr(const object_t& obj) noexcept -> object_ptr {
408 bool is_string = is<string>(obj);
409 // The newly created string will be deleted when the exit method is called, or if the lock has already been entered.
410 return std::make_pair(is_string ? get_string_ptr(*(new string(as<string>(obj)))) : reinterpret_cast<intptr>(&obj), is_string);
411 }
412
413 template<class type_t>
414 [[nodiscard]] static auto get_ptr(const type_t* str) -> object_ptr {return get_ptr(string(str));}
415
416 static auto enter_ptr(object_ptr obj) -> void;
417 static auto enter_ptr(object_ptr obj, bool& lock_taken) -> void;
418 static auto exit_ptr(object_ptr obj) -> void;
419 [[nodiscard]] static auto get_string_ptr(const string& str) -> intptr;
420 static auto is_entered_ptr(object_ptr obj) noexcept -> bool;
421 static auto pulse_ptr(object_ptr obj) -> void;
422 static auto pulse_all_ptr(object_ptr obj) -> void;
423 static auto try_enter_ptr(object_ptr obj, int32 milliseconds_timeout, bool& lock_taken) noexcept -> bool;
424 static auto wait_ptr(object_ptr obj, int32 milliseconds_timeout) -> bool;
425 };
426 }
427}
Contains xtd::as method.
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock.hpp:32
Provides a mechanism that synchronizes access to objects.
Definition monitor.hpp:129
static auto try_enter(const object_t &obj, int64 milliseconds_timeout, bool &lock_taken) noexcept -> bool
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition monitor.hpp:312
static auto enter(const object_t &obj, bool &lock_taken) -> void
Acquires an exclusive lock on the specified obj.
Definition monitor.hpp:161
static auto is_entered(const object_t &obj) -> bool
Determines whether the current thread holds the lock on the specified object.
Definition monitor.hpp:186
static auto enter(const object_t &obj) -> void
Acquires an exclusive lock on the specified obj.
Definition monitor.hpp:145
static auto exit(const object_t &obj) -> void
Releases an exclusive lock on the specified obj.
Definition monitor.hpp:173
static auto try_enter(const object_t &obj, const time_span &timeout, bool &lock_taken) noexcept -> bool
Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object.
Definition monitor.hpp:343
static auto try_enter(const object_t &obj, int32 milliseconds_timeout) noexcept -> bool
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition monitor.hpp:263
static auto try_enter(const object_t &obj, bool &lock_taken) noexcept -> bool
Attempts to acquire an exclusive lock on the specified object.
Definition monitor.hpp:250
static auto try_enter(const object_t &obj, const time_span &timeout) noexcept -> bool
Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object.
Definition monitor.hpp:325
static auto try_enter(const object_t &obj) noexcept -> bool
Attempts to acquire an exclusive lock on the specified object.
Definition monitor.hpp:232
static auto pulse(const object_t &obj) -> void
Notifies a thread in the waiting queue of a change in the locked object's state.
Definition monitor.hpp:203
static auto wait(const object_t &obj) -> bool
Releases the lock on an object and blocks the current thread until it reacquires the lock.
Definition monitor.hpp:393
static auto pulse_all(const object_t &obj) -> void
Notifies all waiting threads of a change in the object's state.
Definition monitor.hpp:219
static auto try_enter(const object_t &obj, int32 milliseconds_timeout, bool &lock_taken) noexcept -> bool
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition monitor.hpp:281
static auto try_enter(const object_t &obj, int64 milliseconds_timeout) noexcept -> bool
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition monitor.hpp:294
static auto wait(const object_t &obj, int32 milliseconds_timeout) -> bool
Releases the lock on an object and blocks the current thread until it reacquires the lock....
Definition monitor.hpp:360
static auto wait(const object_t &obj, const time_span &timeout) -> bool
Releases the lock on an object and blocks the current thread until it reacquires the lock....
Definition monitor.hpp:377
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.hpp:33
static constexpr int32 infinite
A constant used to specify an infinite waiting period. This field is constant.
Definition timeout.hpp:41
Represents a time interval.
Definition time_span.hpp:29
Contains core_export_ keyword.
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:38
#define core_export_
Define shared library export.
Definition core_export.hpp:13
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::intmax_t intptr
Represent a pointer or a handle.
Definition intptr.hpp:23
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
bool is(xtd::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:484
@ enter
The ENTER key.
Definition console_key.hpp:28
Contains xtd::invalid_operation_exception exception.
Contains xtd::is method.
The xtd::threading namespace provides classes and interfaces that enable multithreaded programming....
Definition abandoned_mutex_exception.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::diagnostics::stack_frame class.
Contains xtd::static_object class.
Contains xtd::time_span class.
Contains xtd::threading::timeout class.
Contains xtd fundamental types.