OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
Barrier.cpp
Go to the documentation of this file.
1 #include <ossim/base/Barrier.h>
2 
4 : m_maxCount(maxCount),
5  m_blockedCount(0),
6  m_waitCount(0)
7 {
8 
9 }
10 
12 {
13  reset();
14 }
15 
17 {
18  std::unique_lock<std::mutex> lock(m_mutex);
19  ++m_blockedCount;
20  if(m_blockedCount < m_maxCount)
21  {
22  ++m_waitCount;
23  m_conditionalBlock.wait(lock, [this]{return m_blockedCount>=m_maxCount;} );
24  --m_waitCount;
25  }
26  else
27  {
28  m_conditionalBlock.notify_all();
29  }
30  // always notify the conditional wait just in case anyone is waiting
31  m_conditionalWait.notify_all();
32 }
33 
35 {
36  std::unique_lock<std::mutex> lock(m_mutex);
37  // force the condition on any waiting threads
38  m_blockedCount = m_maxCount;
39  if(m_waitCount.load() > 0){
40  m_conditionalBlock.notify_all();
41  // wait until the wait count goes back to zero
42  m_conditionalWait.wait(lock, [this]{return m_waitCount.load()<1;});
43  }
44  // should be safe to reset everything at this point
45  m_blockedCount = 0;
46  m_waitCount = 0;
47 }
48 
50 {
51  // all threads should be released after this call
52  reset();
53  {
54  // now safe to update the new max count
55  std::unique_lock<std::mutex> lock(m_mutex);
56  m_maxCount = maxCount;
57  }
58 }
59 
61 {
62  std::lock_guard<std::mutex> lock(m_mutex);
63  return m_maxCount;
64 }
65 
67 {
68  std::lock_guard<std::mutex> lock(m_mutex);
69  return m_blockedCount;
70 }
71 
72 
void block()
block will block the thread based on a wait condition.
Definition: Barrier.cpp:16
ossim_int32 getBlockedCount() const
Definition: Barrier.cpp:66
~Barrier()
Destructor will reset and release all blocked threads.
Definition: Barrier.cpp:11
Barrier(ossim_int32 n)
Constructor.
Definition: Barrier.cpp:3
void reset()
Will reset the barrier to the original values.
Definition: Barrier.cpp:34
ossim_int32 getMaxCount() const
Definition: Barrier.cpp:60
int ossim_int32