forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuiExecutor.cpp
171 lines (146 loc) · 3.27 KB
/
GuiExecutor.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2015.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Repository at: https://github.com/djeedjay/DebugViewPP/
#include "stdafx.h"
#include <cassert>
#include "Win32/Win32Lib.h"
#include "CobaltFusion/GuiExecutor.h"
#include "CobaltFusion/dbgstream.h"
namespace fusion {
namespace {
void DoCall(std::function<void ()>&& fn)
{
try
{
fn();
}
catch (std::exception& e)
{
cdbg << "Executor: exception ignored: " << e.what() << "\n";
}
catch (...)
{
cdbg << "Executor: exception ignored\n";
}
}
} // namespace
GuiExecutorBase::~GuiExecutorBase()
{
}
GuiExecutor::GuiExecutor() :
m_guiThreadId(std::this_thread::get_id()),
m_wnd(*this)
{
assert(::IsGUIThread(FALSE));
m_wnd.Create();
}
GuiExecutor::~GuiExecutor()
{
if (IsExecutorThread())
m_wnd.DestroyWindow();
else
Call([this] { m_wnd.DestroyWindow(); });
}
ScheduledCall GuiExecutor::CallAt(const TimePoint& at, std::function<void ()> fn)
{
unsigned id = GetCallId();
CallAsync([this, id, at, fn]()
{
m_scheduledCalls.Insert(GuiExecutor::CallData(id, at, fn));
ResetTimer();
});
return MakeScheduledCall(id);
}
ScheduledCall GuiExecutor::CallAfter(const Duration& interval, std::function<void ()> fn)
{
return CallAt(std::chrono::steady_clock::now() + interval, fn);
}
ScheduledCall GuiExecutor::CallEvery(const Duration& interval, std::function<void ()> fn)
{
assert(interval > Duration::zero());
unsigned id = GetCallId();
auto at = std::chrono::steady_clock::now() + interval;
CallAsync([this, id, at, interval, fn]()
{
m_scheduledCalls.Insert(GuiExecutor::CallData(id, at, interval, fn));
ResetTimer();
});
return MakeScheduledCall(id);
}
void GuiExecutor::Cancel(const ScheduledCall& call)
{
unsigned id = GetId(call);
if (IsExecutorThread())
{
m_scheduledCalls.Remove(id);
ResetTimer();
}
else
{
Call([this, id]()
{
m_scheduledCalls.Remove(id);
ResetTimer();
});
}
}
bool GuiExecutor::IsExecutorThread() const
{
return std::this_thread::get_id() == m_guiThreadId;
}
bool GuiExecutor::IsIdle() const
{
return m_q.Empty();
}
void GuiExecutor::Synchronize()
{
Call([] {});
}
void GuiExecutor::OnMessage()
{
assert(IsExecutorThread());
while (!m_q.Empty())
DoCall(m_q.Pop());
}
void GuiExecutor::OnTimer()
{
ResetTimer();
}
void GuiExecutor::ResetTimer()
{
assert(IsExecutorThread());
m_wnd.ClearTimer();
while (!m_scheduledCalls.IsEmpty())
{
auto now = std::chrono::steady_clock::now();
auto at = m_scheduledCalls.NextDeadline();
if (at <= now)
{
DoCall(m_scheduledCalls.Pop().fn);
}
else
{
m_wnd.SetTimerMs(static_cast<unsigned>(std::chrono::duration_cast<std::chrono::milliseconds>(at - now).count()));
break;
}
}
}
bool GuiWaitFor(std::function<bool ()> pred)
{
while (!pred())
{
MSG msg;
switch (GetMessage(&msg, nullptr, 0, 0 ))
{
case -1: Win32::ThrowLastError("GetMessage");
case 0: return false;
default: break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return true;
}
} // namespace fusion