-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from dend/microsoft-master
Merge latest changes
- Loading branch information
Showing
150 changed files
with
5,198 additions
and
2,585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,4 +55,9 @@ class Logger | |
{ | ||
logger->critical(fmt, args...); | ||
} | ||
|
||
static void flush() | ||
{ | ||
logger->flush(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <windows.h> | ||
#include <string> | ||
|
||
class EventLocker | ||
{ | ||
public: | ||
EventLocker(HANDLE h) | ||
{ | ||
eventHandle = h; | ||
SetEvent(eventHandle); | ||
} | ||
|
||
static std::optional<EventLocker> Get(std::wstring eventName) | ||
{ | ||
EventLocker locker(eventName); | ||
if (!locker.eventHandle) | ||
{ | ||
return {}; | ||
} | ||
|
||
return locker; | ||
} | ||
|
||
EventLocker(EventLocker& e) = delete; | ||
EventLocker& operator=(EventLocker& e) = delete; | ||
|
||
EventLocker(EventLocker&& e) noexcept | ||
{ | ||
this->eventHandle = e.eventHandle; | ||
e.eventHandle = nullptr; | ||
} | ||
|
||
EventLocker& operator=(EventLocker&& e) noexcept | ||
{ | ||
this->eventHandle = e.eventHandle; | ||
e.eventHandle = nullptr; | ||
} | ||
|
||
~EventLocker() | ||
{ | ||
if (eventHandle) | ||
{ | ||
ResetEvent(eventHandle); | ||
CloseHandle(eventHandle); | ||
eventHandle = nullptr; | ||
} | ||
} | ||
private: | ||
EventLocker(std::wstring eventName) | ||
{ | ||
eventHandle = CreateEvent(nullptr, true, false, eventName.c_str()); | ||
if (!eventHandle) | ||
{ | ||
return; | ||
} | ||
|
||
SetEvent(eventHandle); | ||
} | ||
|
||
HANDLE eventHandle; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <functional> | ||
#include <thread> | ||
#include <string> | ||
#include <windows.h> | ||
|
||
class EventWaiter | ||
{ | ||
public: | ||
EventWaiter() {} | ||
EventWaiter(const std::wstring& name, std::function<void(DWORD)> callback) | ||
{ | ||
// Create localExitThreadEvent and localWaitingEvent for capturing. We can not capture 'this' as we implement move constructor. | ||
auto localExitThreadEvent = exitThreadEvent = CreateEvent(nullptr, false, false, nullptr); | ||
HANDLE localWaitingEvent = waitingEvent = CreateEvent(nullptr, false, false, name.c_str()); | ||
std::thread([=]() { | ||
HANDLE events[2] = { localWaitingEvent, localExitThreadEvent }; | ||
while (true) | ||
{ | ||
auto waitResult = WaitForMultipleObjects(2, events, false, INFINITE); | ||
if (waitResult == WAIT_OBJECT_0 + 1) | ||
{ | ||
break; | ||
} | ||
|
||
if (waitResult == WAIT_FAILED) | ||
{ | ||
callback(GetLastError()); | ||
continue; | ||
} | ||
|
||
if (waitResult == WAIT_OBJECT_0) | ||
{ | ||
callback(ERROR_SUCCESS); | ||
} | ||
} | ||
}).detach(); | ||
} | ||
|
||
EventWaiter(EventWaiter&) = delete; | ||
EventWaiter& operator=(EventWaiter&) = delete; | ||
|
||
EventWaiter(EventWaiter&& a) noexcept | ||
{ | ||
this->exitThreadEvent = a.exitThreadEvent; | ||
this->waitingEvent = a.waitingEvent; | ||
|
||
a.exitThreadEvent = nullptr; | ||
a.waitingEvent = nullptr; | ||
} | ||
|
||
EventWaiter& operator=(EventWaiter&& a) noexcept | ||
{ | ||
this->exitThreadEvent = a.exitThreadEvent; | ||
this->waitingEvent = a.waitingEvent; | ||
|
||
a.exitThreadEvent = nullptr; | ||
a.waitingEvent = nullptr; | ||
return *this; | ||
} | ||
|
||
~EventWaiter() | ||
{ | ||
if (exitThreadEvent) | ||
{ | ||
SetEvent(exitThreadEvent); | ||
CloseHandle(exitThreadEvent); | ||
} | ||
|
||
if (waitingEvent) | ||
{ | ||
CloseHandle(waitingEvent); | ||
} | ||
} | ||
|
||
private: | ||
HANDLE exitThreadEvent = nullptr; | ||
HANDLE waitingEvent = nullptr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <functional> | ||
#include <string> | ||
#include <Windows.h> | ||
#include <thread> | ||
|
||
namespace ProcessWaiter | ||
{ | ||
void OnProcessTerminate(std::wstring parent_pid, std::function<void(DWORD)> callback) | ||
{ | ||
DWORD pid = std::stol(parent_pid); | ||
std::thread([=]() { | ||
HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, pid); | ||
if (process != nullptr) | ||
{ | ||
if (WaitForSingleObject(process, INFINITE) == WAIT_OBJECT_0) | ||
{ | ||
CloseHandle(process); | ||
callback(ERROR_SUCCESS); | ||
} | ||
else | ||
{ | ||
CloseHandle(process); | ||
callback(GetLastError()); | ||
} | ||
} | ||
else | ||
{ | ||
callback(GetLastError()); | ||
} | ||
}).detach(); | ||
} | ||
} |
Oops, something went wrong.