Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
smpi_init_options needs to be public
[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 SIMGRID_KERNEL_ACTIVITY_MUTEX_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_MUTEX_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   std::atomic_int_fast32_t refcount_{1};
19   s4u::Mutex piface_;
20   bool locked_ = false;
21
22 public:
23   MutexImpl() : piface_(this) {}
24   MutexImpl(MutexImpl const&) = delete;
25   MutexImpl& operator=(MutexImpl const&) = delete;
26
27   void lock(actor::ActorImpl* issuer);
28   bool try_lock(actor::ActorImpl* issuer);
29   void unlock(actor::ActorImpl* issuer);
30   bool is_locked() { return locked_; }
31
32   MutexImpl* ref();
33   void unref();
34
35   actor::ActorImpl* owner_ = nullptr;
36   // List of sleeping actors:
37   actor::SynchroList sleeping_;
38
39   // boost::intrusive_ptr<Mutex> support:
40   friend void intrusive_ptr_add_ref(MutexImpl* mutex)
41   {
42     XBT_ATTRIB_UNUSED auto previous = mutex->refcount_.fetch_add(1);
43     xbt_assert(previous != 0);
44   }
45
46   friend void intrusive_ptr_release(MutexImpl* mutex)
47   {
48     if (mutex->refcount_.fetch_sub(1) == 1)
49       delete mutex;
50   }
51
52   s4u::Mutex& mutex() { return piface_; }
53 };
54 }
55 }
56 }
57 #endif /* SIMIX_MUTEXIMPL_HPP */