Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b528e0eb7e38a8497789f5df6837576c35047bf0
[simgrid.git] / src / kernel / activity / SemaphoreImpl.hpp
1 /* Copyright (c) 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_SEMAPHOREIMPL_HPP_
7 #define SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_
8
9 #include <atomic>
10 #include <boost/intrusive/list.hpp>
11
12 #include "simgrid/s4u/Semaphore.hpp"
13 #include "src/kernel/actor/ActorImpl.hpp"
14
15 namespace simgrid {
16 namespace kernel {
17 namespace activity {
18
19 class XBT_PUBLIC SemaphoreImpl {
20 public:
21   explicit SemaphoreImpl(unsigned int value) : value_(value){};
22   ~SemaphoreImpl() = default;
23
24   SemaphoreImpl(SemaphoreImpl const&) = delete;
25   SemaphoreImpl& operator=(SemaphoreImpl const&) = delete;
26
27   void acquire(smx_actor_t issuer, double timeout);
28   void release();
29   bool would_block() { return (value_ == 0); }
30   unsigned int get_capacity() { return value_; }
31
32   friend void intrusive_ptr_add_ref(SemaphoreImpl* sem)
33   {
34     XBT_ATTRIB_UNUSED auto previous = sem->refcount_.fetch_add(1);
35     xbt_assert(previous != 0);
36   }
37   friend void intrusive_ptr_release(SemaphoreImpl* sem)
38   {
39     if (sem->refcount_.fetch_sub(1) == 1)
40       delete sem;
41   }
42
43   unsigned int value_;
44   actor::SynchroList sleeping_; /* list of sleeping actors*/
45
46 private:
47   std::atomic_int_fast32_t refcount_{1};
48 };
49 } // namespace activity
50 } // namespace kernel
51 } // namespace simgrid
52
53 #endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_ */