#include "src/kernel/activity/SemaphoreImpl.hpp"
#include "src/kernel/activity/SynchroRaw.hpp"
+#include <cmath> // std::isfinite
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_semaphore, simix_synchro, "Semaphore kernel-space implementation");
void SemaphoreImpl::acquire(actor::ActorImpl* issuer, double timeout)
{
XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
+ xbt_assert(std::isfinite(timeout), "timeout is not finite!");
+ simix::marshal<bool>(issuer->simcall_.result_, false); // default result, will be set to 'true' on timeout
+
if (value_ <= 0) {
- RawImplPtr synchro(new RawImpl());
+ RawImplPtr synchro(new RawImpl([this, issuer]() {
+ this->remove_sleeping_actor(*issuer);
+ simix::marshal<bool>(issuer->simcall_.result_, true);
+ }));
synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
synchro->register_simcall(&issuer->simcall_);
sleeping_.push_back(*issuer);
}
}
-} // namespace activity
-} // namespace kernel
-} // namespace simgrid
-
-// Simcall handlers:
-/**
- * @brief Handles a sem acquire simcall without timeout.
- */
-void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
+/** Increase the refcount for this semaphore */
+SemaphoreImpl* SemaphoreImpl::ref()
{
- sem->acquire(simcall->issuer_, -1);
+ intrusive_ptr_add_ref(this);
+ return this;
}
-/**
- * @brief Handles a sem acquire simcall with timeout.
- */
-void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
+/** Decrease the refcount for this mutex */
+void SemaphoreImpl::unref()
{
- simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
- sem->acquire(simcall->issuer_, timeout);
+ intrusive_ptr_release(this);
}
+
+} // namespace activity
+} // namespace kernel
+} // namespace simgrid