X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/62534c29d0fe83dbec6b7e7642d2ecbe100f3d7b..916422155091e4e7e0f463cec933b88fed642e8a:/include/simgrid/s4u/Semaphore.hpp diff --git a/include/simgrid/s4u/Semaphore.hpp b/include/simgrid/s4u/Semaphore.hpp index 442b147eb1..99089a7553 100644 --- a/include/simgrid/s4u/Semaphore.hpp +++ b/include/simgrid/s4u/Semaphore.hpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2006-2018. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2006-2023. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ @@ -7,48 +7,55 @@ #define SIMGRID_S4U_SEMAPHORE_HPP #include -#include -namespace simgrid { -namespace s4u { +namespace simgrid::s4u { /** @brief A classical semaphore, but blocking in the simulation world - * @ingroup s4u_api * + * @beginrst * It is strictly impossible to use a real semaphore, such as - * sem_init, + * `sem_init `_, * because it would block the whole simulation. * Instead, you should use the present class, that offers a very similar interface. * - * As for any S4U object, Semaphores are using the @ref s4u_raii "RAII idiom" for memory management. - * Use createSemaphore() to get a ::SemaphorePtr to a newly created semaphore - * and only manipulate ::SemaphorePtr. + * An example is available in Section :ref:`s4u_ex_IPC`. + * + * As for any S4U object, you can use the :ref:`RAII idiom ` for memory management of semaphores. + * Use :cpp:func:`create() ` to get a :cpp:type:`simgrid::s4u::SemaphorePtr` to a newly + * created semaphore, that will get automatically freed when the variable goes out of scope. + * @endrst * */ class XBT_PUBLIC Semaphore { - smx_sem_t sem_; - std::atomic_int_fast32_t refcount_{0}; +#ifndef DOXYGEN + friend kernel::activity::SemaphoreImpl; + friend XBT_PUBLIC void kernel::activity::intrusive_ptr_release(kernel::activity::SemaphoreImpl* sem); +#endif - explicit Semaphore(unsigned int initial_capacity); - ~Semaphore(); + kernel::activity::SemaphoreImpl* const pimpl_; - friend void intrusive_ptr_add_ref(Semaphore* sem); - friend void intrusive_ptr_release(Semaphore* sem); + friend XBT_PUBLIC void intrusive_ptr_add_ref(const Semaphore* sem); + friend XBT_PUBLIC void intrusive_ptr_release(const Semaphore* sem); -public: - // No copy: - /** You cannot create a new semaphore by copying an existing one. Use SemaphorePtr instead */ - Semaphore(Semaphore const&) = delete; - /** You cannot create a new semaphore by value assignment either. Use SemaphorePtr instead */ - Semaphore& operator=(Semaphore const&) = delete; + explicit Semaphore(kernel::activity::SemaphoreImpl* sem) : pimpl_(sem) {} + ~Semaphore() = default; +#ifndef DOXYGEN + Semaphore(Semaphore const&) = delete; // No copy constructor. Use SemaphorePtr instead + Semaphore& operator=(Semaphore const&) = delete; // No direct assignment either. Use SemaphorePtr instead +#endif +public: /** Constructs a new semaphore */ static SemaphorePtr create(unsigned int initial_capacity); void acquire(); + /** Returns true if there was a timeout */ + bool acquire_timeout(double timeout); void release(); + int get_capacity() const; + bool would_block() const; }; -}} // namespace simgrid::s4u +} // namespace simgrid::s4u #endif /* SIMGRID_S4U_SEMAPHORE_HPP */