File: MessageThread.cpp

package info (click to toggle)
libtgvoip 2.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 9,004 kB
  • sloc: cpp: 61,331; ansic: 24,725; sh: 4,150; makefile: 749; asm: 242
file content (183 lines) | stat: -rwxr-xr-x 4,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Created by Grishka on 17.06.2018.
//

#include <assert.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include <stdint.h>

#ifndef _WIN32
#include <sys/time.h>
#endif

#include "MessageThread.h"
#include "VoIPController.h"
#include "logging.h"

using namespace tgvoip;

MessageThread::MessageThread() : Thread(std::bind(&MessageThread::Run, this)){

	SetName("MessageThread");

#ifdef _WIN32
#if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP
	event=CreateEvent(NULL, false, false, NULL);
#else
	event=CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS);
#endif
#else
	pthread_cond_init(&cond, NULL);
#endif
}

MessageThread::~MessageThread(){
	Stop();
#ifdef _WIN32
	CloseHandle(event);
#else
	pthread_cond_destroy(&cond);
#endif
}

void MessageThread::Stop(){
	if(running){
		running=false;
#ifdef _WIN32
		SetEvent(event);
#else
		pthread_cond_signal(&cond);
#endif
		Join();
	}
}

void MessageThread::Run(){
	queueMutex.Lock();
	while(running){
		double currentTime=VoIPController::GetCurrentTime();
		double waitTimeout=queue.empty() ? DBL_MAX : (queue[0].deliverAt-currentTime);
		//LOGW("MessageThread wait timeout %f", waitTimeout);
		if(waitTimeout>0.0){
#ifdef _WIN32
			queueMutex.Unlock();
			DWORD actualWaitTimeout=waitTimeout==DBL_MAX ? INFINITE : ((DWORD)round(waitTimeout*1000.0));
#if !defined(WINAPI_FAMILY) || WINAPI_FAMILY!=WINAPI_FAMILY_PHONE_APP
			WaitForSingleObject(event, actualWaitTimeout);
#else
			WaitForSingleObjectEx(event, actualWaitTimeout, false);
#endif
			// we don't really care if a context switch happens here and anything gets added to the queue by another thread
			// since any new no-delay messages will get delivered on this iteration anyway
			queueMutex.Lock();
#else
			if(waitTimeout!=DBL_MAX){
				struct timeval now;
				struct timespec timeout;
				gettimeofday(&now, NULL);
				waitTimeout+=now.tv_sec;
				waitTimeout+=(now.tv_usec/1000000.0);
				timeout.tv_sec=(time_t)(floor(waitTimeout));
				timeout.tv_nsec=(long)((waitTimeout-floor(waitTimeout))*1000000000.0);
				pthread_cond_timedwait(&cond, queueMutex.NativeHandle(), &timeout);
			}else{
				pthread_cond_wait(&cond, queueMutex.NativeHandle());
			}
#endif
		}
		if(!running){
			queueMutex.Unlock();
			return;
		}
		currentTime=VoIPController::GetCurrentTime();
		std::vector<Message> msgsToDeliverNow;
		for(std::vector<Message>::iterator m=queue.begin();m!=queue.end();){
			if(m->deliverAt==0.0 || currentTime>=m->deliverAt){
				msgsToDeliverNow.push_back(*m);
				m=queue.erase(m);
				continue;
			}
			++m;
		}

		for(Message& m:msgsToDeliverNow){
			//LOGI("MessageThread delivering %u", m.msg);
			cancelCurrent=false;
			if(m.deliverAt==0.0)
				m.deliverAt=VoIPController::GetCurrentTime();
			if(m.func!=nullptr){
				m.func();
			}
			if(!cancelCurrent && m.interval>0.0){
				m.deliverAt+=m.interval;
				InsertMessageInternal(m);
			}
		}

	}
	queueMutex.Unlock();
}

uint32_t MessageThread::Post(std::function<void()> func, double delay, double interval){
	assert(delay>=0);
	//LOGI("MessageThread post [function] delay %f", delay);
	if(!IsCurrent()){
		queueMutex.Lock();
	}
	double currentTime=VoIPController::GetCurrentTime();
	Message m{lastMessageID++, delay==0.0 ? 0.0 : (currentTime+delay), interval, func};
	InsertMessageInternal(m);
	if(!IsCurrent()){
#ifdef _WIN32
		SetEvent(event);
#else
		pthread_cond_signal(&cond);
#endif
		queueMutex.Unlock();
	}
	return m.id;
}

void MessageThread::InsertMessageInternal(MessageThread::Message &m){
	if(queue.empty()){
		queue.push_back(m);
	}else{
		if(queue[0].deliverAt>m.deliverAt){
			queue.insert(queue.begin(), m);
		}else{
			std::vector<Message>::iterator insertAfter=queue.begin();
			for(; insertAfter!=queue.end(); ++insertAfter){
				std::vector<Message>::iterator next=std::next(insertAfter);
				if(next==queue.end() || (next->deliverAt>m.deliverAt && insertAfter->deliverAt<=m.deliverAt)){
					queue.insert(next, m);
					break;
				}
			}
		}
	}
}

void MessageThread::Cancel(uint32_t id){
	if(!IsCurrent()){
		queueMutex.Lock();
	}

	for(std::vector<Message>::iterator m=queue.begin();m!=queue.end();){
		if(m->id==id){
			m=queue.erase(m);
		}else{
			++m;
		}
	}

	if(!IsCurrent()){
		queueMutex.Unlock();
	}
}

void MessageThread::CancelSelf(){
	assert(IsCurrent());
	cancelCurrent=true;
}