xtd 0.2.0
monitor.h
Go to the documentation of this file.
1 #pragma once
5 #include "timeout.h"
6 #include "../diagnostics/stack_frame.h"
7 #include "../core_export.h"
8 #include "../invalid_operation_exception.h"
9 #include "../static.h"
10 #include "../time_span.h"
11 #include "../types.h"
12 #include "../as.h"
13 #include "../is.h"
14 #include <unordered_map>
15 #include <utility>
16 
18 namespace xtd {
20  namespace threading {
22  class lock_guard;
24 
127  class core_export_ monitor static_ {
128  class condition_variable;
129  class critical_section;
130  struct item;
131  using item_collection = std::unordered_map<intptr, item>;
132  using object_ptr = std::pair<intptr, bool>;
133  struct static_data;
134 
135  public:
137 
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);
146  }
147 
149  template<typename type_t>
150  static void enter(const type_t* str) {enter(ustring(str));}
152 
158  template<typename object_t>
159  static void enter(const object_t& obj, bool& lock_taken) {
160  enter_ptr(get_ptr(obj), lock_taken);
161  }
162 
164  template<typename type_t>
165  static void enter(const type_t* str, bool& lock_taken) {enter(ustring(str), lock_taken);}
167 
172  template<typename object_t>
173  static void exit(const object_t& obj) {
174  exit_ptr(get_ptr(obj));
175  }
176 
178  template<typename type_t>
179  static void exit(const type_t* str) {exit(ustring(str));}
181 
187  template<typename object_t>
188  static bool is_entered(const object_t& obj) {
189  return is_entered_ptr(get_ptr(obj));
190  }
191 
193  template<typename type_t>
194  static bool is_entered(const type_t* str) {return is_entered(ustring(str));}
196 
206  template<typename object_t>
207  static void pulse(const object_t& obj) {
208  pulse_ptr(get_ptr(obj));
209  }
210 
212  template<typename type_t>
213  static void pulse(const type_t* str) {pulse(ustring(str));}
215 
224  template<typename object_t>
225  static void pulse_all(const object_t& obj) {
226  pulse_all_ptr(get_ptr(obj));
227  }
228 
230  template<typename type_t>
231  static void pulse_all(const type_t* str) {pulse_all(ustring(str));}
233 
239  template<typename object_t>
240  static bool try_enter(const object_t& obj) noexcept {
241  auto lock_taken = false;
242  return try_enter_ptr(get_ptr(obj), timeout::infinite, lock_taken);
243  }
244 
246  template<typename type_t>
247  static bool try_enter(const type_t* str) {return try_enter(ustring(str));}
249 
257  template<typename object_t>
258  static bool try_enter(const object_t& obj, bool& lock_taken) noexcept {
259  return try_enter_ptr(get_ptr(obj), timeout::infinite, lock_taken);
260  }
261 
263  template<typename type_t>
264  static bool try_enter(const type_t* str, bool& lock_taken) {return try_enter(ustring(str), lock_taken);}
266 
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);
276  }
277 
279  template<typename type_t>
280  static bool try_enter(const type_t* str, int32 milliseconds_timeout) {return try_enter(ustring(str), milliseconds_timeout);}
282 
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);
293  }
294 
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);}
299 
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);
309  }
310 
312  template<typename type_t>
313  static bool try_enter(const type_t* str, int64 milliseconds_timeout) {return try_enter(ustring(str), milliseconds_timeout);}
315 
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);
326  }
327 
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);}
332 
338  template<typename object_t>
339  static bool try_enter(const object_t& obj, const time_span& timeout) noexcept {
340  auto lock_taken = false;
341  return try_enter_ptr(get_ptr(obj), timeout.total_milliseconds_duration().count(), lock_taken);
342  }
343 
345  template<typename type_t>
346  static bool try_enter(const type_t* str, const time_span& timeout) {return try_enter(ustring(str), timeout);}
348 
356  template<typename object_t>
357  static bool try_enter(const object_t& obj, const time_span& timeout, bool& lock_taken) noexcept {
358  return try_enter_ptr(get_ptr(obj), timeout.total_milliseconds_duration().count(), lock_taken);
359  }
360 
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);
378  }
379 
394  template<typename object_t>
395  static bool wait(const object_t& obj, const time_span& timeout) {
396  return wait_ptr(get_ptr(obj), as<int32>(timeout.total_milliseconds()));
397  }
398 
412  template<typename object_t>
413  static bool wait(const object_t& obj) {
414  return wait_ptr(get_ptr(obj), timeout::infinite);
415  }
417 
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);}
422 
423  private:
424  friend class xtd::threading::lock_guard;
425 
426  static static_data& get_static_data();
427 
428  template<typename object_t>
429  static object_ptr get_ptr(const object_t& obj) noexcept {
430  bool is_string = is<ustring>(obj);
431  // The newly created string will be deleted when the exit method is called, or if the lock has already been entered.
432  return std::make_pair(is_string ? get_ustring_ptr(*(new ustring(as<ustring>(obj)))) : reinterpret_cast<intptr>(&obj), is_string);
433  }
434 
435  template<typename type_t>
436  static object_ptr get_ptr(const type_t* str) {return get_ptr(ustring(str));}
437 
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);
441  static intptr get_ustring_ptr(const ustring& str);
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);
447  };
448  }
449 }
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&#39;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&#39;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&#39;s state.
Definition: monitor.h:225