6 #include "../diagnostics/stack_frame.h" 7 #include "../core_export.h" 8 #include "../invalid_operation_exception.h" 10 #include "../time_span.h" 14 #include <unordered_map> 128 class condition_variable;
129 class critical_section;
131 using item_collection = std::unordered_map<intptr, item>;
132 using object_ptr = std::pair<intptr, bool>;
142 template<
typename object_t>
143 static void enter(
const object_t& obj) {
144 auto lock_taken =
false;
145 enter_ptr(get_ptr(obj), lock_taken);
149 template<
typename type_t>
158 template<
typename object_t>
159 static void enter(
const object_t& obj,
bool& lock_taken) {
160 enter_ptr(get_ptr(obj), lock_taken);
164 template<
typename type_t>
165 static void enter(
const type_t* str,
bool& lock_taken) {
enter(
ustring(str), lock_taken);}
172 template<
typename object_t>
173 static void exit(
const object_t& obj) {
174 exit_ptr(get_ptr(obj));
178 template<
typename type_t>
179 static void exit(
const type_t* str) {exit(
ustring(str));}
187 template<
typename object_t>
189 return is_entered_ptr(get_ptr(obj));
193 template<
typename type_t>
194 static bool is_entered(
const type_t* str) {
return is_entered(
ustring(str));}
206 template<
typename object_t>
207 static void pulse(
const object_t& obj) {
208 pulse_ptr(get_ptr(obj));
212 template<
typename type_t>
213 static void pulse(
const type_t* str) {pulse(
ustring(str));}
224 template<
typename object_t>
226 pulse_all_ptr(get_ptr(obj));
230 template<
typename type_t>
231 static void pulse_all(
const type_t* str) {pulse_all(
ustring(str));}
239 template<
typename object_t>
241 auto lock_taken =
false;
246 template<
typename type_t>
247 static bool try_enter(
const type_t* str) {
return try_enter(
ustring(str));}
257 template<
typename object_t>
258 static bool try_enter(
const object_t& obj,
bool& lock_taken) noexcept {
263 template<
typename type_t>
264 static bool try_enter(
const type_t* str,
bool& lock_taken) {
return try_enter(
ustring(str), lock_taken);}
272 template<
typename object_t>
273 static bool try_enter(
const object_t& obj,
int32 milliseconds_timeout) noexcept {
274 auto lock_taken =
false;
275 return try_enter_ptr(get_ptr(obj), milliseconds_timeout, lock_taken);
279 template<
typename type_t>
280 static bool try_enter(
const type_t* str,
int32 milliseconds_timeout) {
return try_enter(
ustring(str), milliseconds_timeout);}
290 template<
typename object_t>
291 static bool try_enter(
const object_t& obj,
int32 milliseconds_timeout,
bool& lock_taken) noexcept {
292 return try_enter_ptr(get_ptr(obj), milliseconds_timeout, lock_taken);
296 template<
typename type_t>
297 static bool try_enter(
const type_t* str,
int32 milliseconds_timeout,
bool& lock_taken) {
return try_enter(
ustring(str), milliseconds_timeout, lock_taken);}
305 template<
typename object_t>
306 static bool try_enter(
const object_t& obj,
int64 milliseconds_timeout) noexcept {
307 auto lock_taken =
false;
308 return try_enter_ptr(get_ptr(obj), static_cast<int32>(milliseconds_timeout), lock_taken);
312 template<
typename type_t>
313 static bool try_enter(
const type_t* str,
int64 milliseconds_timeout) {
return try_enter(
ustring(str), milliseconds_timeout);}
323 template<
typename object_t>
324 static bool try_enter(
const object_t& obj,
int64 milliseconds_timeout,
bool& lock_taken) noexcept {
325 return try_enter_ptr(get_ptr(obj), static_cast<int32>(milliseconds_timeout), lock_taken);
329 template<
typename type_t>
330 static bool try_enter(
const type_t* str,
int64 milliseconds_timeout,
bool& lock_taken) {
return try_enter(
ustring(str), milliseconds_timeout, lock_taken);}
338 template<
typename object_t>
340 auto lock_taken =
false;
341 return try_enter_ptr(get_ptr(obj),
timeout.total_milliseconds_duration().count(), lock_taken);
345 template<
typename type_t>
356 template<
typename object_t>
375 template<
typename object_t>
376 static bool wait(
const object_t& obj,
int32 milliseconds_timeout) {
377 return wait_ptr(get_ptr(obj), milliseconds_timeout);
394 template<
typename object_t>
412 template<
typename object_t>
413 static bool wait(
const object_t& obj) {
419 template<
typename type_t>
420 static bool try_enter(
const type_t* str,
const time_span& timeout,
bool& lock_taken) {
return try_enter(
ustring(str), timeout, lock_taken);}
426 static static_data& get_static_data();
428 template<
typename object_t>
429 static object_ptr get_ptr(
const object_t& obj) noexcept {
430 bool is_string = is<ustring>(obj);
432 return std::make_pair(is_string ? get_ustring_ptr(*(
new ustring(
as<ustring>(obj)))) : reinterpret_cast<intptr>(&obj), is_string);
435 template<
typename type_t>
436 static object_ptr get_ptr(
const type_t* str) {
return get_ptr(
ustring(str));}
438 static void enter_ptr(object_ptr obj);
439 static void enter_ptr(object_ptr obj,
bool& lock_taken);
440 static void exit_ptr(object_ptr obj);
442 static bool is_entered_ptr(object_ptr obj) noexcept;
443 static void pulse_ptr(object_ptr obj);
444 static void pulse_all_ptr(object_ptr obj);
445 static bool try_enter_ptr(object_ptr obj,
int32 milliseconds_timeout,
bool& lock_taken) noexcept;
446 static bool wait_ptr(object_ptr obj,
int32 milliseconds_timeout);
static bool try_enter(const object_t &obj) noexcept
Attempts to acquire an exclusive lock on the specified object.
Definition: monitor.h:240
static void enter(const object_t &obj, bool &lock_taken)
Acquires an exclusive lock on the specified obj.
Definition: monitor.h:159
intmax_t intptr
Represent a pointer or a handle.
Definition: types.h:151
int32 as< int32 >(std::any value)
Casts a type into another type.
Definition: as.h:9228
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition: static.h:37
static bool try_enter(const object_t &obj, const time_span &timeout, bool &lock_taken) noexcept
Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object...
Definition: monitor.h:357
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition: lock_guard.h:30
Contains xtd::threading::timeout class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
static bool is_entered(const object_t &obj)
Determines whether the current thread holds the lock on the specified object.
Definition: monitor.h:188
static bool try_enter(const object_t &obj, int64 milliseconds_timeout, bool &lock_taken) noexcept
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition: monitor.h:324
std::chrono::milliseconds total_milliseconds_duration() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
static bool wait(const object_t &obj)
Releases the lock on an object and blocks the current thread until it reacquires the lock...
Definition: monitor.h:413
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
static bool try_enter(const object_t &obj, int32 milliseconds_timeout) noexcept
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition: monitor.h:273
static void pulse(const object_t &obj)
Notifies a thread in the waiting queue of a change in the locked object's state.
Definition: monitor.h:207
static constexpr int32 infinite
A constant used to specify an infinite waiting period. This field is constant.
Definition: timeout.h:39
double total_milliseconds() const noexcept
Gets the value of the current xtd::time_span structure expressed in whole and fractional milliseconds...
static void enter(const object_t &obj)
Acquires an exclusive lock on the specified obj.
Definition: monitor.h:143
static bool try_enter(const object_t &obj, int32 milliseconds_timeout, bool &lock_taken) noexcept
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition: monitor.h:291
static bool try_enter(const object_t &obj, int64 milliseconds_timeout) noexcept
Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified obj...
Definition: monitor.h:306
static bool wait(const object_t &obj, const time_span &timeout)
Releases the lock on an object and blocks the current thread until it reacquires the lock...
Definition: monitor.h:395
Represents a time interval.
Definition: time_span.h:26
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
Contains a constant used to specify an infinite amount of time. This class cannot be inherited...
Definition: timeout.h:31
ustring as< ustring >(std::any value)
Casts a type into another type.
Definition: as.h:15388
static void exit(const object_t &obj)
Releases an exclusive lock on the specified obj.
Definition: monitor.h:173
int_least64_t int64
Represents a 64-bit signed integer.
Definition: types.h:140
static bool wait(const object_t &obj, int32 milliseconds_timeout)
Releases the lock on an object and blocks the current thread until it reacquires the lock...
Definition: monitor.h:376
static bool try_enter(const object_t &obj, const time_span &timeout) noexcept
Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object...
Definition: monitor.h:339
static bool try_enter(const object_t &obj, bool &lock_taken) noexcept
Attempts to acquire an exclusive lock on the specified object.
Definition: monitor.h:258
Provides a mechanism that synchronizes access to objects.
Definition: monitor.h:127
static void pulse_all(const object_t &obj)
Notifies all waiting threads of a change in the object's state.
Definition: monitor.h:225