xtd 0.2.0
Loading...
Searching...
No Matches
monitor.h
Go to the documentation of this file.
1
4#pragma once
5#include "timeout.h"
6#include "../collections/generic/key_value_pair.h"
7#include "../diagnostics/stack_frame.h"
8#include "../core_export.h"
9#include "../invalid_operation_exception.h"
10#include "../static.h"
11#include "../time_span.h"
12#include "../types.h"
13#include "../as.h"
14#include "../is.h"
15#include <unordered_map>
16#include <utility>
17
19namespace xtd {
21 namespace threading {
23 class lock_guard;
25
131 class condition_variable;
132 class critical_section;
133 struct item;
134 using item_collection = std::unordered_map<intptr, item>;
136 struct static_data;
137
138 public:
140
145 template<typename object_t>
146 static void enter(const object_t& obj) {
147 auto lock_taken = false;
148 enter_ptr(get_ptr(obj), lock_taken);
149 }
150
152 template<typename type_t>
153 static void enter(const type_t* str) {enter(string(str));}
155
161 template<typename object_t>
162 static void enter(const object_t& obj, bool& lock_taken) {
163 enter_ptr(get_ptr(obj), lock_taken);
164 }
165
167 template<typename type_t>
168 static void enter(const type_t* str, bool& lock_taken) {enter(string(str), lock_taken);}
170
175 template<typename object_t>
176 static void exit(const object_t& obj) {
177 exit_ptr(get_ptr(obj));
178 }
179
181 template<typename type_t>
182 static void exit(const type_t* str) {exit(string(str));}
184
190 template<typename object_t>
191 static bool is_entered(const object_t& obj) {
192 return is_entered_ptr(get_ptr(obj));
193 }
194
196 template<typename type_t>
197 static bool is_entered(const type_t* str) {return is_entered(string(str));}
199
209 template<typename object_t>
210 static void pulse(const object_t& obj) {
211 pulse_ptr(get_ptr(obj));
212 }
213
215 template<typename type_t>
216 static void pulse(const type_t* str) {pulse(string(str));}
218
227 template<typename object_t>
228 static void pulse_all(const object_t& obj) {
229 pulse_all_ptr(get_ptr(obj));
230 }
231
233 template<typename type_t>
234 static void pulse_all(const type_t* str) {pulse_all(string(str));}
236
242 template<typename object_t>
243 static bool try_enter(const object_t& obj) noexcept {
244 auto lock_taken = false;
245 return try_enter_ptr(get_ptr(obj), timeout::infinite, lock_taken);
246 }
247
249 template<typename type_t>
250 static bool try_enter(const type_t* str) {return try_enter(string(str));}
252
260 template<typename object_t>
261 static bool try_enter(const object_t& obj, bool& lock_taken) noexcept {
262 return try_enter_ptr(get_ptr(obj), timeout::infinite, lock_taken);
263 }
264
266 template<typename type_t>
267 static bool try_enter(const type_t* str, bool& lock_taken) {return try_enter(string(str), lock_taken);}
269
275 template<typename object_t>
276 static bool try_enter(const object_t& obj, int32 milliseconds_timeout) noexcept {
277 auto lock_taken = false;
278 return try_enter_ptr(get_ptr(obj), milliseconds_timeout, lock_taken);
279 }
280
282 template<typename type_t>
283 static bool try_enter(const type_t* str, int32 milliseconds_timeout) {return try_enter(string(str), milliseconds_timeout);}
285
293 template<typename object_t>
294 static bool try_enter(const object_t& obj, int32 milliseconds_timeout, bool& lock_taken) noexcept {
295 return try_enter_ptr(get_ptr(obj), milliseconds_timeout, lock_taken);
296 }
297
299 template<typename type_t>
300 static bool try_enter(const type_t* str, int32 milliseconds_timeout, bool& lock_taken) {return try_enter(string(str), milliseconds_timeout, lock_taken);}
302
308 template<typename object_t>
309 static bool try_enter(const object_t& obj, int64 milliseconds_timeout) noexcept {
310 auto lock_taken = false;
311 return try_enter_ptr(get_ptr(obj), static_cast<int32>(milliseconds_timeout), lock_taken);
312 }
313
315 template<typename type_t>
316 static bool try_enter(const type_t* str, int64 milliseconds_timeout) {return try_enter(string(str), milliseconds_timeout);}
318
326 template<typename object_t>
327 static bool try_enter(const object_t& obj, int64 milliseconds_timeout, bool& lock_taken) noexcept {
328 return try_enter_ptr(get_ptr(obj), static_cast<int32>(milliseconds_timeout), lock_taken);
329 }
330
332 template<typename type_t>
333 static bool try_enter(const type_t* str, int64 milliseconds_timeout, bool& lock_taken) {return try_enter(string(str), milliseconds_timeout, lock_taken);}
335
341 template<typename object_t>
342 static bool try_enter(const object_t& obj, const time_span& timeout) noexcept {
343 auto lock_taken = false;
344 return try_enter_ptr(get_ptr(obj), timeout.total_milliseconds_duration().count(), lock_taken);
345 }
346
348 template<typename type_t>
349 static bool try_enter(const type_t* str, const time_span& timeout) {return try_enter(string(str), timeout);}
351
359 template<typename object_t>
360 static bool try_enter(const object_t& obj, const time_span& timeout, bool& lock_taken) noexcept {
361 return try_enter_ptr(get_ptr(obj), timeout.total_milliseconds_duration().count(), lock_taken);
362 }
363
378 template<typename object_t>
379 static bool wait(const object_t& obj, int32 milliseconds_timeout) {
380 return wait_ptr(get_ptr(obj), milliseconds_timeout);
381 }
382
397 template<typename object_t>
398 static bool wait(const object_t& obj, const time_span& timeout) {
399 return wait_ptr(get_ptr(obj), as<int32>(timeout.total_milliseconds()));
400 }
401
415 template<typename object_t>
416 static bool wait(const object_t& obj) {
417 return wait_ptr(get_ptr(obj), timeout::infinite);
418 }
420
422 template<typename type_t>
423 static bool try_enter(const type_t* str, const time_span& timeout, bool& lock_taken) {return try_enter(string(str), timeout, lock_taken);}
425
426 private:
427 friend class xtd::threading::lock_guard;
428
429 static static_data& get_static_data();
430
431 template<typename object_t>
432 static object_ptr get_ptr(const object_t& obj) noexcept {
433 bool is_string = is<string>(obj);
434 // The newly created string will be deleted when the exit method is called, or if the lock has already been entered.
435 return std::make_pair(is_string ? get_ustring_ptr(*(new string(as<string>(obj)))) : reinterpret_cast<intptr>(&obj), is_string);
436 }
437
438 template<typename type_t>
439 static object_ptr get_ptr(const type_t* str) {return get_ptr(string(str));}
440
441 static void enter_ptr(object_ptr obj);
442 static void enter_ptr(object_ptr obj, bool& lock_taken);
443 static void exit_ptr(object_ptr obj);
444 static intptr get_ustring_ptr(const string& str);
445 static bool is_entered_ptr(object_ptr obj) noexcept;
446 static void pulse_ptr(object_ptr obj);
447 static void pulse_all_ptr(object_ptr obj);
448 static bool try_enter_ptr(object_ptr obj, int32 milliseconds_timeout, bool& lock_taken) noexcept;
449 static bool wait_ptr(object_ptr obj, int32 milliseconds_timeout);
450 };
451 }
452}
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock_guard.h:32
Provides a mechanism that synchronizes access to objects.
Definition monitor.h:130
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:210
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:327
static bool try_enter(const object_t &obj) noexcept
Attempts to acquire an exclusive lock on the specified object.
Definition monitor.h:243
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:342
static void pulse_all(const object_t &obj)
Notifies all waiting threads of a change in the object's state.
Definition monitor.h:228
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:416
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:398
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:294
static void exit(const object_t &obj)
Releases an exclusive lock on the specified obj.
Definition monitor.h:176
static bool is_entered(const object_t &obj)
Determines whether the current thread holds the lock on the specified object.
Definition monitor.h:191
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:276
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:360
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:379
static void enter(const object_t &obj)
Acquires an exclusive lock on the specified obj.
Definition monitor.h:146
static void enter(const object_t &obj, bool &lock_taken)
Acquires an exclusive lock on the specified obj.
Definition monitor.h:162
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:261
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:309
Contains a constant used to specify an infinite amount of time. This class cannot be inherited.
Definition timeout.h:33
std::pair< key_t, value_t > key_value_pair
Defines a key/value pair that can be set or retrieved.
Definition key_value_pair.h:29
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.h:37
#define core_export_
Define shared library export.
Definition core_export.h:13
int32_t int32
Represents a 32-bit signed integer.
Definition int32.h:23
int64_t int64
Represents a 64-bit signed integer.
Definition int64.h:23
intmax_t intptr
Represent a pointer or a handle.
Definition intptr.h:23
@ enter
The ENTER key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Represents a time interval.
Definition time_span.h:29
Contains xtd::threading::timeout class.