Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] Headers cleanup
[simgrid.git] / include / simgrid / s4u / mutex.hpp
1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_S4U_MUTEX_HPP
7 #define SIMGRID_S4U_MUTEX_HPP
8
9 #include <mutex>
10 #include <utility>
11
12 #include <boost/intrusive_ptr.hpp>
13
14 #include <xbt/base.h>
15 #include "simgrid/simix.h"
16
17 namespace simgrid {
18 namespace s4u {
19
20 class ConditionVariable;
21
22 XBT_PUBLIC_CLASS Mutex {
23 friend ConditionVariable;
24 public:
25   Mutex() :
26     mutex_(simcall_mutex_init()) {}
27   Mutex(simgrid::simix::Mutex* mutex) : mutex_(SIMIX_mutex_ref(mutex)) {}
28   ~Mutex()
29   {
30     SIMIX_mutex_unref(mutex_);
31   }
32
33   // Copy+move (with the copy-and-swap idiom):
34   Mutex(Mutex const& mutex) : mutex_(SIMIX_mutex_ref(mutex.mutex_)) {}
35   friend void swap(Mutex& first, Mutex& second)
36   {
37     using std::swap;
38     swap(first.mutex_, second.mutex_);
39   }
40   Mutex& operator=(Mutex mutex)
41   {
42     swap(*this, mutex);
43     return *this;
44   }
45   Mutex(Mutex&& mutex) : mutex_(nullptr)
46   {
47     swap(*this, mutex);
48   }
49
50   bool valid() const
51   {
52     return mutex_ != nullptr;
53   }
54
55 public:
56   void lock();
57   void unlock();
58   bool try_lock();
59
60 private:
61   simgrid::simix::Mutex* mutex_;
62 };
63
64 }} // namespace simgrid::s4u
65
66 #endif /* SIMGRID_S4U_MUTEX_HPP */