-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageLoop.cpp
More file actions
275 lines (222 loc) · 5.64 KB
/
MessageLoop.cpp
File metadata and controls
275 lines (222 loc) · 5.64 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "StdAfx.h"
#include "MessageLoop.h"
#include <list>
#include "Task.h"
#include <assert.h>
class Fifo {
public:
Fifo(void) {
::InitializeCriticalSection(&cs_);
}
virtual ~Fifo(void) {
{
::EnterCriticalSection(&cs_);
TASKFIFO::iterator it = task_fifo_.begin();
for (; it != task_fifo_.end(); ++ it) {
delete (*it);
}
task_fifo_.clear();
::LeaveCriticalSection(&cs_);
}
::DeleteCriticalSection(&cs_);
}
void AddTask(ITask* task, DWORD time_out = 0) {
if (!task) {
return ;
}
FifoElement* element = new FifoElement(task, time_out);
{
::EnterCriticalSection(&cs_);
task_fifo_.push_back(element);
::LeaveCriticalSection(&cs_);
}
}
void DispatchTask() {
if (task_fifo_.empty()) {
return ;
}
FifoElement* element = NULL;
::EnterCriticalSection(&cs_);
{
element = task_fifo_.front();
task_fifo_.pop_front();
if (element->spend_time_ > 0 &&
(GetTickCount() - element->generate_time_) < element->spend_time_)
{ // If this task is delay task, we put it in fifo back
task_fifo_.push_back(element);
element = NULL;
}
}
::LeaveCriticalSection(&cs_);
if (element && element->task_) {
element->task_->Run();
delete element;
element = NULL;
}
}
void DispatchDelayTask() {
if (task_fifo_.empty()) {
return ;
}
FifoElement* element = NULL;
::EnterCriticalSection(&cs_);
do {
element = task_fifo_.front();
task_fifo_.pop_front();
if (element->spend_time_ <= 0)
{ // If this task is delay task, we put it in fifo back
task_fifo_.push_back(element);
element = NULL;
}
else if ((GetTickCount() - element->generate_time_) < element->spend_time_){
LOG(ERR) << __FUNCTION__ << L"delay time not correct!";
break;
}
else {
break;
}
} while (1);
::LeaveCriticalSection(&cs_);
if (element && element->task_) {
element->task_->Run();
delete element;
element = NULL;
}
}
protected:
class FifoElement {
public:
FifoElement(ITask* task, DWORD time_out)
: task_(task), spend_time_(time_out) {
if (task) {
task->AddRef();
}
generate_time_ = GetTickCount();
}
virtual ~FifoElement() {
if (task_) {
task_->Release();
}
}
ITask* task_;
DWORD spend_time_;
DWORD generate_time_;
};
protected:
typedef std::list<FifoElement*> TASKFIFO;
TASKFIFO task_fifo_;
CRITICAL_SECTION cs_;
};
////////////////////////////////////////////////////////////////////////
// Call back task
class CallBackTask : public ITask {
public:
CallBackTask(VoidCallBack* call_back)
: call_back_(call_back) {
}
virtual ~CallBackTask() {
delete call_back_;
}
void Run() {
if (call_back_) {
call_back_->Exec();
}
}
void Abort() {}
protected:
VoidCallBack* call_back_;
};
////////////////////////////////////////////////////////////////////////
//
static const int kMsgHaveWork = WM_USER + 1;
MessageLoopBase::MessageLoopBase()
: running_(false)
, fifo_(new Fifo()) {
LOG(INFO) << __FUNCTION__;
}
MessageLoopBase::~MessageLoopBase() {
LOG(INFO) << __FUNCTION__;
if (fifo_) {
delete fifo_;
fifo_ = NULL;
}
}
void MessageLoopBase::PostTask(ITask* task, DWORD time_out) {
fifo_->AddTask(task, time_out);
}
void MessageLoopBase::PostTask(VoidCallBack* call_back, DWORD time_out) {
CallBackTask* task = new CallBackTask(call_back);
fifo_->AddTask(task, time_out);
}
void MessageLoopBase::RunLoop() {
running_ = true;
while (running_) {
fifo_->DispatchTask();
Sleep(50);
}
}
void MessageLoopBase::Quit() {
running_ = false;
}
///////////////////////////////////////////////////////////////////////////////
// MessageLoopUI
MessageLoopUI::MessageLoopUI() : hwnd_(NULL), timer_id_offset_(0) {
LOG(INFO) << __FUNCTION__;
}
MessageLoopUI::~MessageLoopUI() {
LOG(INFO) << __FUNCTION__;
}
void MessageLoopUI::PostTask(ITask* task, DWORD time_out) {
MessageLoopBase::PostTask(task, time_out);
if (time_out <= 0) {
PostMessage(hwnd_, kMsgHaveWork, 0, 0);
}
else {
++ timer_id_offset_;
::SetTimer(hwnd_, timer_id_offset_, time_out, NULL);
delay_taskid_set_.insert(timer_id_offset_);
}
}
void MessageLoopUI::PostTask(VoidCallBack* call_back, DWORD time_out) {
CallBackTask* task = new CallBackTask(call_back);
MessageLoopUI::PostTask(task, time_out);
}
void MessageLoopUI::OnWindowMessage(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
if (!hwnd_) {
return ;
}
if (hwnd_ != hwnd) {
assert(false);
return ;
}
if (message == kMsgHaveWork) {
fifo_->DispatchTask();
}
else if (message == WM_TIMER) {
fifo_->DispatchDelayTask();
long id = static_cast<long>(wParam);
if (id != 0) {
::KillTimer(hwnd, id);
}
TimerIDSet::iterator it = delay_taskid_set_.find(id);
if (it != delay_taskid_set_.end()) {
delay_taskid_set_.erase(it);
}
}
}
void MessageLoopUI::RunLoop() {
running_ = true;
MSG msg = { 0 };
// Main message loop:
for (;;) {
BOOL ret = ::GetMessage(&msg, NULL, 0, 0);
if (ret) {
// Dispatch window message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!running_) {
break;
}
}
}