OSVR-Core
SignalEvent.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_SignalEvent_h_GUID_DD338B8E_2CC9_43B8_0699_13169941BD2E
26 #define INCLUDED_SignalEvent_h_GUID_DD338B8E_2CC9_43B8_0699_13169941BD2E
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <stdexcept>
36 
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
39 
45 class SignalEvent {
46  public:
48  explicit SignalEvent(bool manual = false)
49  : h_(CreateEvent(nullptr, manual, false, nullptr)) {
50  if (nullptr == h_) {
51  throw std::runtime_error("Could not create Windows event handle!");
52  }
53  }
54 
56  ~SignalEvent() { CloseHandle(h_); }
58  void set() { SetEvent(h_); }
59 
61  void clear() { ResetEvent(h_); }
64  bool wait() { return WAIT_OBJECT_0 == WaitForSingleObject(h_, INFINITE); }
65 
69  bool wait(DWORD milliseconds) {
70  return WAIT_OBJECT_0 == WaitForSingleObject(h_, milliseconds);
71  }
72  SignalEvent(SignalEvent const &) = delete;
73  SignalEvent &operator=(SignalEvent const &) = delete;
74 
75  private:
76  HANDLE h_;
77 };
78 
79 #endif // INCLUDED_SignalEvent_h_GUID_DD338B8E_2CC9_43B8_0699_13169941BD2E
~SignalEvent()
Destructor.
Definition: SignalEvent.h:56
bool wait(DWORD milliseconds)
Wait for the event, with a timeout.
Definition: SignalEvent.h:69
Handy little low-level sync primitive An automatic reset event restores to "un-signalled" after relea...
Definition: SignalEvent.h:45
SignalEvent(bool manual=false)
Constructor.
Definition: SignalEvent.h:48
void clear()
Un-signal the event (not usually necessary for auto=reset events)
Definition: SignalEvent.h:61
bool wait()
Wait for the event, infinitely.
Definition: SignalEvent.h:64