Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Attempt to move msg_bar_t to S4U
[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 count) : expected_processes_(count)
21 {
22   mutex_ = Mutex::create();
23   cond_  = ConditionVariable::create();
24 }
25
26 /**
27  * Wait functions
28  */
29 int Barrier::wait()
30 {
31   mutex_->lock();
32   arrived_processes_++;
33   XBT_DEBUG("waiting %p %u/%u", this, arrived_processes_, expected_processes_);
34   if (arrived_processes_ == expected_processes_) {
35     cond_->notify_all();
36     mutex_->unlock();
37     arrived_processes_ = 0;
38     return -1;
39   }
40
41   cond_->wait(mutex_);
42   mutex_->unlock();
43   return 0;
44 }
45 } // namespace s4u
46 } // namespace simgrid