Mir
signal.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 or 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Christopher James Halse Rogers <christopher.halse.rogers@canonical.com>
17  */
18 
19 #ifndef MIR_TEST_SIGNAL_H_
20 #define MIR_TEST_SIGNAL_H_
21 
22 #include <gmock/gmock.h>
23 
24 #include <condition_variable>
25 #include <chrono>
26 #include <mutex>
27 
28 namespace mir
29 {
30 namespace test
31 {
35 class Signal
36 {
37 public:
38  void raise();
39  bool raised();
40 
41  void wait();
42  template<typename rep, typename period>
43  bool wait_for(std::chrono::duration<rep, period> delay)
44  {
45  std::unique_lock<decltype(mutex)> lock(mutex);
46  return cv.wait_for(lock, delay, [this]() { return signalled; });
47  }
48  template<class Clock, class Duration>
49  bool wait_until(std::chrono::time_point<Clock, Duration> const& time)
50  {
51  std::unique_lock<decltype(mutex)> lock(mutex);
52  return cv.wait_until(lock, time, [this]() { return signalled; });
53  }
54 
55  void reset();
56 
57 private:
58  std::mutex mutex;
59  std::condition_variable cv;
60  bool signalled{false};
61 };
62 
63 ACTION_P(ReturnFalseAndWakeUp, signal)
64 {
65  signal->raise();
66  return false;
67 }
68 ACTION_P(WakeUp, signal)
69 {
70  signal->raise();
71 }
72 ACTION_P2(WakeUpWhenZero, signal, atomic_int)
73 {
74  if (atomic_int->fetch_sub(1) == 1)
75  {
76  signal->raise();
77  }
78 }
79 
80 }
81 }
82 
83 #endif // MIR_TEST_SIGNAL_H_
AutoUnblockThread is a helper thread class that can gracefully shutdown at destruction time...
Definition: sw_splash.h:26
bool wait_until(std::chrono::time_point< Clock, Duration > const &time)
Definition: signal.h:49
bool wait_for(std::chrono::duration< rep, period > delay)
Definition: signal.h:43
ACTION_P2(WakeUpWhenZero, signal, atomic_int)
Definition: signal.h:72
ACTION_P(ReturnFalseAndWakeUp, signal)
Definition: signal.h:63
A threadsafe, waitable signal.
Definition: signal.h:35

Copyright © 2012-2018 Canonical Ltd.
Generated on Tue Feb 20 03:16:44 UTC 2018