1 /* Copyright (c) 2018-2020. 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. */
9 #include "simgrid/Exception.hpp"
10 #include "simgrid/barrier.h"
11 #include "simgrid/s4u/Barrier.hpp"
12 #include "simgrid/simix.h"
13 #include "xbt/log.hpp"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_barrier, s4u, "S4U barrier");
20 /** @brief Create a new barrier
24 BarrierPtr Barrier::create(unsigned int expected_actors)
26 return BarrierPtr(new Barrier(expected_actors));
29 /** @brief Block the current actor until all expected actors reach the barrier.
31 * This method is meant to be somewhat consistent with the pthread_barrier_wait function.
33 * @return 0 for all actors but one: exactly one actor will get SG_BARRIER_SERIAL_THREAD as a return value.
39 XBT_DEBUG("waiting %p %u/%u", this, arrived_actors_, expected_actors_);
40 if (arrived_actors_ == expected_actors_) {
44 return SG_BARRIER_SERIAL_THREAD;
52 void intrusive_ptr_add_ref(Barrier* barrier)
55 barrier->refcount_.fetch_add(1, std::memory_order_relaxed);
58 void intrusive_ptr_release(Barrier* barrier)
61 if (barrier->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
62 std::atomic_thread_fence(std::memory_order_acquire);
67 } // namespace simgrid
69 /* **************************** Public C interface *************************** */
71 sg_bar_t sg_barrier_init(unsigned int count)
73 return new simgrid::s4u::Barrier(count);
76 /** @brief Initializes a barrier, with count elements */
77 void sg_barrier_destroy(const_sg_bar_t bar)
82 /** @brief Performs a barrier already initialized */
83 int sg_barrier_wait(sg_bar_t bar)