0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TableMutatorCompletionCounter.h
Go to the documentation of this file.
1 /* -*- c++ -*-
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 
22 #ifndef Hypertable_Lib_TableMutatorCompletionCounter_h
23 #define Hypertable_Lib_TableMutatorCompletionCounter_h
24 
25 #include <Common/Time.h>
26 
27 #include <condition_variable>
28 #include <mutex>
29 
30 namespace Hypertable {
31 
41  public:
43 
44  void set(size_t count) {
45  std::unique_lock<std::mutex> lock(m_mutex);
46  m_outstanding = count;
47  m_done = (m_outstanding == 0) ? true : false;
48  m_errors = m_retries = false;
49  }
50 
51  void decrement() {
52  std::unique_lock<std::mutex> lock(m_mutex);
54  m_outstanding--;
55 
56  if (m_outstanding == 0) {
57  m_done = true;
58  m_cond.notify_all();
59  }
60  }
61 
62  bool wait_for_completion(Timer &timer) {
63  std::unique_lock<std::mutex> lock(m_mutex);
64 
65  timer.start();
66 
67  auto expire_time = std::chrono::system_clock::now() +
68  std::chrono::milliseconds(timer.remaining());
69 
70  if (!m_cond.wait_until(lock, expire_time,
71  [this](){ return m_outstanding == 0; }))
73 
74  return !(m_retries || m_errors);
75  }
76 
78  std::unique_lock<std::mutex> lock(m_mutex);
79  m_cond.wait(lock, [this](){ return m_outstanding == 0; });
80  return !(m_retries || m_errors);
81  }
82 
83  void set_retries() { m_retries = true; }
84 
85  void set_errors() { m_errors = true; }
86 
87  bool has_retries() { return m_retries; }
88 
89  bool has_errors() { return m_errors; }
90 
91  void clear_errors() { m_errors = false; }
92 
93  bool is_complete() { return m_done; }
94 
95  private:
97  std::condition_variable m_cond;
98  size_t m_outstanding {};
99  bool m_retries {};
100  bool m_errors {};
101  bool m_done {};
102  };
103 
104 }
105 
106 #endif // Hypertable_Lib_TableMutatorCompletionCounter_h
static std::mutex mutex
Definition: Logger.cc:43
Tracks outstanding RangeServer update requests.
uint32_t remaining()
Returns the remaining time till expiry.
Definition: Timer.h:101
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Time related declarations.
Hypertable definitions
void start()
Starts the timer.
Definition: Timer.h:64
A timer class to keep timeout states across AsyncComm related calls.
Definition: Timer.h:44
#define HT_THROW(_code_, _msg_)
Definition: Error.h:478