2024-07-21 21:07:05 +08:00
|
|
|
#include <Windows.h>
|
2024-02-07 20:59:24 +08:00
|
|
|
class win_event
|
|
|
|
{
|
|
|
|
typedef win_event _Self;
|
|
|
|
typedef HANDLE __native_handle_type;
|
2024-07-21 21:07:05 +08:00
|
|
|
typedef const char *__native_string_type;
|
2024-02-07 20:59:24 +08:00
|
|
|
|
|
|
|
__native_handle_type _M_handle;
|
|
|
|
__native_string_type _M_name;
|
|
|
|
|
2024-07-21 21:07:05 +08:00
|
|
|
win_event(const _Self &);
|
|
|
|
_Self &operator=(const _Self &);
|
|
|
|
|
2024-02-07 20:59:24 +08:00
|
|
|
public:
|
|
|
|
typedef __native_handle_type native_handle_type;
|
|
|
|
typedef __native_string_type native_string_type;
|
|
|
|
|
|
|
|
explicit win_event(native_string_type name, bool create = true)
|
2024-07-21 21:07:05 +08:00
|
|
|
: _M_name(name)
|
2024-02-07 20:59:24 +08:00
|
|
|
{
|
|
|
|
_M_handle = create ? // lpEventAttributes, bManualReset, bInitialState, lpName
|
2024-07-21 21:07:05 +08:00
|
|
|
::CreateEventA(nullptr, TRUE, FALSE, name)
|
|
|
|
: ::OpenEventA(EVENT_ALL_ACCESS, FALSE, name); // dwDesiredAccess, bInheritHandle, lpName
|
2024-02-07 20:59:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~win_event() { ::CloseHandle(_M_handle); }
|
|
|
|
|
|
|
|
native_handle_type native_handle() const { return _M_handle; }
|
|
|
|
native_string_type native_name() const { return _M_name; }
|
|
|
|
|
|
|
|
bool valid() const { return _M_handle; }
|
|
|
|
|
|
|
|
bool signal(bool t)
|
2024-07-21 21:07:05 +08:00
|
|
|
{
|
|
|
|
return t ? ::SetEvent(_M_handle) : ::ResetEvent(_M_handle);
|
|
|
|
}
|
2024-02-07 20:59:24 +08:00
|
|
|
|
|
|
|
/// Return true only if when it is wake up by notify instead of timeout
|
|
|
|
bool wait(DWORD msec = INFINITE)
|
2024-07-21 21:07:05 +08:00
|
|
|
{
|
|
|
|
return WAIT_OBJECT_0 == ::WaitForSingleObject(_M_handle, msec);
|
|
|
|
}
|
2024-02-07 20:59:24 +08:00
|
|
|
};
|