Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stick to our coding standards: fields must have a trailing _
[simgrid.git] / src / kernel / activity / SemaphoreImpl.cpp
1 /* Copyright (c) 2019-2020. 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
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_semaphore, simix_synchro, "Semaphore kernel-space implementation");
10
11 namespace simgrid {
12 namespace kernel {
13 namespace activity {
14
15 void SemaphoreImpl::acquire(actor::ActorImpl* issuer, double timeout)
16 {
17   XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
18   if (value_ <= 0) {
19     RawImplPtr synchro = RawImplPtr(new RawImpl());
20     synchro->set_host(issuer->get_host()).set_timeout(timeout).start();
21     synchro->register_simcall(&issuer->simcall_);
22     sleeping_.push_back(*issuer);
23   } else {
24     value_--;
25     issuer->simcall_answer();
26   }
27 }
28 void SemaphoreImpl::release()
29 {
30   XBT_DEBUG("Sem release semaphore %p", this);
31
32   if (not sleeping_.empty()) {
33     auto& actor = sleeping_.front();
34     sleeping_.pop_front();
35     actor.waiting_synchro_ = nullptr;
36     actor.simcall_answer();
37   } else {
38     value_++;
39   }
40 }
41
42 } // namespace activity
43 } // namespace kernel
44 } // namespace simgrid
45
46 // Simcall handlers:
47 /**
48  * @brief Handles a sem acquire simcall without timeout.
49  */
50 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
51 {
52   sem->acquire(simcall->issuer_, -1);
53 }
54
55 /**
56  * @brief Handles a sem acquire simcall with timeout.
57  */
58 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
59 {
60   simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
61   sem->acquire(simcall->issuer_, timeout);
62 }