Skip to content

Commit 188c35a

Browse files
gixStephanTLavavej
andauthored
Fix reliability issues in the parallelize tool (#1632)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 87152f4 commit 188c35a

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tools/inc/stljobs.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ inline handle<invalid_handle_value_policy> create_file(LPCWSTR lpFileName, DWORD
123123
return result;
124124
}
125125

126+
inline handle<null_handle_policy> create_event(
127+
LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName) {
128+
handle<null_handle_policy> result{CreateEventW(lpEventAttributes, bManualReset, bInitialState, lpName)};
129+
if (!result) {
130+
api_failure("CreateEventW");
131+
}
132+
133+
return result;
134+
}
135+
126136
inline handle<invalid_handle_value_policy> create_named_pipe(LPCWSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode,
127137
DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut,
128138
LPSECURITY_ATTRIBUTES lpSecurityAttributes) {
@@ -305,14 +315,17 @@ struct output_collecting_pipe {
305315
writeHandle = create_file(pipeNameBuffer, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, &inheritSa, OPEN_EXISTING,
306316
FILE_FLAG_OVERLAPPED, HANDLE{});
307317

318+
readEvent = create_event(nullptr, TRUE, FALSE, nullptr);
319+
overlapped.hEvent = readEvent.get();
320+
308321
readIo = tp_io{std::move(readHandle), callback, this, nullptr};
309322

310323
start();
311324
}
312325

313326
~output_collecting_pipe() noexcept {
314327
if (readIo) {
315-
if (running.load()) {
328+
if (running.exchange(false)) { // prevent callback() from calling read_some()
316329
if (!CancelIoEx(readIo.get_file(), &overlapped)) {
317330
api_failure("CancelIoEx"); // slams into noexcept
318331
}
@@ -413,6 +426,7 @@ struct output_collecting_pipe {
413426
std::string targetBuffer; // if running, owned by a threadpool thread, otherwise owned by the calling thread
414427
size_t validTill{};
415428
handle<invalid_handle_value_policy> writeHandle;
429+
handle<null_handle_policy> readEvent;
416430
tp_io readIo;
417431
OVERLAPPED overlapped{};
418432
};

0 commit comments

Comments
 (0)