1.1.62
by Ubuntu daily release
Import upstream version 0.2.0+14.10.20140603.3 |
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 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 |
||
19 |
/** AutoUnblockThread is a helper thread class that can gracefully shutdown
|
|
20 |
* at destruction time. This is helpul for tests that botch create
|
|
21 |
* threads and use ASSERT macros for example (or any other condition that
|
|
22 |
* makes the test exit early). Using naked std::thread would call std::terminate
|
|
23 |
* under such conditions.
|
|
24 |
*/
|
|
25 |
||
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 |
||
37 |
class AutoUnblockThread |
|
38 |
{
|
|
39 |
public: |
|
40 |
template<typename Callable, typename... Args> |
|
41 |
explicit AutoUnblockThread(std::function<void(void)> const& unblock, |
|
42 |
Callable&& f, |
|
43 |
Args&&... args) |
|
1.1.76
by Ubuntu daily release
Import upstream version 0.7.3+14.10.20140918.1 |
44 |
: unblock{unblock}, thread(f, args...) |
1.1.62
by Ubuntu daily release
Import upstream version 0.2.0+14.10.20140603.3 |
45 |
{}
|
46 |
||
47 |
~AutoUnblockThread() |
|
48 |
{
|
|
49 |
stop(); |
|
50 |
}
|
|
51 |
||
52 |
void stop() |
|
53 |
{
|
|
54 |
unblock(); |
|
55 |
if (thread.joinable()) |
|
56 |
thread.join(); |
|
57 |
}
|
|
58 |
||
1.1.71
by Ubuntu daily release
Import upstream version 0.6.0+14.10.20140811 |
59 |
std::thread::native_handle_type native_handle() |
60 |
{
|
|
61 |
return thread.native_handle(); |
|
62 |
}
|
|
63 |
||
1.1.62
by Ubuntu daily release
Import upstream version 0.2.0+14.10.20140603.3 |
64 |
private: |
65 |
std::function<void(void)> unblock; |
|
66 |
std::thread thread; |
|
67 |
};
|
|
68 |
||
69 |
/** AutoJoinThread is a convenience AutoUnblockThread when there is
|
|
70 |
* no need for unblocking anything to join the thread.
|
|
71 |
*/
|
|
72 |
class AutoJoinThread : public AutoUnblockThread |
|
73 |
{
|
|
74 |
public: |
|
75 |
template<typename Callable, typename... Args> |
|
76 |
explicit AutoJoinThread(Callable&& f, |
|
77 |
Args&&... args) |
|
1.1.76
by Ubuntu daily release
Import upstream version 0.7.3+14.10.20140918.1 |
78 |
: AutoUnblockThread([]{}, f, args...) |
1.1.62
by Ubuntu daily release
Import upstream version 0.2.0+14.10.20140603.3 |
79 |
{}
|
80 |
};
|
|
81 |
||
82 |
}
|
|
83 |
}
|
|
84 |
#endif
|