Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c1537f15317bde64d6498da6e795926de384e18a
[simgrid.git] / src / kernel / activity / BarrierImpl.cpp
1 /* Copyright (c) 2007-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 "src/kernel/activity/BarrierImpl.hpp"
7 #include "src/kernel/activity/Synchro.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_barrier, ker_synchro, "Barrier kernel-space implementation");
10
11 namespace simgrid {
12 namespace kernel {
13 namespace activity {
14
15 /* -------- Acquisition -------- */
16 bool BarrierAcquisitionImpl::test(actor::ActorImpl*)
17 {
18   return granted_;
19 }
20 void BarrierAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
21 {
22   xbt_assert(issuer == issuer_, "Cannot wait on acquisitions created by another actor (id %ld)", issuer_->get_pid());
23   xbt_assert(timeout < 0, "Timeouts on barrier acquisitions are not implemented yet.");
24
25   this->register_simcall(&issuer_->simcall_); // Block on that acquisition
26
27   if (granted_) { // This was unblocked already
28     finish();
29   } else {
30     // Already in the queue
31   }
32 }
33 void BarrierAcquisitionImpl::finish()
34 {
35   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
36   smx_simcall_t simcall = simcalls_.front();
37   simcalls_.pop_front();
38
39   simcall->issuer_->waiting_synchro_ = nullptr;
40   simcall->issuer_->simcall_answer();
41 }
42 /* -------- Barrier -------- */
43
44 unsigned BarrierImpl::next_id_ = 0;
45
46 BarrierAcquisitionImplPtr BarrierImpl::acquire_async(actor::ActorImpl* issuer)
47 {
48   auto res = BarrierAcquisitionImplPtr(new kernel::activity::BarrierAcquisitionImpl(issuer, this), true);
49
50   XBT_DEBUG("%s acquires barrier #%u (%zu of %u)", issuer->get_cname(), id_, ongoing_acquisitions_.size(),
51             expected_actors_);
52   if (ongoing_acquisitions_.size() < expected_actors_ - 1) {
53     /* Not everybody arrived yet */
54     ongoing_acquisitions_.push_back(res);
55
56   } else {
57     for (auto const& acqui : ongoing_acquisitions_) {
58       acqui->granted_ = true;
59       if (acqui == acqui->get_issuer()->waiting_synchro_)
60         acqui->finish();
61       // else, the issuer is not blocked on this acquisition so no need to release it
62     }
63     ongoing_acquisitions_.clear(); // Rearm the barier for subsequent uses
64     res->granted_ = true;
65   }
66   return res;
67 }
68
69 } // namespace activity
70 } // namespace kernel
71 } // namespace simgrid