-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPool.h
313 lines (235 loc) · 9.17 KB
/
ThreadPool.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* File: ThreadPool.h
* Author: maxds
*
* Created on 8 novembre 2011, 20:20
*/
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include "Thread.h"
#include "IActor.h"
#include <queue>
#include <map>
#include <auto_ptr.h>
namespace Acting
{
#ifndef STRUCT_TASK_T
#define STRUCT_TASK_T
/**
*
*/
typedef struct
{
IActor* __candidat;
//Id actor_id;
Threading::Thread tid;
} task_t;
#endif
#ifndef CLASS_THREAD_POOL
#define CLASS_THREAD_POOL
/**
* Une classe implémentant un pool de threads
*/
template <unsigned int THREAD_COUNT,unsigned int POOL_ID>
class ThreadPool {
public:
/*
* Initialise le pool de thread
*/
static int InitPool();
/*
* Ajoute un acteur dans le pool de thread
*/
static int AddItem(IActor* a);
/*
* Ferme le pool de thread
*/
static int FinalizePool();
private:
/**
* La liste des threads utilisés
*/
static Threading::Thread __threads[THREAD_COUNT];
/**
* File des acteurs en attente d'exécution par un thread
*/
static std::queue<task_t> __actors[THREAD_COUNT];
/**
* Associantion entre tid de la bibliothèque de thread et tid utilisateur
*/
static std::map<unsigned int , unsigned int> __threads_ids;
/**
* Compteur du thread elu
*/
static int __elected_thread;
/**
* Permet de choisir le thread auquel affecter le code de l'acteur
*/
static inline int ElectThread();
/**
*Un mutex pour stdout
*/
static Threading::Mutex_t __mutex_stdout;
/**
* Un mutex pour chaque file de jobs
*/
static Threading::Mutex_t __mutex_queueus[THREAD_COUNT];
/**
* Un mutex pour proteger la map associative (pththread_t,tid)
*/
static Threading::Mutex_t __mutex_map;
/**
* Une variable de condition par thread pour signaler que la file des jobs n'est plus vide
*/
static Threading::Vcondition __condition_queue_not_empty[THREAD_COUNT];
/**
* Un mutex protégeant la file de chaque thread
*/
static Threading::Mutex_t __mutex_condition_queue_not_empty[THREAD_COUNT];
/**
* Code executé par chaque thread du pool
* @return
*/
static int Loop();
};
#endif
}
#ifndef REGION_STATIC
#define REGION_STATIC
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> Threading::Thread
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__threads[THREAD_COUNT];
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> std::queue<Acting::task_t>
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__actors [THREAD_COUNT];
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> Threading::Mutex_t
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__mutex_queueus[THREAD_COUNT];
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> std::map<unsigned int , unsigned int>
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__threads_ids;
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> int
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__elected_thread;
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> Threading::Mutex_t
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__mutex_stdout;
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> Threading::Mutex_t
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__mutex_map;
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> Threading::Vcondition
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__condition_queue_not_empty[THREAD_COUNT];
template <unsigned int THREAD_COUNT ,unsigned int POOL_ID> Threading::Mutex_t
Acting::ThreadPool<THREAD_COUNT,POOL_ID>::__mutex_condition_queue_not_empty[THREAD_COUNT];
#endif
#ifndef CLASS_THREAD_POOL_IMPL
#define CLASS_THREAD_POOL_IMPL
/**
* Methode doit être parametrée
*/
template <unsigned int THREAD_COUNT,unsigned int POOL_ID>
inline int Acting::ThreadPool<THREAD_COUNT,POOL_ID>::ElectThread(){
return (__elected_thread+1) % THREAD_COUNT;
}
/**
* Initiatialisation du pool de thread
*/
template <unsigned int THREAD_COUNT,unsigned int POOL_ID>
int Acting::ThreadPool<THREAD_COUNT,POOL_ID>::InitPool(){
__elected_thread = 0;
Threading::Mutex::init(&__mutex_stdout);
Threading::Mutex::init(&__mutex_map);
/*Code d'initialisation*/
Threading::Mutex::lock(&__mutex_map);
for(int i =0;i<THREAD_COUNT;i++){
Threading::create_thread(&__threads[i],(void* (*)(void*))Loop,NULL);
Threading::Mutex::init(&__mutex_queueus[i]);
/*initialisation des vc*/
Threading::Mutex::init(&__mutex_condition_queue_not_empty[i]);
Threading::Condition::init(&__condition_queue_not_empty[i]);
//std::printf("Insertion de %d %d\n",(unsigned int)__threads[i],i);
__threads_ids[(unsigned int)__threads[i]]=i;
}
Threading::Mutex::unlock(&__mutex_map);
}
/**
* Fermeture du pool de thread
*/
template <unsigned int THREAD_COUNT,unsigned int POOL_ID>
int Acting::ThreadPool<THREAD_COUNT,POOL_ID>::FinalizePool(){
for(int i =0;i<THREAD_COUNT;i++){
Threading::join_thread(__threads[i]);
Threading::Mutex::init(&__mutex_queueus[i]);
}
Threading::Mutex::destroy(&__mutex_stdout);
}
/**
* Ajout d'une tâche dans le pool de thread
*/
template <unsigned int THREAD_COUNT,unsigned int POOL_ID>
int Acting::ThreadPool<THREAD_COUNT,POOL_ID>::AddItem(IActor* a){
task_t new_task;
new_task.__candidat = a;
//new_task.actor_id = a->GetId();
//ici l'election se fait au tourniquet
//lui preferer une méthode générique, FIFO ..
__elected_thread= (__elected_thread+1) % THREAD_COUNT;
//Ici il faudra choisir sur quel thread placer le job
//On commencera par du Round Robin
//Ici on ajoute un job à la file des traveaux
Threading::Mutex::lock(&__mutex_condition_queue_not_empty[__elected_thread]);
Threading::Mutex::lock(&__mutex_queueus[__elected_thread]);
__actors[__elected_thread].push(new_task);
Threading::Condition::signal(&__condition_queue_not_empty[__elected_thread]);
Threading::Mutex::unlock(&__mutex_queueus[__elected_thread]);
Threading::Mutex::unlock(&__mutex_condition_queue_not_empty[__elected_thread]);
}
/*
* Code exécuté par chaque thread du pool
*/
template <unsigned int THREAD_COUNT,unsigned int POOL_ID>
int Acting::ThreadPool<THREAD_COUNT,POOL_ID>::Loop(){
/**
*Recuperation de l'identifiant du thread courant
**/
Threading::Mutex::lock(&__mutex_map);
unsigned tid = __threads_ids[Threading::get_thread_id()];
Threading::Mutex::unlock(&__mutex_map);
/*********************************************************/
/**
* Boucle d'exécution du thread courant
*/
while(true){
//Ici on endort le thread tid sur la variable de condition
//__actors[tid].empty()==true
Threading::Mutex::lock(&__mutex_condition_queue_not_empty[tid]);
while(__actors[tid].empty()){
//Essayer ici d'aller voler du travail chez une autre thread
//Si pas de boulot alors se mettre se mettre en attente
Threading::Condition::wait(&__mutex_condition_queue_not_empty[tid],&__condition_queue_not_empty[tid]);
#ifdef DEBUG
//std::printf("Hello from thread %d executing actor : %d on queue %d\n",Threading::get_thread_id(),t.__candidat->GetId(),tid);
#endif
}
//Le thread va consommer sa file des travaux
while(!__actors[tid].empty()){
Threading::Mutex::lock(&__mutex_queueus[tid]);
/**
* Ici, au lieu de choisir directement l'acteur en tete,
* essayer de choisir l'acteur qui est d'envoie
*/
task_t t = __actors[tid].front();
__actors[tid].pop();
Threading::Mutex::unlock(&__mutex_queueus[tid]);
//sauvgarder le context du thread ici
actor_context* c = new actor_context;
c->__actor_id = t.__candidat->GetUserId();
//save_context(c);
//setjmp(c->__ctx);
//printf("Here is a context switch\n");
//restor_context(c);
//Execute le code de l'acteur
t.__candidat->Act();
//tester si t est un acteur d'envoie, si c'est le cas, le liberer
//Mettre en place un mécanisme qui lorsque le thread exécutant l'acteur t.__candidiat
//est préempté, qui permette de sauvegarder le contexte du thread
//De switcher de context
}
Threading::Mutex::unlock(&__mutex_condition_queue_not_empty[tid]);
}
}
#endif
#endif /* THREADPOOL_H */