1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #include "td/utils/port/detail/EventFdWindows.h"
8 
9 char disable_linker_warning_about_empty_file_event_fd_windows_cpp TD_UNUSED;
10 
11 #ifdef TD_EVENTFD_WINDOWS
12 
13 #include "td/utils/logging.h"
14 
15 namespace td {
16 namespace detail {
17 
init()18 void EventFdWindows::init() {
19   auto handle = CreateEventW(nullptr, true, false, nullptr);
20   if (handle == nullptr) {
21     auto error = OS_ERROR("CreateEventW failed");
22     LOG(FATAL) << error;
23   }
24   event_ = NativeFd(handle);
25 }
26 
empty()27 bool EventFdWindows::empty() {
28   return !event_;
29 }
30 
close()31 void EventFdWindows::close() {
32   event_.close();
33 }
34 
get_pending_error()35 Status EventFdWindows::get_pending_error() {
36   return Status::OK();
37 }
38 
get_poll_info()39 PollableFdInfo &EventFdWindows::get_poll_info() {
40   UNREACHABLE();
41 }
42 
release()43 void EventFdWindows::release() {
44   if (SetEvent(event_.fd()) == 0) {
45     auto error = OS_ERROR("SetEvent failed");
46     LOG(FATAL) << error;
47   }
48 }
49 
acquire()50 void EventFdWindows::acquire() {
51   if (ResetEvent(event_.fd()) == 0) {
52     auto error = OS_ERROR("ResetEvent failed");
53     LOG(FATAL) << error;
54   }
55 }
56 
wait(int timeout_ms)57 void EventFdWindows::wait(int timeout_ms) {
58   WaitForSingleObject(event_.fd(), timeout_ms);
59   if (ResetEvent(event_.fd()) == 0) {
60     auto error = OS_ERROR("ResetEvent failed");
61     LOG(FATAL) << error;
62   }
63 }
64 
65 }  // namespace detail
66 }  // namespace td
67 
68 #endif
69