Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Handle simcall result through mc::SimcallObserver.
[simgrid.git] / src / kernel / activity / SemaphoreImpl.cpp
1 /* Copyright (c) 2019-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/kernel/activity/SemaphoreImpl.hpp"
7 #include "src/kernel/activity/SynchroRaw.hpp"
8 #include "src/mc/checker/SimcallObserver.hpp"
9 #include <cmath> // std::isfinite
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_semaphore, simix_synchro, "Semaphore kernel-space implementation");
12
13 namespace simgrid {
14 namespace kernel {
15 namespace activity {
16
17 void SemaphoreImpl::acquire(actor::ActorImpl* issuer, double timeout)
18 {
19   XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
20   xbt_assert(std::isfinite(timeout), "timeout is not finite!");
21
22   if (value_ <= 0) {
23     RawImplPtr synchro(new RawImpl([this, issuer]() {
24       this->remove_sleeping_actor(*issuer);
25       auto* observer = dynamic_cast<mc::SemAcquireSimcall*>(issuer->simcall_.observer_);
26       xbt_assert(observer != nullptr);
27       observer->set_result(true);
28     }));
29     synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
30     synchro->register_simcall(&issuer->simcall_);
31     sleeping_.push_back(*issuer);
32   } else {
33     value_--;
34     issuer->simcall_answer();
35   }
36 }
37 void SemaphoreImpl::release()
38 {
39   XBT_DEBUG("Sem release semaphore %p", this);
40
41   if (not sleeping_.empty()) {
42     auto& actor = sleeping_.front();
43     sleeping_.pop_front();
44     actor.waiting_synchro_ = nullptr;
45     actor.simcall_answer();
46   } else {
47     value_++;
48   }
49 }
50
51 /** Increase the refcount for this semaphore */
52 SemaphoreImpl* SemaphoreImpl::ref()
53 {
54   intrusive_ptr_add_ref(this);
55   return this;
56 }
57
58 /** Decrease the refcount for this mutex */
59 void SemaphoreImpl::unref()
60 {
61   intrusive_ptr_release(this);
62 }
63
64 } // namespace activity
65 } // namespace kernel
66 } // namespace simgrid