Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / kernel / activity / MutexImpl.hpp
1 /* Copyright (c) 2012-2022. 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_KERNEL_ACTIVITY_MUTEX_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_MUTEX_HPP
8
9 #include "simgrid/s4u/Mutex.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   std::atomic_int_fast32_t refcount_{1};
19   s4u::Mutex piface_;
20   bool locked_ = false;
21   actor::ActorImpl* owner_ = nullptr;
22   // List of sleeping actors:
23   actor::SynchroList sleeping_;
24
25 public:
26   MutexImpl() : piface_(this) {}
27   MutexImpl(MutexImpl const&) = delete;
28   MutexImpl& operator=(MutexImpl const&) = delete;
29
30   void lock(actor::ActorImpl* issuer);
31   bool try_lock(actor::ActorImpl* issuer);
32   void unlock(actor::ActorImpl* issuer);
33   bool is_locked() const { return locked_; }
34
35   MutexImpl* ref();
36   void unref();
37
38   void remove_sleeping_actor(actor::ActorImpl& actor) { xbt::intrusive_erase(sleeping_, actor); }
39   actor::ActorImpl* get_owner() const { return owner_; }
40
41   // boost::intrusive_ptr<Mutex> support:
42   friend void intrusive_ptr_add_ref(MutexImpl* mutex)
43   {
44     XBT_ATTRIB_UNUSED auto previous = mutex->refcount_.fetch_add(1);
45     xbt_assert(previous != 0);
46   }
47
48   friend void intrusive_ptr_release(MutexImpl* mutex)
49   {
50     if (mutex->refcount_.fetch_sub(1) == 1)
51       delete mutex;
52   }
53
54   s4u::Mutex& mutex() { return piface_; }
55 };
56 } // namespace activity
57 } // namespace kernel
58 } // namespace simgrid
59 #endif