Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify the way I/O actions are created (CPU style)
[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::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 void BarrierAcquisitionImpl::finish()
32 {
33   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
34   actor::Simcall* simcall = simcalls_.front();
35   simcalls_.pop_front();
36
37   simcall->issuer_->waiting_synchro_ = nullptr;
38   simcall->issuer_->simcall_answer();
39 }
40 /* -------- Barrier -------- */
41
42 unsigned BarrierImpl::next_id_ = 0;
43
44 BarrierAcquisitionImplPtr BarrierImpl::acquire_async(actor::ActorImpl* issuer)
45 {
46   auto res = BarrierAcquisitionImplPtr(new kernel::activity::BarrierAcquisitionImpl(issuer, this), true);
47
48   XBT_DEBUG("%s acquires barrier #%u (%zu of %u)", issuer->get_cname(), id_, ongoing_acquisitions_.size(),
49             expected_actors_);
50   if (ongoing_acquisitions_.size() < expected_actors_ - 1) {
51     /* Not everybody arrived yet */
52     ongoing_acquisitions_.push_back(res);
53
54   } else {
55     for (auto const& acqui : ongoing_acquisitions_) {
56       acqui->granted_ = true;
57       if (acqui == acqui->get_issuer()->waiting_synchro_)
58         acqui->finish();
59       // else, the issuer is not blocked on this acquisition so no need to release it
60     }
61     ongoing_acquisitions_.clear(); // Rearm the barier for subsequent uses
62     res->granted_ = true;
63   }
64   return res;
65 }
66
67 } // namespace simgrid::kernel::activity