xtd 0.2.0
mutex.h
Go to the documentation of this file.
1 #pragma once
5 #include "wait_handle.h"
6 #include "../date_time.h"
7 #include "../icomparable.h"
8 #include "../iequatable.h"
9 
11 namespace xtd {
13  namespace threading {
15  class thread;
17 
48  class core_export_ mutex : public wait_handle, public icomparable<mutex>, public iequatable<mutex> {
49  class mutex_base;
50  class named_mutex;
51  class unnamed_mutex;
52  public:
54 
59 
61 
65  mutex();
68  explicit mutex(bool initially_owned);
73  explicit mutex(const ustring& name);
79  mutex(const ustring& name, bool& created_new);
84  mutex(bool initially_owned, const ustring& name);
90  mutex(bool initially_owned, const ustring& name, bool& created_new);
92 
94  template <typename char_t>
95  explicit mutex(const char_t* name) : mutex(ustring(name)) {}
96  ~mutex();
98 
100 
102  intptr handle() const noexcept override;
103  void handle(intptr value) override;
104 
107  native_handle_type native_handle() const noexcept;
109 
111 
113  void close() override;
114 
115  int32 compare_to(const mutex& value) const noexcept override;
116 
117  bool equals(const mutex& value) const noexcept override;
118 
123  void lock();
124 
130  static mutex open_existing(const ustring& name);
131 
134  void release_mutex();
135 
142  bool try_lock() noexcept;
143 
153  bool try_lock_for(const time_span& timeout) noexcept;
154 
164  bool try_lock_until(const date_time& timeout_time) noexcept;
165 
170  static bool try_open_existing(const ustring& name, mutex& result) noexcept;
171 
176  void unlock();
178 
179  protected:
181 
183  bool signal() override;
184 
185  bool wait(int32 milliseconds_timeout) override;
187 
188  private:
189  friend class thread;
190  void create(bool initially_owned, bool& created_new);
191  std::shared_ptr<mutex_base> mutex_;
192  ustring name_;
193  };
194  }
195 }
signal
Specifies signals that can interrupt the current process.
Definition: signal.h:18
intmax_t intptr
Represent a pointer or a handle.
Definition: types.h:151
Contains xtd::threading::wait_handle exception.
intptr native_handle_type
Rpresents the native handle type.
Definition: mutex.h:57
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition: icomparable.h:17
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition: lock_guard.h:30
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
A synchronization primitive that can also be used for interprocess synchronization.
Definition: mutex.h:48
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: iequatable.h:18
Represents an instant in time, typically expressed as a date and time of day.
Definition: date_time.h:78
Creates and controls a thread, sets its priority, and gets its status.
Definition: thread.h:41
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
Encapsulates operating system specific objects that wait for exclusive access to shared resources...
Definition: wait_handle.h:48