1 /* Copyright (c) 2018-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 <simgrid/barrier.h>
7 #include <simgrid/s4u/Barrier.hpp>
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(s4u_barrier, s4u, "S4U barrier");
15 /** @brief Create a new barrier
19 BarrierPtr Barrier::create(unsigned int expected_actors)
21 return BarrierPtr(new Barrier(expected_actors));
24 /** @brief Block the current actor until all expected actors reach the barrier.
26 * This method is meant to be somewhat consistent with the pthread_barrier_wait function.
28 * @return 0 for all actors but one: exactly one actor will get SG_BARRIER_SERIAL_THREAD as a return value.
34 XBT_DEBUG("waiting %p %u/%u", this, arrived_actors_, expected_actors_);
35 if (arrived_actors_ == expected_actors_) {
39 return SG_BARRIER_SERIAL_THREAD;
47 void intrusive_ptr_add_ref(Barrier* barrier)
50 barrier->refcount_.fetch_add(1, std::memory_order_relaxed);
53 void intrusive_ptr_release(Barrier* barrier)
56 if (barrier->refcount_.fetch_sub(1, std::memory_order_release) == 1) {
57 std::atomic_thread_fence(std::memory_order_acquire);
62 } // namespace simgrid
64 /* **************************** Public C interface *************************** */
66 sg_bar_t sg_barrier_init(unsigned int count)
68 return new simgrid::s4u::Barrier(count);
71 /** @brief Initializes a barrier, with count elements */
72 void sg_barrier_destroy(const_sg_bar_t bar)
77 /** @brief Performs a barrier already initialized */
78 int sg_barrier_wait(sg_bar_t bar)