Skip to content

Commit a9b30d5

Browse files
authored
Merge pull request #613 from evoskuil/master
Fix window quit race, style.
2 parents 89e9b76 + 62481e2 commit a9b30d5

File tree

7 files changed

+38
-33
lines changed

7 files changed

+38
-33
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ console_bs_SOURCES = \
9696
console/executor_store.cpp \
9797
console/executor_test_reader.cpp \
9898
console/executor_test_writer.cpp \
99-
console/executor_windows.cpp \
99+
console/executor_window.cpp \
100100
console/localize.hpp \
101101
console/main.cpp \
102102
console/stack_trace.cpp \

builds/cmake/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ if (with-console)
334334
"../../console/executor_store.cpp"
335335
"../../console/executor_test_reader.cpp"
336336
"../../console/executor_test_writer.cpp"
337-
"../../console/executor_windows.cpp"
338-
"../../console/libbitcoin.ico"
337+
"../../console/executor_window.cpp"
339338
"../../console/localize.hpp"
340339
"../../console/main.cpp"
341340
"../../console/stack_trace.cpp"

builds/msvc/vs2022/bs/bs.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
<ClCompile Include="..\..\..\..\console\executor_store.cpp" />
140140
<ClCompile Include="..\..\..\..\console\executor_test_reader.cpp" />
141141
<ClCompile Include="..\..\..\..\console\executor_test_writer.cpp" />
142-
<ClCompile Include="..\..\..\..\console\executor_windows.cpp" />
142+
<ClCompile Include="..\..\..\..\console\executor_window.cpp" />
143143
<ClCompile Include="..\..\..\..\console\main.cpp" />
144144
<ClCompile Include="..\..\..\..\console\stack_trace.cpp" />
145145
</ItemGroup>

builds/msvc/vs2022/bs/bs.vcxproj.filters

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<ClCompile Include="..\..\..\..\console\executor_test_writer.cpp">
8282
<Filter>src</Filter>
8383
</ClCompile>
84-
<ClCompile Include="..\..\..\..\console\executor_windows.cpp">
84+
<ClCompile Include="..\..\..\..\console\executor_window.cpp">
8585
<Filter>src</Filter>
8686
</ClCompile>
8787
<ClCompile Include="..\..\..\..\console\main.cpp">

console/executor.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace libbitcoin {
2929
namespace server {
3030

3131
using boost::format;
32+
using namespace system;
3233
using namespace std::placeholders;
3334

3435
// static initializers.
@@ -81,6 +82,34 @@ executor::~executor()
8182
// ----------------------------------------------------------------------------
8283
// static
8384

85+
#if defined(HAVE_MSC)
86+
BOOL WINAPI executor::control_handler(DWORD signal)
87+
{
88+
switch (signal)
89+
{
90+
// Keyboard events. These prevent exit altogether when TRUE returned.
91+
// handle_stop(signal) therefore shuts down gracefully/completely.
92+
case CTRL_C_EVENT:
93+
case CTRL_BREAK_EVENT:
94+
95+
// A signal that the system sends to all processes attached to a
96+
// console when the user closes the console (by clicking Close on the
97+
// console window's window menu). Returning TRUE here does not
98+
// materially delay exit, so aside from capture this is a noop.
99+
case CTRL_CLOSE_EVENT:
100+
executor::handle_stop(possible_narrow_sign_cast<int>(signal));
101+
return TRUE;
102+
103+
////// Only services receive this (*any* user is logging off).
104+
////case CTRL_LOGOFF_EVENT:
105+
////// Only services receive this (all users already logged off).
106+
////case CTRL_SHUTDOWN_EVENT:
107+
default:
108+
return FALSE;
109+
}
110+
}
111+
#endif
112+
84113
void executor::initialize_stop()
85114
{
86115
poll_for_stopping();

console/executor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class executor
6969

7070
HWND window_{};
7171
std::thread thread_{};
72+
std::promise<bool> ready_{};
7273
#endif
7374

7475
// Executor.
Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,10 @@ namespace server {
2626
// TODO: use RegisterServiceCtrlHandlerEx for service registration.
2727

2828
using namespace system;
29-
3029
constexpr auto window_name = L"HiddenShutdownWindow";
3130
constexpr auto window_text = L"Flushing tables...";
3231
constexpr auto window_title = L"Libbitcoin Server";
3332

34-
// static
35-
BOOL WINAPI executor::control_handler(DWORD signal)
36-
{
37-
switch (signal)
38-
{
39-
// Keyboard events. These prevent exit altogether when TRUE returned.
40-
// handle_stop(signal) therefore shuts down gracefully/completely.
41-
case CTRL_C_EVENT:
42-
case CTRL_BREAK_EVENT:
43-
44-
// A signal that the system sends to all processes attached to a
45-
// console when the user closes the console (by clicking Close on the
46-
// console window's window menu). Returning TRUE here does not
47-
// materially delay exit, so aside from capture this is a noop.
48-
case CTRL_CLOSE_EVENT:
49-
executor::handle_stop(possible_narrow_sign_cast<int>(signal));
50-
return TRUE;
51-
52-
////// Only services receive this (*any* user is logging off).
53-
////case CTRL_LOGOFF_EVENT:
54-
////// Only services receive this (all users already logged off).
55-
////case CTRL_SHUTDOWN_EVENT:
56-
default:
57-
return FALSE;
58-
}
59-
}
60-
6133
// static
6234
LRESULT CALLBACK executor::window_proc(HWND handle, UINT message,
6335
WPARAM wparam, LPARAM lparam)
@@ -120,6 +92,7 @@ void executor::create_hidden_window()
12092

12193
MSG message{};
12294
BOOL result{};
95+
ready_.set_value(true);
12396
while (!is_zero(result = ::GetMessageW(&message, NULL, 0, 0)))
12497
{
12598
// fault
@@ -134,6 +107,9 @@ void executor::create_hidden_window()
134107

135108
void executor::destroy_hidden_window()
136109
{
110+
// Wait until window is accepting messages, so WM_QUIT isn't missed.
111+
ready_.get_future().wait();
112+
137113
if (!is_null(window_))
138114
::PostMessageW(window_, WM_QUIT, 0, 0);
139115

0 commit comments

Comments
 (0)