1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
* Copyright © 2015 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Ted Gould <ted.gould@canonical.com>
*/
#pragma once
#include <future>
#include <mutex>
#include <thread>
#include <gio/gio.h>
namespace GLib
{
class ContextThread
{
std::thread _thread;
std::shared_ptr<GMainContext> _context;
std::shared_ptr<GMainLoop> _loop;
std::shared_ptr<GCancellable> _cancel;
std::function<void(void)> afterLoop_;
std::shared_ptr<std::once_flag> afterFlag_;
public:
ContextThread(std::function<void()> beforeLoop = [] {}, std::function<void()> afterLoop = [] {});
~ContextThread();
void quit();
bool isCancelled();
std::shared_ptr<GCancellable> getCancellable();
void executeOnThread(std::function<void()> work);
template <typename T>
auto executeOnThread(std::function<T()> work) -> T
{
if (std::this_thread::get_id() == _thread.get_id())
{
/* Don't block if we're on the same thread */
return work();
}
std::promise<T> promise;
std::function<void()> magicFunc = [&promise, &work]() {
try
{
promise.set_value(work());
}
catch (...)
{
promise.set_exception(std::current_exception());
}
};
executeOnThread(magicFunc);
auto future = promise.get_future();
future.wait();
return future.get();
}
void timeout(const std::chrono::milliseconds& length, std::function<void()> work);
template <class Rep, class Period>
void timeout(const std::chrono::duration<Rep, Period>& length, std::function<void()> work)
{
return timeout(std::chrono::duration_cast<std::chrono::milliseconds>(length), work);
}
void timeoutSeconds(const std::chrono::seconds& length, std::function<void()> work);
template <class Rep, class Period>
void timeoutSeconds(const std::chrono::duration<Rep, Period>& length, std::function<void()> work)
{
return timeoutSeconds(std::chrono::duration_cast<std::chrono::seconds>(length), work);
}
private:
void simpleSource(std::function<GSource*()> srcBuilder, std::function<void()> work);
};
}
|