Mir
auto_unblock_thread.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: Alberto Aguirre <alberto.aguirre@canonical.com>
17  */
18 
26 #ifndef MIR_TEST_AUTO_UNBLOCK_THREAD_H_
27 #define MIR_TEST_AUTO_UNBLOCK_THREAD_H_
28 
29 #include <thread>
30 #include <functional>
31 
32 namespace mir
33 {
34 namespace test
35 {
36 
38 {
39 public:
40  AutoJoinThread() = default;
41  template<typename Callable, typename... Args>
42  explicit AutoJoinThread(Callable&& f,
43  Args&&... args)
44  : thread{std::forward<Callable>(f), std::forward<Args>(args)...}
45  {}
46 
48  {
49  stop();
50  }
51 
52  void stop()
53  {
54  if (thread.joinable())
55  thread.join();
56  }
57 
58  std::thread::native_handle_type native_handle()
59  {
60  return thread.native_handle();
61  }
62 
63  AutoJoinThread(AutoJoinThread&& t) = default;
64  AutoJoinThread& operator=(AutoJoinThread&& t) = default;
65 
66 private:
67  std::thread thread;
68 };
69 
71 {
72 public:
73  AutoUnblockThread() = default;
74 
75  template<typename Callable, typename... Args>
76  explicit AutoUnblockThread(std::function<void(void)> const& unblock,
77  Callable&& f,
78  Args&&... args)
79  : AutoJoinThread{std::forward<Callable>(f), std::forward<Args>(args)...},
80  unblock{unblock}
81  {}
82 
84  {
85  stop();
86  }
87 
88  AutoUnblockThread(AutoUnblockThread&& t) = default;
90 
91  void stop()
92  {
93  if (unblock)
94  unblock();
96  }
97 
98 private:
99  std::function<void(void)> unblock;
100 };
101 
102 }
103 }
104 #endif
void stop()
Definition: auto_unblock_thread.h:91
AutoUnblockThread is a helper thread class that can gracefully shutdown at destruction time...
Definition: blob.h:26
void stop()
Definition: auto_unblock_thread.h:52
std::thread::native_handle_type native_handle()
Definition: auto_unblock_thread.h:58
Definition: auto_unblock_thread.h:70
AutoUnblockThread & operator=(AutoUnblockThread &&t)=default
Definition: auto_unblock_thread.h:37
~AutoUnblockThread()
Definition: auto_unblock_thread.h:83
AutoJoinThread(Callable &&f, Args &&... args)
Definition: auto_unblock_thread.h:42
~AutoJoinThread()
Definition: auto_unblock_thread.h:47
AutoJoinThread & operator=(AutoJoinThread &&t)=default
AutoUnblockThread(std::function< void(void)> const &unblock, Callable &&f, Args &&... args)
Definition: auto_unblock_thread.h:76

Copyright © 2012-2018 Canonical Ltd.
Generated on Sun Jun 17 06:26:29 UTC 2018