Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Semaphore made observable from the Checker side
[simgrid.git] / src / s4u / s4u_Semaphore.cpp
1 /* Copyright (c) 2018-2022. 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 <simgrid/modelchecker.h>
7 #include <simgrid/s4u/Semaphore.hpp>
8 #include <simgrid/semaphore.h>
9
10 #include "src/kernel/activity/SemaphoreImpl.hpp"
11 #include "src/kernel/actor/SynchroObserver.hpp"
12 #include "src/mc/mc_replay.hpp"
13
14 namespace simgrid {
15 namespace s4u {
16
17 SemaphorePtr Semaphore::create(unsigned int initial_capacity)
18 {
19   auto* sem = new kernel::activity::SemaphoreImpl(initial_capacity);
20   return SemaphorePtr(&sem->sem(), false);
21 }
22
23 void Semaphore::acquire()
24 {
25   acquire_timeout(-1);
26 }
27
28 bool Semaphore::acquire_timeout(double timeout)
29 {
30   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
31
32   if (MC_is_active() || MC_record_replay_is_active()) { // Split in 2 simcalls for transition persistency
33     kernel::actor::SemaphoreObserver lock_observer{issuer, mc::Transition::Type::SEM_LOCK, pimpl_};
34     auto acquisition =
35         kernel::actor::simcall_answered([issuer, this] { return pimpl_->acquire_async(issuer); }, &lock_observer);
36
37     kernel::actor::SemaphoreAcquisitionObserver wait_observer{issuer, mc::Transition::Type::SEM_WAIT, acquisition.get(),
38                                                               timeout};
39     return kernel::actor::simcall_blocking(
40         [issuer, acquisition, timeout] { return acquisition->wait_for(issuer, timeout); }, &wait_observer);
41
42   } else { // Do it in one simcall only and without observer
43     kernel::actor::SemaphoreAcquisitionObserver observer{issuer, mc::Transition::Type::SEM_WAIT, nullptr, timeout};
44     return kernel::actor::simcall_blocking(
45         [this, issuer, timeout] { return pimpl_->acquire_async(issuer)->wait_for(issuer, timeout); }, &observer);
46   }
47 }
48
49 void Semaphore::release()
50 {
51   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
52   kernel::actor::SemaphoreObserver observer{issuer, mc::Transition::Type::SEM_UNLOCK, pimpl_};
53
54   kernel::actor::simcall_answered([this] { pimpl_->release(); }, &observer);
55 }
56
57 int Semaphore::get_capacity() const
58 {
59   return pimpl_->get_capacity();
60 }
61
62 bool Semaphore::would_block() const
63 {
64   return pimpl_->would_block();
65 }
66
67 /* refcounting of the intrusive_ptr is delegated to the implementation object */
68 void intrusive_ptr_add_ref(const Semaphore* sem)
69 {
70   intrusive_ptr_add_ref(sem->pimpl_);
71 }
72
73 void intrusive_ptr_release(const Semaphore* sem)
74 {
75   intrusive_ptr_release(sem->pimpl_);
76 }
77
78 } // namespace s4u
79 } // namespace simgrid
80
81 /* **************************** Public C interface *************************** */
82 /** @brief creates a semaphore object of the given initial capacity */
83 sg_sem_t sg_sem_init(int initial_value)
84 {
85   return simgrid::s4u::Semaphore::create(initial_value).detach();
86 }
87
88 /** @brief locks on a semaphore object */
89 void sg_sem_acquire(sg_sem_t sem)
90 {
91   sem->acquire();
92 }
93
94 /** @brief locks on a semaphore object up until the provided timeout expires */
95 int sg_sem_acquire_timeout(sg_sem_t sem, double timeout)
96 {
97   return sem->acquire_timeout(timeout);
98 }
99
100 /** @brief releases the semaphore object */
101 void sg_sem_release(sg_sem_t sem)
102 {
103   sem->release();
104 }
105
106 int sg_sem_get_capacity(const_sg_sem_t sem)
107 {
108   return sem->get_capacity();
109 }
110
111 void sg_sem_destroy(const_sg_sem_t sem)
112 {
113   intrusive_ptr_release(sem);
114 }
115
116 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
117  *
118  * Note that the returned value may be wrong right after the function call, when you try to use it...
119  * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
120  */
121 int sg_sem_would_block(const_sg_sem_t sem)
122 {
123   return sem->would_block();
124 }