Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move MutexImpl to the right namespace
[simgrid.git] / src / kernel / activity / MutexImpl.hpp
1 /* Copyright (c) 2012-2017. 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/simix/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();
20   ~MutexImpl();
21   MutexImpl(MutexImpl const&) = delete;
22   MutexImpl& operator=(MutexImpl const&) = delete;
23
24   void lock(smx_actor_t issuer);
25   bool try_lock(smx_actor_t issuer);
26   void unlock(smx_actor_t issuer);
27
28   bool locked       = false;
29   smx_actor_t owner = nullptr;
30   // List of sleeping processes:
31   simgrid::simix::SynchroList sleeping;
32
33   // boost::intrusive_ptr<Mutex> support:
34   friend void intrusive_ptr_add_ref(MutexImpl* mutex)
35   {
36     XBT_ATTRIB_UNUSED auto previous = mutex->refcount_.fetch_add(1);
37     xbt_assert(previous != 0);
38   }
39   friend void intrusive_ptr_release(MutexImpl* mutex)
40   {
41     if (mutex->refcount_.fetch_sub(1) == 1)
42       delete mutex;
43   }
44
45   simgrid::s4u::Mutex& mutex() { return mutex_; }
46
47 private:
48   std::atomic_int_fast32_t refcount_{1};
49   simgrid::s4u::Mutex mutex_;
50 };
51 }
52 }
53 }
54 #endif /* SIMIX_MUTEXIMPL_HPP */