1 /* Copyright (c) 2019-2020. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_KERNEL_ACTIVITY_SEMAPHORE_HPP
7 #define SIMGRID_KERNEL_ACTIVITY_SEMAPHORE_HPP
10 #include <boost/intrusive/list.hpp>
12 #include "simgrid/s4u/Semaphore.hpp"
13 #include "src/kernel/actor/ActorImpl.hpp"
19 class XBT_PUBLIC SemaphoreImpl {
20 std::atomic_int_fast32_t refcount_{1};
24 actor::SynchroList sleeping_; /* list of sleeping actors*/
26 explicit SemaphoreImpl(unsigned int value) : value_(value){};
27 ~SemaphoreImpl() = default;
29 SemaphoreImpl(SemaphoreImpl const&) = delete;
30 SemaphoreImpl& operator=(SemaphoreImpl const&) = delete;
32 void acquire(actor::ActorImpl* issuer, double timeout);
34 bool would_block() { return (value_ == 0); }
36 unsigned int get_capacity() { return value_; }
38 friend void intrusive_ptr_add_ref(SemaphoreImpl* sem)
40 XBT_ATTRIB_UNUSED auto previous = sem->refcount_.fetch_add(1);
41 xbt_assert(previous != 0);
43 friend void intrusive_ptr_release(SemaphoreImpl* sem)
45 if (sem->refcount_.fetch_sub(1) == 1)
49 } // namespace activity
51 } // namespace simgrid
53 #endif /* SIMGRID_KERNEL_ACTIVITY_SEMAPHOREIMPL_HPP_ */