X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/2cf13759ba1696d88f5ff86afee5324f30ccdbfc..36f318bf846ce5271b53a53deeb90ed0340b441f:/src/kernel/activity/SemaphoreImpl.cpp diff --git a/src/kernel/activity/SemaphoreImpl.cpp b/src/kernel/activity/SemaphoreImpl.cpp index 9d47f0bce5..fb13608087 100644 --- a/src/kernel/activity/SemaphoreImpl.cpp +++ b/src/kernel/activity/SemaphoreImpl.cpp @@ -1,10 +1,12 @@ -/* Copyright (c) 2019. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2019-2021. 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. */ #include "src/kernel/activity/SemaphoreImpl.hpp" -#include "src/simix/smx_synchro_private.hpp" +#include "src/kernel/activity/SynchroRaw.hpp" +#include "src/kernel/actor/SimcallObserver.hpp" +#include // std::isfinite XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_semaphore, simix_synchro, "Semaphore kernel-space implementation"); @@ -12,19 +14,24 @@ namespace simgrid { namespace kernel { namespace activity { -void SemaphoreImpl::acquire(smx_actor_t issuer, double timeout) +void SemaphoreImpl::acquire(actor::ActorImpl* issuer, double timeout) { - smx_activity_t synchro = nullptr; - XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout); + xbt_assert(std::isfinite(timeout), "timeout is not finite!"); + if (value_ <= 0) { - synchro = SIMIX_synchro_wait(issuer->get_host(), timeout); - synchro->simcalls_.push_front(&issuer->simcall); - issuer->waiting_synchro = synchro; + RawImplPtr synchro(new RawImpl([this, issuer]() { + this->remove_sleeping_actor(*issuer); + auto* observer = dynamic_cast(issuer->simcall_.observer_); + xbt_assert(observer != nullptr); + observer->set_result(true); + })); + synchro->set_host(issuer->get_host()).set_timeout(timeout).start(); + synchro->register_simcall(&issuer->simcall_); sleeping_.push_back(*issuer); } else { value_--; - SIMIX_simcall_answer(&issuer->simcall); + issuer->simcall_answer(); } } void SemaphoreImpl::release() @@ -34,31 +41,26 @@ void SemaphoreImpl::release() if (not sleeping_.empty()) { auto& actor = sleeping_.front(); sleeping_.pop_front(); - actor.waiting_synchro = nullptr; - SIMIX_simcall_answer(&actor.simcall); + actor.waiting_synchro_ = nullptr; + actor.simcall_answer(); } else { value_++; } } -} // 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