Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
66e9900eddbef3171493ad41c3f66dc68895e28d
[simgrid.git] / src / kernel / activity / SemaphoreImpl.cpp
1 /* Copyright (c) 2019. 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   RawImplPtr synchro = nullptr;
18
19   XBT_DEBUG("Wait semaphore %p (timeout:%f)", this, timeout);
20   if (value_ <= 0) {
21     synchro = RawImplPtr(new RawImpl());
22     (*synchro).set_host(issuer->get_host()).set_timeout(timeout).start();
23     synchro->simcalls_.push_front(&issuer->simcall);
24     issuer->waiting_synchro = synchro;
25     sleeping_.push_back(*issuer);
26   } else {
27     value_--;
28     SIMIX_simcall_answer(&issuer->simcall);
29   }
30 }
31 void SemaphoreImpl::release()
32 {
33   XBT_DEBUG("Sem release semaphore %p", this);
34
35   if (not sleeping_.empty()) {
36     auto& actor = sleeping_.front();
37     sleeping_.pop_front();
38     actor.waiting_synchro = nullptr;
39     SIMIX_simcall_answer(&actor.simcall);
40   } else {
41     value_++;
42   }
43 }
44
45 } // namespace activity
46 } // namespace kernel
47 } // namespace simgrid
48
49 // Simcall handlers:
50 /**
51  * @brief Handles a sem acquire simcall without timeout.
52  */
53 void simcall_HANDLER_sem_acquire(smx_simcall_t simcall, smx_sem_t sem)
54 {
55   sem->acquire(simcall->issuer, -1);
56 }
57
58 /**
59  * @brief Handles a sem acquire simcall with timeout.
60  */
61 void simcall_HANDLER_sem_acquire_timeout(smx_simcall_t simcall, smx_sem_t sem, double timeout)
62 {
63   simcall_sem_acquire_timeout__set__result(simcall, 0); // default result, will be set to 1 on timeout
64   sem->acquire(simcall->issuer, timeout);
65 }