0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Barrier.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
29 #ifndef Common_Barrier_h
30 #define Common_Barrier_h
31 
32 #include <condition_variable>
33 #include <mutex>
34 
35 namespace Hypertable {
36 
44 class Barrier {
45  public:
46  Barrier() { }
47 
52  void enter() {
53  std::unique_lock<std::mutex> lock(m_mutex);
54  m_unblocked_cond.wait(lock, [this](){ return !m_hold; });
55  m_counter++;
56  }
57 
61  void exit() {
62  std::lock_guard<std::mutex> lock(m_mutex);
63  HT_ASSERT(m_counter > 0);
64  m_counter--;
65  if (m_hold && m_counter == 0)
66  m_quiesced_cond.notify_one();
67  }
68 
73  void put_up() {
74  std::unique_lock<std::mutex> lock(m_mutex);
75  m_unblocked_cond.wait(lock, [this](){ return !m_hold; });
76  m_hold = true;
77  m_quiesced_cond.wait(lock, [this](){ return m_counter == 0; });
78  }
79 
83  void take_down() {
84  std::lock_guard<std::mutex> lock(m_mutex);
85  m_hold = false;
86  m_unblocked_cond.notify_all();
87  }
88 
94  public:
99  ScopedActivator(Barrier &barrier) : m_barrier(barrier) {
100  m_barrier.put_up();
101  }
102 
106  }
107 
108  private:
111  };
112 
113  private:
116 
118  std::condition_variable m_unblocked_cond;
119 
121  std::condition_variable m_quiesced_cond;
122 
124  bool m_hold {};
125 
127  uint32_t m_counter {};
128 };
129 
132 }
133 
134 #endif // Common_Barrier_h
static std::mutex mutex
Definition: Logger.cc:43
bool m_hold
True if the barrier is up.
Definition: Barrier.h:124
Barrier & m_barrier
A reference to the Barrier.
Definition: Barrier.h:110
A Barrier to block execution of code.
Definition: Barrier.h:44
void exit()
Leaves the critical section; will wake up/notify waiting threads if necessary.
Definition: Barrier.h:61
std::condition_variable m_unblocked_cond
Condition to wait for when barrier is up.
Definition: Barrier.h:118
uint32_t m_counter
Number of threads that have enter()ed (but not exit()ed) the code.
Definition: Barrier.h:127
#define HT_ASSERT(_e_)
Definition: Logger.h:396
~ScopedActivator()
Destructor; "takes down" the barrier.
Definition: Barrier.h:104
std::condition_variable m_quiesced_cond
Condition to wait for to take the barrier down.
Definition: Barrier.h:121
void enter()
Enters the critical section.
Definition: Barrier.h:52
Hypertable definitions
ScopedActivator(Barrier &barrier)
Constructor; "puts up" the barrier.
Definition: Barrier.h:99
std::mutex m_mutex
Mutex to lock access to the members and conditions.
Definition: Barrier.h:115
void put_up()
"Puts" the barrier "up".
Definition: Barrier.h:73
void take_down()
"Takes" the barrier "down"; all threads waiting in enter() are allowed to continue.
Definition: Barrier.h:83
A helper class to put up a barrier when entering a scope and take it down when leaving the scope...
Definition: Barrier.h:93