1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 #pragma once 24 25 #ifndef __MT_EVENT_KERNEL__ 26 #define __MT_EVENT_KERNEL__ 27 28 29 namespace MT 30 { 31 // 32 // 33 // 34 class Event 35 { 36 ::MW_HANDLE eventHandle; 37 38 public: 39 40 MT_NOCOPYABLE(Event); 41 Event()42 Event() 43 { 44 static_assert(sizeof(Event) == sizeof(::MW_HANDLE), "sizeof(Event) is invalid"); 45 eventHandle = nullptr; 46 } 47 Event(EventReset::Type resetType,bool initialState)48 Event(EventReset::Type resetType, bool initialState) 49 { 50 eventHandle = nullptr; 51 Create(resetType, initialState); 52 } 53 ~Event()54 ~Event() 55 { 56 CloseHandle(eventHandle); 57 eventHandle = nullptr; 58 } 59 Create(EventReset::Type resetType,bool initialState)60 void Create(EventReset::Type resetType, bool initialState) 61 { 62 if (eventHandle != nullptr) 63 { 64 CloseHandle(eventHandle); 65 } 66 67 MW_BOOL bManualReset = (resetType == EventReset::AUTOMATIC) ? 0 : 1; 68 MW_BOOL bInitialState = initialState ? 1 : 0; 69 eventHandle = ::CreateEventW(nullptr, bManualReset, bInitialState, nullptr); 70 } 71 Signal()72 void Signal() 73 { 74 SetEvent(eventHandle); 75 } 76 Reset()77 void Reset() 78 { 79 ResetEvent(eventHandle); 80 } 81 Wait(uint32 milliseconds)82 bool Wait(uint32 milliseconds) 83 { 84 MW_DWORD res = WaitForSingleObject(eventHandle, milliseconds); 85 return (res == MW_WAIT_OBJECT_0); 86 } 87 88 }; 89 90 } 91 92 93 #endif 94