Logo AND Algorithmique Numérique Distribuée

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