Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a863b7dd0a74a9443f69c52c275b28afacfc15c3
[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 private:
25   friend simgrid::simix::Mutex;
26   simgrid::simix::Mutex* mutex_;
27   Mutex(simgrid::simix::Mutex* mutex) : mutex_(mutex) {}
28 public:
29
30   friend void intrusive_ptr_add_ref(Mutex* mutex)
31   {
32     xbt_assert(mutex);
33     SIMIX_mutex_ref(mutex->mutex_);
34   }
35   friend void intrusive_ptr_release(Mutex* mutex)
36   {
37     xbt_assert(mutex);
38     SIMIX_mutex_unref(mutex->mutex_);
39   }
40   using Ptr = boost::intrusive_ptr<Mutex>;
41
42   // No copy:
43   Mutex(Mutex const&) = delete;
44   Mutex& operator(Mutex const&) = delete;
45
46   static Ptr createMutex();
47
48 public:
49   void lock();
50   void unlock();
51   bool try_lock();
52 };
53
54 using MutexPtr = Mutex::Ptr;
55
56 }} // namespace simgrid::s4u
57
58 #endif /* SIMGRID_S4U_MUTEX_HPP */