1 /* Copyright (c) 2018-2023. 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 #include <simgrid/modelchecker.h>
7 #include <simgrid/s4u/Semaphore.hpp>
8 #include <simgrid/semaphore.h>
10 #include "src/kernel/activity/SemaphoreImpl.hpp"
11 #include "src/kernel/actor/SynchroObserver.hpp"
12 #include "src/mc/mc_replay.hpp"
14 namespace simgrid::s4u {
16 SemaphorePtr Semaphore::create(unsigned int initial_capacity)
18 auto* sem = new kernel::activity::SemaphoreImpl(initial_capacity);
19 return SemaphorePtr(&sem->sem(), false);
22 void Semaphore::acquire()
27 bool Semaphore::acquire_timeout(double timeout)
29 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
31 if (MC_is_active() || MC_record_replay_is_active()) { // Split in 2 simcalls for transition persistency
32 kernel::actor::SemaphoreObserver lock_observer{issuer, mc::Transition::Type::SEM_ASYNC_LOCK, pimpl_};
34 kernel::actor::simcall_answered([issuer, this] { return pimpl_->acquire_async(issuer); }, &lock_observer);
36 kernel::actor::SemaphoreAcquisitionObserver wait_observer{issuer, mc::Transition::Type::SEM_WAIT, acquisition.get(),
38 return kernel::actor::simcall_blocking([issuer, acquisition, timeout] { acquisition->wait_for(issuer, timeout); },
41 } else { // Do it in one simcall only and without observer
42 kernel::actor::SemaphoreAcquisitionObserver observer{issuer, mc::Transition::Type::SEM_WAIT, nullptr, timeout};
43 return kernel::actor::simcall_blocking(
44 [this, issuer, timeout] { pimpl_->acquire_async(issuer)->wait_for(issuer, timeout); }, &observer);
48 void Semaphore::release()
50 kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
51 kernel::actor::SemaphoreObserver observer{issuer, mc::Transition::Type::SEM_UNLOCK, pimpl_};
53 kernel::actor::simcall_answered([this] { pimpl_->release(); }, &observer);
56 int Semaphore::get_capacity() const
58 return pimpl_->get_capacity();
61 bool Semaphore::would_block() const
63 return pimpl_->would_block();
66 /* refcounting of the intrusive_ptr is delegated to the implementation object */
67 void intrusive_ptr_add_ref(const Semaphore* sem)
69 intrusive_ptr_add_ref(sem->pimpl_);
72 void intrusive_ptr_release(const Semaphore* sem)
74 intrusive_ptr_release(sem->pimpl_);
77 } // namespace simgrid::s4u
79 /* **************************** Public C interface *************************** */
80 /** @brief creates a semaphore object of the given initial capacity */
81 sg_sem_t sg_sem_init(int initial_value)
83 return simgrid::s4u::Semaphore::create(initial_value).detach();
86 /** @brief locks on a semaphore object */
87 void sg_sem_acquire(sg_sem_t sem)
92 /** @brief locks on a semaphore object up until the provided timeout expires */
93 int sg_sem_acquire_timeout(sg_sem_t sem, double timeout)
95 return sem->acquire_timeout(timeout);
98 /** @brief releases the semaphore object */
99 void sg_sem_release(sg_sem_t sem)
104 int sg_sem_get_capacity(const_sg_sem_t sem)
106 return sem->get_capacity();
109 void sg_sem_destroy(const_sg_sem_t sem)
111 intrusive_ptr_release(sem);
114 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
116 * Note that the returned value may be wrong right after the function call, when you try to use it...
117 * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
119 int sg_sem_would_block(const_sg_sem_t sem)
121 return sem->would_block();