Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI] Assert: host_speed must be > 0, not >= 0.
[simgrid.git] / src / s4u / s4u_Barrier.cpp
1 /* Copyright (c) 2018. 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 <exception>
7 #include <mutex>
8
9 #include <xbt/ex.hpp>
10 #include <xbt/log.hpp>
11
12 #include "simgrid/s4u/Barrier.hpp"
13 #include "simgrid/simix.h"
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_barrier, "S4U barrier");
16
17 namespace simgrid {
18 namespace s4u {
19
20 Barrier::Barrier(unsigned int expected_processes) : mutex_(Mutex::create()), cond_(ConditionVariable::create()), expected_processes_(expected_processes)
21 {
22 }
23
24 /**
25  * Wait functions
26  */
27 int Barrier::wait()
28 {
29   mutex_->lock();
30   arrived_processes_++;
31   XBT_DEBUG("waiting %p %u/%u", this, arrived_processes_, expected_processes_);
32   if (arrived_processes_ == expected_processes_) {
33     cond_->notify_all();
34     mutex_->unlock();
35     arrived_processes_ = 0;
36     return -1;
37   }
38
39   cond_->wait(mutex_);
40   mutex_->unlock();
41   return 0;
42 }
43 } // namespace s4u
44 } // namespace simgrid