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 #include <sys/time.h> 26 #include <sched.h> 27 #include <errno.h> 28 29 30 namespace MT 31 { 32 // 33 // 34 // 35 class Event 36 { 37 static const int NOT_SIGNALED = 0; 38 static const int SIGNALED = 1; 39 40 pthread_mutex_t mutex; 41 pthread_cond_t condition; 42 AtomicInt val; 43 EventReset::Type resetType; 44 bool isInitialized; 45 46 private: 47 48 Event(const Event&) {} 49 void operator=(const Event&) {} 50 51 void AutoResetIfNeed() 52 { 53 if (resetType == EventReset::MANUAL) 54 { 55 return; 56 } 57 Reset(); 58 } 59 60 public: 61 62 Event() 63 : isInitialized(false) 64 { 65 } 66 67 Event(EventReset::Type resetType, bool initialState) 68 : isInitialized(false) 69 { 70 Create(resetType, initialState); 71 } 72 73 ~Event() 74 { 75 if (isInitialized) 76 { 77 pthread_cond_destroy( &condition ); 78 pthread_mutex_destroy( &mutex ); 79 } 80 } 81 82 void Create(EventReset::Type _resetType, bool initialState) 83 { 84 MT_ASSERT (!isInitialized, "Event already initialized"); 85 86 resetType = _resetType; 87 88 pthread_mutex_init( &mutex, nullptr ); 89 pthread_cond_init( &condition, nullptr ); 90 val.Set(initialState ? SIGNALED : NOT_SIGNALED); 91 92 isInitialized = true; 93 } 94 95 void Signal() 96 { 97 MT_ASSERT (isInitialized, "Event not initialized"); 98 99 pthread_mutex_lock( &mutex ); 100 101 val.Set(SIGNALED); 102 pthread_cond_broadcast( &condition ); 103 104 pthread_mutex_unlock( &mutex ); 105 } 106 107 void Reset() 108 { 109 MT_ASSERT (isInitialized, "Event not initialized"); 110 111 val.Set(NOT_SIGNALED); 112 } 113 114 bool Wait(uint32 milliseconds) 115 { 116 MT_ASSERT (isInitialized, "Event not initialized"); 117 118 pthread_mutex_lock( &mutex ); 119 120 // early exit if event already signaled 121 if ( val.Get() != NOT_SIGNALED ) 122 { 123 AutoResetIfNeed(); 124 pthread_mutex_unlock( &mutex ); 125 return true; 126 } 127 128 //convert milliseconds to posix time 129 struct timeval tv; 130 gettimeofday( &tv, nullptr ); 131 struct timespec tm; 132 tm.tv_sec = milliseconds / 1000 + tv.tv_sec; 133 tm.tv_nsec = ( milliseconds % 1000 ) * 1000000 + tv.tv_usec * 1000; 134 135 int ret = 0; 136 do 137 { 138 ret = pthread_cond_timedwait( &condition, &mutex, &tm ); 139 } while( ret == EINTR ); 140 141 sched_yield(); 142 143 pthread_mutex_unlock( &mutex ); 144 145 AutoResetIfNeed(); 146 return ret == 0; 147 } 148 149 }; 150 151 } 152 153 154