1 /* Copyright (c) 2019-2022. 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 "src/kernel/activity/SemaphoreImpl.hpp"
7 #include "src/kernel/activity/Synchro.hpp"
8 #include "src/kernel/actor/SimcallObserver.hpp"
9 #include <cmath> // std::isfinite
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_semaphore, ker_synchro, "Semaphore kernel-space implementation");
17 void SemaphoreImpl::acquire(actor::ActorImpl* issuer, double timeout)
19 XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
20 xbt_assert(std::isfinite(timeout), "timeout is not finite!");
23 RawImplPtr synchro(new SynchroImpl([this, issuer]() {
24 this->remove_sleeping_actor(*issuer);
25 auto* observer = dynamic_cast<kernel::actor::SemAcquireSimcall*>(issuer->simcall_.observer_);
26 xbt_assert(observer != nullptr);
27 observer->set_result(true);
29 synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
30 synchro->register_simcall(&issuer->simcall_);
31 sleeping_.push_back(*issuer);
34 issuer->simcall_answer();
37 void SemaphoreImpl::release()
39 XBT_DEBUG("Sem release semaphore %p", this);
41 if (not sleeping_.empty()) {
42 auto& actor = sleeping_.front();
43 sleeping_.pop_front();
44 actor.waiting_synchro_ = nullptr;
45 actor.simcall_answer();
51 /** Increase the refcount for this semaphore */
52 SemaphoreImpl* SemaphoreImpl::ref()
54 intrusive_ptr_add_ref(this);
58 /** Decrease the refcount for this mutex */
59 void SemaphoreImpl::unref()
61 intrusive_ptr_release(this);
64 } // namespace activity
66 } // namespace simgrid