Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
some MC tests still believe that platform.xml exist
[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 <utility>
10
11 #include <boost/intrusive_ptr.hpp>
12 #include <xbt/base.h>
13 #include "simgrid/simix.h"
14 #include <simgrid/s4u/conditionVariable.hpp>
15
16 namespace simgrid {
17 namespace s4u {
18
19 class ConditionVariable;
20 XBT_PUBLIC_CLASS Mutex {
21 friend ConditionVariable;
22 public:
23   Mutex() :
24     mutex_(simcall_mutex_init()) {}
25   Mutex(simgrid::simix::Mutex* mutex) : mutex_(SIMIX_mutex_ref(mutex)) {}
26   ~Mutex()
27   {
28     SIMIX_mutex_unref(mutex_);
29   }
30
31   // Copy+move (with the copy-and-swap idiom):
32   Mutex(Mutex const& mutex) : mutex_(SIMIX_mutex_ref(mutex.mutex_)) {}
33   friend void swap(Mutex& first, Mutex& second)
34   {
35     using std::swap;
36     swap(first.mutex_, second.mutex_);
37   }
38   Mutex& operator=(Mutex mutex)
39   {
40     swap(*this, mutex);
41     return *this;
42   }
43   Mutex(Mutex&& mutex) : mutex_(nullptr)
44   {
45     swap(*this, mutex);
46   }
47
48   bool valid() const
49   {
50     return mutex_ != nullptr;
51   }
52
53 public:
54   void lock();
55   void unlock();
56   bool try_lock();
57
58 private:
59   simgrid::simix::Mutex* mutex_;
60 };
61 }} // namespace simgrid::s4u
62
63 #endif /* SIMGRID_S4U_MUTEX_HPP */