X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d8e688dbb6900e672d3d36acea16bb421ea1a50b..87ab6a5d123f0032675234e655f79dac2bc78beb:/src/s4u/s4u_Barrier.cpp diff --git a/src/s4u/s4u_Barrier.cpp b/src/s4u/s4u_Barrier.cpp index 713245c9e1..f7104fb009 100644 --- a/src/s4u/s4u_Barrier.cpp +++ b/src/s4u/s4u_Barrier.cpp @@ -1,66 +1,94 @@ -/* Copyright (c) 2018. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2018-2022. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include -#include +#include +#include +#include -#include -#include +#include "src/kernel/activity/BarrierImpl.hpp" +#include "src/kernel/actor/SynchroObserver.hpp" +#include "src/mc/mc_replay.hpp" -#include "simgrid/barrier.h" -#include "simgrid/s4u/Barrier.hpp" -#include "simgrid/simix.h" +XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_barrier, s4u, "S4U barrier"); -XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_barrier, "S4U barrier"); +namespace simgrid::s4u { -namespace simgrid { -namespace s4u { - -Barrier::Barrier(unsigned int count) : expected_processes_(count) +/** @brief Create a new barrier + * + * See @ref s4u_raii. + */ +BarrierPtr Barrier::create(unsigned int expected_actors) { - mutex_ = Mutex::create(); - cond_ = ConditionVariable::create(); + auto* res = new kernel::activity::BarrierImpl(expected_actors); + return BarrierPtr(&res->piface_, false); } -/** - * Wait functions +/** @brief Block the current actor until all expected actors reach the barrier. + * + * This method is meant to be somewhat consistent with the pthread_barrier_wait function. + * + * @return false for all actors but one: exactly one actor will get true as a return value. */ int Barrier::wait() { - mutex_->lock(); - arrived_processes_++; - XBT_DEBUG("waiting %p %u/%u", this, arrived_processes_, expected_processes_); - if (arrived_processes_ == expected_processes_) { - cond_->notify_all(); - mutex_->unlock(); - arrived_processes_ = 0; - return -1; + kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self(); + + if (MC_is_active() || MC_record_replay_is_active()) { // Split in 2 simcalls for transition persistency + kernel::actor::BarrierObserver lock_observer{issuer, mc::Transition::Type::BARRIER_ASYNC_LOCK, pimpl_}; + auto acquisition = + kernel::actor::simcall_answered([issuer, this] { return pimpl_->acquire_async(issuer); }, &lock_observer); + + kernel::actor::BarrierObserver wait_observer{issuer, mc::Transition::Type::BARRIER_WAIT, acquisition.get()}; + return kernel::actor::simcall_blocking([issuer, acquisition] { acquisition->wait_for(issuer, -1); }, + &wait_observer); + + } else { // Do it in one simcall only + kernel::activity::BarrierAcquisitionImpl* acqui = nullptr; // unused here, but must be typed to pick the right ctor + kernel::actor::BarrierObserver observer{issuer, mc::Transition::Type::BARRIER_WAIT, acqui}; + return kernel::actor::simcall_blocking([issuer, this] { pimpl_->acquire_async(issuer)->wait_for(issuer, -1); }, + &observer); } +} - cond_->wait(mutex_); - mutex_->unlock(); - return 0; +std::string Barrier::to_string() const +{ + return pimpl_->to_string(); } -} // namespace s4u -} // namespace simgrid + +void intrusive_ptr_add_ref(Barrier* barrier) +{ + intrusive_ptr_add_ref(barrier->pimpl_); +} + +void intrusive_ptr_release(Barrier* barrier) +{ + intrusive_ptr_release(barrier->pimpl_); +} +} // namespace simgrid::s4u /* **************************** Public C interface *************************** */ sg_bar_t sg_barrier_init(unsigned int count) { - return new simgrid::s4u::Barrier(count); + simgrid::s4u::BarrierPtr bar = simgrid::s4u::Barrier::create(count); + intrusive_ptr_add_ref(bar.get()); + return bar.get(); } /** @brief Initializes a barrier, with count elements */ void sg_barrier_destroy(sg_bar_t bar) { - delete bar; + intrusive_ptr_release(bar); } -/** @brief Performs a barrier already initialized */ +/** @brief Performs a barrier already initialized. + * + * @return 0 for all actors but one: exactly one actor will get SG_BARRIER_SERIAL_THREAD as a return value. */ int sg_barrier_wait(sg_bar_t bar) { - return bar->wait(); + if (bar->wait()) + return SG_BARRIER_SERIAL_THREAD; + return 0; }