Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
27a47c137d64a6b3a5f2455b65ccc9f77d90ac4d
[simgrid.git] / src / kernel / activity / MutexImpl.hpp
1 /* Copyright (c) 2012-2019. 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 SIMIX_MUTEXIMPL_HPP
7 #define SIMIX_MUTEXIMPL_HPP
8
9 #include "simgrid/s4u/ConditionVariable.hpp"
10 #include "src/kernel/actor/ActorImpl.hpp"
11 #include <boost/intrusive/list.hpp>
12
13 namespace simgrid {
14 namespace kernel {
15 namespace activity {
16
17 class XBT_PUBLIC MutexImpl {
18 public:
19   MutexImpl() : piface_(this) {}
20   MutexImpl(MutexImpl const&) = delete;
21   MutexImpl& operator=(MutexImpl const&) = delete;
22
23   void lock(actor::ActorImpl* issuer);
24   bool try_lock(actor::ActorImpl* issuer);
25   void unlock(actor::ActorImpl* issuer);
26
27   MutexImpl* ref();
28   void unref();
29   bool locked_             = false;
30   actor::ActorImpl* owner_ = nullptr;
31   // List of sleeping actors:
32   actor::SynchroList sleeping_;
33
34   // boost::intrusive_ptr<Mutex> support:
35   friend void intrusive_ptr_add_ref(MutexImpl* mutex)
36   {
37     XBT_ATTRIB_UNUSED auto previous = mutex->refcount_.fetch_add(1);
38     xbt_assert(previous != 0);
39   }
40
41   friend void intrusive_ptr_release(MutexImpl* mutex)
42   {
43     if (mutex->refcount_.fetch_sub(1) == 1)
44       delete mutex;
45   }
46
47   s4u::Mutex& mutex() { return piface_; }
48
49 private:
50   std::atomic_int_fast32_t refcount_{1};
51   s4u::Mutex piface_;
52 };
53 }
54 }
55 }
56 #endif /* SIMIX_MUTEXIMPL_HPP */