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 uint32 numOfWaitingThreads; 41 42 pthread_mutex_t mutex; 43 pthread_cond_t condition; 44 AtomicInt val; 45 EventReset::Type resetType; 46 bool isInitialized; 47 48 private: 49 50 Event(const Event&) {} 51 void operator=(const Event&) {} 52 53 void AutoResetIfNeed() 54 { 55 if (resetType == EventReset::MANUAL) 56 { 57 return; 58 } 59 Reset(); 60 } 61 62 public: 63 64 Event() 65 : numOfWaitingThreads(0) 66 , isInitialized(false) 67 { 68 } 69 70 Event(EventReset::Type resetType, bool initialState) 71 : numOfWaitingThreads(0) 72 , isInitialized(false) 73 { 74 Create(resetType, initialState); 75 } 76 77 ~Event() 78 { 79 if (isInitialized) 80 { 81 pthread_cond_destroy( &condition ); 82 pthread_mutex_destroy( &mutex ); 83 } 84 } 85 86 void Create(EventReset::Type _resetType, bool initialState) 87 { 88 MT_ASSERT (!isInitialized, "Event already initialized"); 89 90 resetType = _resetType; 91 92 pthread_mutex_init( &mutex, nullptr ); 93 pthread_cond_init( &condition, nullptr ); 94 val.Set(initialState ? SIGNALED : NOT_SIGNALED); 95 96 isInitialized = true; 97 numOfWaitingThreads = 0; 98 } 99 100 void Signal() 101 { 102 MT_ASSERT (isInitialized, "Event not initialized"); 103 104 pthread_mutex_lock( &mutex ); 105 106 val.Set(SIGNALED); 107 108 if (numOfWaitingThreads) 109 { 110 if (resetType == EventReset::MANUAL) 111 { 112 pthread_cond_broadcast( &condition ); 113 } else 114 { 115 pthread_cond_signal( &condition ); 116 } 117 } 118 119 pthread_mutex_unlock( &mutex ); 120 } 121 122 void Reset() 123 { 124 MT_ASSERT (isInitialized, "Event not initialized"); 125 val.Set(NOT_SIGNALED); 126 } 127 128 bool Wait(uint32 milliseconds) 129 { 130 MT_ASSERT (isInitialized, "Event not initialized"); 131 132 pthread_mutex_lock( &mutex ); 133 134 // early exit if event already signaled 135 if ( val.Get() != NOT_SIGNALED ) 136 { 137 AutoResetIfNeed(); 138 pthread_mutex_unlock( &mutex ); 139 return true; 140 } 141 142 numOfWaitingThreads++; 143 144 //convert milliseconds to posix time 145 struct timeval tv; 146 gettimeofday( &tv, nullptr ); 147 struct timespec tm; 148 tm.tv_sec = tv.tv_sec + milliseconds / 1000; 149 uint64 nanosec = (uint64)tv.tv_usec * 1000 + (uint64)milliseconds * 1000000; 150 if (nanosec >= 1000000000) 151 { 152 tm.tv_sec += (long)(nanosec / 1000000000); 153 nanosec = nanosec % 1000000000; 154 } 155 tm.tv_nsec = (long)nanosec; 156 157 int ret = 0; 158 while(true) 159 { 160 ret = pthread_cond_timedwait( &condition, &mutex, &tm ); 161 162 MT_ASSERT(ret == 0 || ret == ETIMEDOUT || ret == EINTR, "Unexpected return value"); 163 164 if (ret != EINTR) 165 { 166 break; 167 } 168 } 169 170 numOfWaitingThreads--; 171 172 AutoResetIfNeed(); 173 174 pthread_mutex_unlock( &mutex ); 175 176 //return true if val in SIGNALED state instead of ret == 0 ? 177 return ret == 0; 178 } 179 180 }; 181 182 } 183 184 185