DASH  0.3.0
Mutex.h
1 #ifndef DASH__MUTEX_H__INCLUDED
2 #define DASH__MUTEX_H__INCLUDED
3 
4 #include <dash/Team.h>
6 
7 namespace dash {
8 
30 class Mutex {
31 private:
32  using self_t = Mutex;
33 
34  struct DestroyDARTLock {
35  void operator()(dart_lock_t lock)
36  {
37  if (DART_LOCK_NULL != lock) {
38  auto ret = dart_team_lock_destroy(&lock);
39 
40  if (ret != DART_OK) {
41  DASH_LOG_ERROR(
42  "dash::Mutex::operator()",
43  "Failed to destroy DART lock! "
44  "(dart_team_lock_destroy failed)");
45  }
46  }
47  }
48  };
49 
50 public:
58  explicit Mutex(Team& team = dash::Team::All());
59 
60  Mutex(const Mutex& other) = delete;
61  Mutex(Mutex&& other) = default;
62 
63  self_t& operator=(const self_t& other) = delete;
64  self_t& operator=(self_t&& other) = default;
65 
71  ~Mutex() = default;
72 
80  bool init();
81 
85  void lock();
86 
91  bool try_lock();
92 
96  void unlock();
97 
98 private:
99  dash::Team const* _team{nullptr};
100  std::unique_ptr<std::remove_pointer<dart_lock_t>::type, DestroyDARTLock>
101  _mutex{DART_LOCK_NULL};
102 }; // class Mutex
103 
104 } // namespace dash
105 
106 #endif // DASH__MUTEX_H__INCLUDED
bool try_lock()
Try to acquire the lock and return immediately.
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
Signals success.
Definition: dart_types.h:33
void lock()
Block until the lock was acquired.
#define DART_LOCK_NULL
Null value for dart_lock_t to reset a DART lock instance.
bool init()
Collective initialization of the DART lock.
~Mutex()=default
Collective destructor to destruct a DART lock.
A Team instance specifies a subset of all available units.
Definition: Team.h:41
Mutex(Team &team=dash::Team::All())
DASH Mutex is only valid for a dash team.
struct dart_lock_struct * dart_lock_t
Lock type to ensure mutual exclusion among units in a team.
dart_ret_t dart_team_lock_destroy(dart_lock_t *lock)
Collective operation to destroy a lock initialized using dart_team_lock_init.
Behaves similar to std::mutex and is used to ensure mutual exclusion within a dash team...
Definition: Mutex.h:30
static Team & All()
The invariant Team instance containing all available units.
Definition: Team.h:213
void unlock()
Release the lock acquired through lock() or try_lock().