diff --git a/tools/inc/stljobs.h b/tools/inc/stljobs.h index a538a9e8456..9167cc60860 100644 --- a/tools/inc/stljobs.h +++ b/tools/inc/stljobs.h @@ -123,6 +123,16 @@ inline handle create_file(LPCWSTR lpFileName, DWORD return result; } +inline handle create_event( + LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset, BOOL bInitialState, LPCWSTR lpName) { + handle result{CreateEventW(lpEventAttributes, bManualReset, bInitialState, lpName)}; + if (!result) { + api_failure("CreateEventW"); + } + + return result; +} + inline handle create_named_pipe(LPCWSTR lpName, DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances, DWORD nOutBufferSize, DWORD nInBufferSize, DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES lpSecurityAttributes) { @@ -305,6 +315,9 @@ struct output_collecting_pipe { writeHandle = create_file(pipeNameBuffer, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0, &inheritSa, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, HANDLE{}); + readEvent = create_event(nullptr, TRUE, FALSE, nullptr); + overlapped.hEvent = readEvent.get(); + readIo = tp_io{std::move(readHandle), callback, this, nullptr}; start(); @@ -312,7 +325,7 @@ struct output_collecting_pipe { ~output_collecting_pipe() noexcept { if (readIo) { - if (running.load()) { + if (running.exchange(false)) { // prevent callback() from calling read_some() if (!CancelIoEx(readIo.get_file(), &overlapped)) { api_failure("CancelIoEx"); // slams into noexcept } @@ -413,6 +426,7 @@ struct output_collecting_pipe { std::string targetBuffer; // if running, owned by a threadpool thread, otherwise owned by the calling thread size_t validTill{}; handle writeHandle; + handle readEvent; tp_io readIo; OVERLAPPED overlapped{}; };