forked from oxygine/oxygine-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadDispatcher.h
176 lines (139 loc) · 4.29 KB
/
ThreadDispatcher.h
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
#pragma once
#include "oxygine-include.h"
#include <vector>
#include <functional>
#if OX_CPP11THREADS
#include <mutex>
#include <condition_variable>
#else
#if defined(_WIN32) && !defined(__MINGW32__)
typedef struct pthread_mutex_t_* pthread_mutex_t;
typedef struct pthread_cond_t_* pthread_cond_t;
#else
# include "pthread.h"
#endif
#endif
namespace oxygine
{
#if OX_CPP11THREADS
class MutexPthreadLock
{
public:
MutexPthreadLock(std::unique_lock<std::mutex>& l, bool lock = true);
~MutexPthreadLock();
protected:
std::unique_lock<std::mutex>& _lock;
bool _locked;
};
#else
class MutexPthreadLock
{
public:
MutexPthreadLock(pthread_mutex_t& m, bool lock = true);
~MutexPthreadLock();
protected:
pthread_mutex_t& _mutex;
bool _locked;
};
#endif
/*
class TDMessage
{
public:
TDMessage& withMSGID(int id) { msgid = id; return *this; }
TDMessage& withArgs(void* Arg1, void* Arg2 = 0) { arg1 = Arg1; arg2 = Arg2; return *this; }
TDMessage& withHighPriority() { hp = true; return *this; }
TDMessage& withReply() { reply = true; return *this; }
int msgid;
void* arg1;
void* arg2;
bool hp;
bool reply;
protected:
};*/
/**Messages Queue used for communication between threads*/
class ThreadDispatcher
{
public:
struct message;
typedef void (*callback)(const message& m);
struct message
{
message(): msgid(0), arg1(0), arg2(0), cb(0), cbData(0), _id(0), need_reply(false) {}
int msgid;
void* arg1;
void* arg2;
callback cb;
void* cbData;
#ifndef __S3E__
std::function< void() > cbFunction;
#endif
unsigned int _id;
bool need_reply;
};
struct peekMessage: public message
{
peekMessage() : num(-1) {}
int num;
};
ThreadDispatcher();
~ThreadDispatcher();
bool empty();
size_t size();
//blocking
void wait();
//blocking
void get(message& ev);
bool peek(peekMessage& ev, bool del);
void clear();
//blocking, sends message and waiting reply from other thread
void* send(int msgid, void* arg1, void* arg2);
//blocking, sends callback and waiting until it is done
void* sendCallback(void* arg1, void* arg2, callback cb, void* cbData, bool highPriority = false);
#ifndef __S3E__
//blocking, sends callback and waiting until it is done
void sendCallback(const std::function<void()>&);
#endif
//async, sends post message
void post(int msgid, void* arg1, void* arg2);
//async, sends post callback
void postCallback(int msgid, void* arg1, void* arg2, callback cb, void* cbData);
void postCallback(void* arg1, void* arg2, callback cb, void* cbData);
#ifndef __S3E__
//async, sends post callback
void postCallback(const std::function<void()>&);
#endif
//remove scheduled postCallback
void removeCallback(int msgid, callback cb, void* cbData);
void reply(void* val);
//void post(TDMessage&);
//void send(TDMessage&);
std::vector<message>& lockMessages();
void unlockMessages();
private:
void _waitMessage();
void _waitReply(int id);
void _runCallbacks();
void _pushMessage(message&);
void _pushMessageWaitReply(message&, bool highPriority = false);
void _popMessage(message&);
void _popMessageNoCB(message&);
void _replyLast(void* val);
#if OX_CPP11THREADS
// TODO: This should be a std::recursive_mutex considering the original code but std::condition_variable only supports a normal std::mutex
std::mutex _mutexInternal;
std::unique_lock<std::mutex> _mutex;
std::condition_variable _cond;
#else
pthread_mutex_t _mutex;
pthread_cond_t _cond;
#endif
typedef std::vector<message> messages;
messages _events;
message _last;
void* _result;
unsigned int _id;
int _replyingTo;
};
typedef ThreadDispatcher ThreadMessages;
}