| 6 |
| #include <time.h> |
| 7 | #include <math.h> |
| 8 | #include <float.h> |
| 9 | #include <stdint.h> |
| 10 |
| 11 | #ifndef _WIN32 |
| 12 | #include <sys/time.h> |
| 13 | #endif |
| 14 |
| 15 | #include "MessageThread.h" |
| 16 | #include "VoIPController.h" |
| 17 | #include "logging.h" |
| 18 |
| 19 | using namespace tgvoip; |
| 20 |
| 21 | MessageThread::MessageThread() : Thread(std::bind(&MessageThread::Run, this)){ |
| 22 |
| 23 | SetName("MessageThread"); |
| 24 |
| 25 | #ifdef _WIN32 |
| 26 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 27 | event=CreateEvent(NULL, false, false, NULL); |
| 28 | #else |
| 29 | event=CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS); |
| 30 | #endif |
| 31 | #else |
| 32 | pthread_cond_init(&cond, NULL); |
| 33 | #endif |
| 34 | } |
| 35 |
| 36 | MessageThread::~MessageThread(){ |
| 37 | Stop(); |
| 38 | #ifdef _WIN32 |
| 39 | CloseHandle(event); |
| 40 | #else |
| 41 | pthread_cond_destroy(&cond); |
| 42 | #endif |
| 43 | } |
| 44 |
| 45 | void MessageThread::Stop(){ |
| 46 | if(running){ |
| 47 | running=false; |
| 48 | #ifdef _WIN32 |
| 49 | SetEvent(event); |
| 50 | #else |
| 51 | pthread_cond_signal(&cond); |
| 52 | #endif |
| 53 | Join(); |
| 54 | } |
| 55 | } |
| 56 |
| 57 | void MessageThread::Run(){ |
| 58 | queueMutex.Lock(); |
| 59 | while(running){ |
| 60 | double currentTime=VoIPController::GetCurrentTime(); |
| 61 | double waitTimeout=queue.empty() ? DBL_MAX : (queue[0].deliverAt-currentTime); |
| 62 | //LOGW("MessageThread wait timeout %f", waitTimeout); |
| 63 | if(waitTimeout>0.0){ |
| 64 | #ifdef _WIN32 |
| 65 | queueMutex.Unlock(); |
| 66 | DWORD actualWaitTimeout=waitTimeout==DBL_MAX ? INFINITE : ((DWORD)round(waitTimeout*1000.0)); |
| 67 | #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP |
| 68 | WaitForSingleObject(event, actualWaitTimeout); |
| 69 | #else |
| 70 | WaitForSingleObjectEx(event, actualWaitTimeout, false); |
| 71 | #endif |
| 72 | // we don't really care if a context switch happens here and anything gets added to the queue by another thread |
| 73 | // since any new no-delay messages will get delivered on this iteration anyway |
| 74 | queueMutex.Lock(); |
| 75 | #else |
| 76 | if(waitTimeout!=DBL_MAX){ |
| 77 | struct timeval now; |
| 78 | struct timespec timeout; |
| 79 | gettimeofday(&now, NULL); |
| 80 | waitTimeout+=now.tv_sec; |
| 81 | waitTimeout+=(now.tv_usec/1000000.0); |
| 82 | timeout.tv_sec=(time_t)(floor(waitTimeout)); |
| 83 | timeout.tv_nsec=(long)((waitTimeout-floor(waitTimeout))*1000000000.0); |
| 84 | pthread_cond_timedwait(&cond, queueMutex.NativeHandle(), &timeout); |
| 85 | }else{ |
| 86 | pthread_cond_wait(&cond, queueMutex.NativeHandle()); |
| 87 | } |
| 88 | #endif |
| 89 | } |
| 90 | if(!running){ |
| 91 | queueMutex.Unlock(); |
| 92 | return; |
| 93 | } |
| 94 | currentTime=VoIPController::GetCurrentTime(); |
| 95 | std::vector<Message> msgsToDeliverNow; |
| 96 | for(std::vector<Message>::iterator m=queue.begin();m!=queue.end();){ |
| 97 | if(m->deliverAt==0.0 || currentTime>=m->deliverAt){ |
| 98 | msgsToDeliverNow.push_back(*m); |
| 99 | m=queue.erase(m); |
| 100 | continue; |
| 101 | } |
| 102 | ++m; |
| 103 | } |
| 104 |
| 105 | for(Message& m:msgsToDeliverNow){ |
| 106 | //LOGI("MessageThread delivering %u", m.msg); |
| 107 | cancelCurrent=false; |
| 108 | if(m.deliverAt==0.0) |
| 109 | m.deliverAt=VoIPController::GetCurrentTime(); |
| 110 | if(m.func!=nullptr){ |
| 111 | m.func(); |
| 112 | } |
| 113 | if(!cancelCurrent && m.interval>0.0){ |
| 114 | m.deliverAt+=m.interval; |
| 115 | InsertMessageInternal(m); |
| 116 | } |
| 117 | } |
| 118 |
| 119 | } |
| 120 | queueMutex.Unlock(); |
| 121 | } |
| 122 |
| 123 | uint32_t MessageThread::Post(std::function<void()> func, double delay, double interval){ |
| 124 | assert(delay>=0); |
| 125 | //LOGI("MessageThread post [function] delay %f", delay); |
| 126 | if(!IsCurrent()){ |
| 127 | queueMutex.Lock(); |
| 128 | } |
| 129 | double currentTime=VoIPController::GetCurrentTime(); |
| 130 | Message m{lastMessageID++, delay==0.0 ? 0.0 : (currentTime+delay), interval, func}; |
| 131 | InsertMessageInternal(m); |
| 132 | if(!IsCurrent()){ |
| 133 | #ifdef _WIN32 |
| 134 | SetEvent(event); |
| 135 | #else |
| 136 | pthread_cond_signal(&cond); |
| 137 | #endif |
| 138 | queueMutex.Unlock(); |
| 139 | } |
| 140 | return m.id; |
| 141 | } |
| 142 |
| 143 | void MessageThread::InsertMessageInternal(MessageThread::Message &m){ |
| 144 | if(queue.empty()){ |
| 145 | queue.push_back(m); |
| 146 | }else{ |
| 147 | if(queue[0].deliverAt>m.deliverAt){ |
| 148 | queue.insert(queue.begin(), m); |
| 149 | }else{ |
| 150 | std::vector<Message>::iterator insertAfter=queue.begin(); |
| 151 | for(; insertAfter!=queue.end(); ++insertAfter){ |
| 152 | std::vector<Message>::iterator next=std::next(insertAfter); |
| 153 | if(next==queue.end() || (next->deliverAt>m.deliverAt && insertAfter->deliverAt<=m.deliverAt)){ |
| 154 | queue.insert(next, m); |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 |
| 162 | void MessageThread::Cancel(uint32_t id){ |
| 163 | if(!IsCurrent()){ |
| 164 | queueMutex.Lock(); |
| 165 | } |
| 166 |
| 167 | for(std::vector<Message>::iterator m=queue.begin();m!=queue.end();){ |
| 168 | if(m->id==id){ |
| 169 | m=queue.erase(m); |
| 170 | }else{ |
| 171 | ++m; |
| 172 | } |
| 173 | } |
| 174 |
| 175 | if(!IsCurrent()){ |
| 176 | queueMutex.Unlock(); |
| 177 | } |
| 178 | } |
| 179 |
| 180 | void MessageThread::CancelSelf(){ |
| 181 | assert(IsCurrent()); |
| 182 | cancelCurrent=true; |
| 183 | } |
| 184 |