1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved. */
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. */
6 #include "src/kernel/activity/BarrierImpl.hpp"
7 #include "src/kernel/activity/Synchro.hpp"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_barrier, ker_synchro, "Barrier kernel-space implementation");
15 /* -------- Acquisition -------- */
16 bool BarrierAcquisitionImpl::test(actor::ActorImpl*)
20 void BarrierAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
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.");
25 this->register_simcall(&issuer_->simcall_); // Block on that acquisition
27 if (granted_) { // This was unblocked already
30 // Already in the queue
33 void BarrierAcquisitionImpl::finish()
35 xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
36 actor::Simcall* simcall = simcalls_.front();
37 simcalls_.pop_front();
39 simcall->issuer_->waiting_synchro_ = nullptr;
40 simcall->issuer_->simcall_answer();
42 /* -------- Barrier -------- */
44 unsigned BarrierImpl::next_id_ = 0;
46 BarrierAcquisitionImplPtr BarrierImpl::acquire_async(actor::ActorImpl* issuer)
48 auto res = BarrierAcquisitionImplPtr(new kernel::activity::BarrierAcquisitionImpl(issuer, this), true);
50 XBT_DEBUG("%s acquires barrier #%u (%zu of %u)", issuer->get_cname(), id_, ongoing_acquisitions_.size(),
52 if (ongoing_acquisitions_.size() < expected_actors_ - 1) {
53 /* Not everybody arrived yet */
54 ongoing_acquisitions_.push_back(res);
57 for (auto const& acqui : ongoing_acquisitions_) {
58 acqui->granted_ = true;
59 if (acqui == acqui->get_issuer()->waiting_synchro_)
61 // else, the issuer is not blocked on this acquisition so no need to release it
63 ongoing_acquisitions_.clear(); // Rearm the barier for subsequent uses
69 } // namespace activity
71 } // namespace simgrid