xtd 0.2.0
monitor.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "timeout.hpp"
6#include "../diagnostics/stack_frame.hpp"
7#include "../core_export.hpp"
8#include "../invalid_operation_exception.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_guard;
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 void enter(const object_t& obj) {
146 auto lock_taken = false;
147 enter_ptr(get_ptr(obj), lock_taken);
148 }
149
151 template<class type_t>
152 static void enter(const type_t* str) {enter(string(str));}
154
160 template<class object_t>
161 static void enter(const object_t& obj, bool& lock_taken) {
162 enter_ptr(get_ptr(obj), lock_taken);
163 }
164
166 template<class type_t>
167 static void enter(const type_t* str, bool& lock_taken) {enter(string(str), lock_taken);}
169
174 template<class object_t>
175 static void exit(const object_t& obj) {
176 exit_ptr(get_ptr(obj));
177 }
178
180 template<class type_t>
181 static void exit(const type_t* str) {exit(string(str));}
183
189 template<class object_t>
190 static bool is_entered(const object_t& obj) {
191 return is_entered_ptr(get_ptr(obj));
192 }
193
195 template<class type_t>
196 static bool is_entered(const type_t* str) {return is_entered(string(str));}
198
208 template<class object_t>
209 static void pulse(const object_t& obj) {
210 pulse_ptr(get_ptr(obj));
211 }
212
214 template<class type_t>
215 static void pulse(const type_t* str) {pulse(string(str));}
217
226 template<class object_t>
227 static void pulse_all(const object_t& obj) {
228 pulse_all_ptr(get_ptr(obj));
229 }
230
232 template<class type_t>
233 static void pulse_all(const type_t* str) {pulse_all(string(str));}
235
241 template<class object_t>
242 static bool try_enter(const object_t& obj) noexcept {
243 auto lock_taken = false;
244 return try_enter_ptr(get_ptr(obj), timeout::infinite, lock_taken);
245 }
246
248 template<class type_t>
249 static bool try_enter(const type_t* str) {return try_enter(string(str));}
251
259 template<class object_t>
260 static bool try_enter(const object_t& obj, bool& lock_taken) noexcept {
261 return try_enter_ptr(get_ptr(obj), timeout::infinite, lock_taken);
262 }
263
265 template<class type_t>
266 static bool try_enter(const type_t* str, bool& lock_taken) {return try_enter(string(str), lock_taken);}
268
274 template<class object_t>
275 static bool try_enter(const object_t& obj, int32 milliseconds_timeout) noexcept {
276 auto lock_taken = false;
277 return try_enter_ptr(get_ptr(obj), milliseconds_timeout, lock_taken);
278 }
279
281 template<class type_t>
282 static bool try_enter(const type_t* str, int32 milliseconds_timeout) {return try_enter(string(str), milliseconds_timeout);}
284
292 template<class object_t>
293 static bool try_enter(const object_t& obj, int32 milliseconds_timeout, bool& lock_taken) noexcept {
294 return try_enter_ptr(get_ptr(obj), milliseconds_timeout, lock_taken);
295 }
296
298 template<class type_t>
299 static bool try_enter(const type_t* str, int32 milliseconds_timeout, bool& lock_taken) {return try_enter(string(str), milliseconds_timeout, lock_taken);}
301
307 template<class object_t>
308 static bool try_enter(const object_t& obj, int64 milliseconds_timeout) noexcept {
309 auto lock_taken = false;
310 return try_enter_ptr(get_ptr(obj), static_cast<int32>(milliseconds_timeout), lock_taken);
311 }
312
314 template<class type_t>
315 static bool try_enter(const type_t* str, int64 milliseconds_timeout) {return try_enter(string(str), milliseconds_timeout);}
317
325 template<class object_t>
326 static bool try_enter(const object_t& obj, int64 milliseconds_timeout, bool& lock_taken) noexcept {
327 return try_enter_ptr(get_ptr(obj), static_cast<int32>(milliseconds_timeout), lock_taken);
328 }
329
331 template<class type_t>
332 static bool try_enter(const type_t* str, int64 milliseconds_timeout, bool& lock_taken) {return try_enter(string(str), milliseconds_timeout, lock_taken);}
334
340 template<class object_t>
341 static bool try_enter(const object_t& obj, const time_span& timeout) noexcept {
342 auto lock_taken = false;
343 return try_enter_ptr(get_ptr(obj), timeout.total_milliseconds_duration().count(), lock_taken);
344 }
345
347 template<class type_t>
348 static bool try_enter(const type_t* str, const time_span& timeout) {return try_enter(string(str), timeout);}
350
358 template<class object_t>
359 static bool try_enter(const object_t& obj, const time_span& timeout, bool& lock_taken) noexcept {
360 return try_enter_ptr(get_ptr(obj), timeout.total_milliseconds_duration().count(), lock_taken);
361 }
362
377 template<class object_t>
378 static bool wait(const object_t& obj, int32 milliseconds_timeout) {
379 return wait_ptr(get_ptr(obj), milliseconds_timeout);
380 }
381
396 template<class object_t>
397 static bool wait(const object_t& obj, const time_span& timeout) {
398 return wait_ptr(get_ptr(obj), as<int32>(timeout.total_milliseconds()));
399 }
400
414 template<class object_t>
415 static bool wait(const object_t& obj) {
416 return wait_ptr(get_ptr(obj), timeout::infinite);
417 }
419
421 template<class type_t>
422 static bool try_enter(const type_t* str, const time_span& timeout, bool& lock_taken) {return try_enter(string(str), timeout, lock_taken);}
424
425 private:
426 friend class xtd::threading::lock_guard;
427
428 static static_data& get_static_data();
429
430 template<class object_t>
431 static object_ptr get_ptr(const object_t& obj) noexcept {
432 bool is_string = is<string>(obj);
433 // The newly created string will be deleted when the exit method is called, or if the lock has already been entered.
434 return std::make_pair(is_string ? get_string_ptr(*(new string(as<string>(obj)))) : reinterpret_cast<intptr>(&obj), is_string);
435 }
436
437 template<class type_t>
438 static object_ptr get_ptr(const type_t* str) {return get_ptr(string(str));}
439
440 static void enter_ptr(object_ptr obj);
441 static void enter_ptr(object_ptr obj, bool& lock_taken);
442 static void exit_ptr(object_ptr obj);
443 static intptr get_string_ptr(const string& str);
444 static bool is_entered_ptr(object_ptr obj) noexcept;
445 static void pulse_ptr(object_ptr obj);
446 static void pulse_all_ptr(object_ptr obj);
447 static bool try_enter_ptr(object_ptr obj, int32 milliseconds_timeout, bool& lock_taken) noexcept;
448 static bool wait_ptr(object_ptr obj, int32 milliseconds_timeout);
449 };
450 }
451}
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock_guard.hpp:32
Provides a mechanism that synchronizes access to objects.
Definition monitor.hpp:129
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.hpp:209
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.hpp:326
static bool try_enter(const object_t &obj) noexcept
Attempts to acquire an exclusive lock on the specified object.
Definition monitor.hpp:242
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.hpp:341
static void pulse_all(const object_t &obj)
Notifies all waiting threads of a change in the object's state.
Definition monitor.hpp:227
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.hpp:415
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.hpp:397
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.hpp:293
static void exit(const object_t &obj)
Releases an exclusive lock on the specified obj.
Definition monitor.hpp:175
static bool is_entered(const object_t &obj)
Determines whether the current thread holds the lock on the specified object.
Definition monitor.hpp:190
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.hpp:275
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.hpp:359
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.hpp:378
static void enter(const object_t &obj)
Acquires an exclusive lock on the specified obj.
Definition monitor.hpp:145
static void enter(const object_t &obj, bool &lock_taken)
Acquires an exclusive lock on the specified obj.
Definition monitor.hpp:161
static bool try_enter(const object_t &obj, bool &lock_taken) noexcept
Attempts to acquire an exclusive lock on the specified object.
Definition monitor.hpp:260
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.hpp:308
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.hpp:33
Represents a time interval.
Definition time_span.hpp:29
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:37
#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
int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
intmax_t intptr
Represent a pointer or a handle.
Definition intptr.hpp:23
@ enter
The ENTER key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::threading::timeout class.