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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/***************************************************
Copyright 2008 Alexandre Skyrme, Noemi Rodriguez, Roberto Ierusalimschy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*****************************************************
[sched.c]
****************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "list.h"
#include "sched.h"
#include "luaproc.h"
#include "channel.h"
#define TRUE 1
#define FALSE 0
/*********
* globals
*********/
/* ready process list */
list lpready = NULL;
/* ready process queue access mutex */
pthread_mutex_t mutex_queue_access = PTHREAD_MUTEX_INITIALIZER;
/* wake worker up conditional variable */
pthread_cond_t cond_wakeup_worker = PTHREAD_COND_INITIALIZER;
/* active luaproc count access mutex */
pthread_mutex_t mutex_lp_count = PTHREAD_MUTEX_INITIALIZER;
/* no active luaproc conditional variable */
pthread_cond_t cond_no_active_lp = PTHREAD_COND_INITIALIZER;
/* number of active luaprocs */
int lpcount = 0;
/* no more lua processes flag */
int no_more_processes = FALSE;
/* worker thread main function */
void *workermain( void *args ) {
node n;
luaproc lp;
int procstat;
int destroyworker;
/* detach thread so resources are freed as soon as thread exits (no further joining) */
pthread_detach( pthread_self( ));
//printf("NEW WORKER\n");
/* main worker loop */
while ( 1 ) {
//printf("a\n");
/* get exclusive access to the ready process queue */
pthread_mutex_lock( &mutex_queue_access );
/* wait until instructed to wake up (because there's work to do or because its time to finish) */
while (( list_node_count( lpready ) == 0 ) && ( no_more_processes == FALSE )) {
pthread_cond_wait( &cond_wakeup_worker, &mutex_queue_access );
}
////printf("b\n");
/* pop the first node from the ready process queue */
n = list_pop_head( lpready );
////printf("c\n");
/* ensure list pop succeeded before proceeding */
if ( n != NULL ) {
//printf("c.0\n");
/* get the popped node's data content (ie, the lua process struct) */
lp = (luaproc )list_data( n );
}
else {
////printf("c.1\n");
/* free access to the process ready queue */
pthread_mutex_unlock( &mutex_queue_access );
/* finished thread */
//printf("c.2 pthread_exit\n");
pthread_exit( NULL );
//printf("c.3\n");
}
////printf("d\n");
/* free access to the process ready queue */
pthread_mutex_unlock( &mutex_queue_access );
//printf("e lua_resum\n");
/* execute the lua code specified in the lua process struct */
procstat = lua_resume( luaproc_get_state( lp ), luaproc_get_args( lp ));
//printf("f\n");
/* reset the process argument count */
luaproc_set_args( lp, 0 );
////printf("g\n");
/* check if process finished its whole execution */
if ( procstat == 0 ) {
//printf("g.0\n");
/* destroy the corresponding list node */
list_destroy_node( n );
////printf("g.1\n");
/* check if worker thread should be destroyed */
destroyworker = luaproc_get_destroyworker( lp );
////printf("g.2 proc finished\n");
/* set process status to finished */
luaproc_set_status( lp, LUAPROC_STAT_FINISHED );
////printf("g.3\n");
/* check if lua process should be recycled and, if not, destroy it */
if ( luaproc_recycle_push( lp ) == FALSE ) {
//printf("g.3.0 lua_close\n");
lua_close( luaproc_get_state( lp ));
}
////printf("g.4\n");
/* decrease active lua process count */
sched_lpcount_dec();
////printf("g.5\n");
/* check if thread should be finished after lua process conclusion */
if ( destroyworker ) {
//printf("g.5.0 pthread_exit\n");
/* if so, finish thread */
pthread_exit( NULL );
}
//printf("g.6\n");
}
/* check if process yielded */
else if ( procstat == LUA_YIELD ) {
//printf("??????????????h.0\n");
/* if so, further check if yield originated from an unmatched send/recv operation */
if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_SEND ) {
//printf("??????????????h.1\n");
/* queue blocked lua process on corresponding channel */
luaproc_queue_sender( lp );
/* unlock channel access */
luaproc_unlock_channel( luaproc_get_channel( lp ));
/* destroy node (but not the associated Lua process) */
list_destroy_node( n );
}
else if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_RECV ) {
//printf("??????????????h.2\n");
/* queue blocked lua process on corresponding channel */
luaproc_queue_receiver( lp );
/* unlock channel access */
luaproc_unlock_channel( luaproc_get_channel( lp ));
/* destroy node (but not the associated Lua process) */
list_destroy_node( n );
}
/* or if yield resulted from an explicit call to coroutine.yield in the lua code being executed */
else {
//printf("??????????????h.3\n");
/* get exclusive access to the ready process queue */
pthread_mutex_lock( &mutex_queue_access );
/* re-insert the job at the end of the ready process queue */
list_add( lpready, n );
/* free access to the process ready queue */
pthread_mutex_unlock( &mutex_queue_access );
}
}
/* check if there was any execution error (LUA_ERRRUN, LUA_ERRSYNTAX, LUA_ERRMEM or LUA_ERRERR) */
else {
//printf("??????????????i.0\n");
/* destroy the corresponding node */
list_destroy_node( n );
/* print error message */
fprintf( stderr, "close lua_State (error: %s)\n", luaL_checkstring( luaproc_get_state( lp ), -1 ));
/* close lua state */
lua_close( luaproc_get_state( lp ));
/* decrease active lua process count */
sched_lpcount_dec();
}
//printf("END\n");
}
}
/* local scheduler initialization */
int sched_init_local( int numworkers ) {
int tid;
int workercount = 0;
pthread_t worker;
/* initialize ready process list */
lpready = list_new();
/* initialize channels */
channel_init();
/* create initial worker threads */
for ( tid = 0; tid < numworkers; tid++ ) {
if ( pthread_create( &worker, NULL, workermain, NULL ) == 0 ) {
workercount++;
}
}
if ( workercount != numworkers ) {
return LUAPROC_SCHED_INIT_ERROR;
}
return LUAPROC_SCHED_OK;
}
/* exit scheduler */
void sched_exit( void ) {
/* get exclusive access to the ready process queue */
pthread_mutex_lock( &mutex_queue_access );
/* destroy the ready process list */
list_destroy( lpready );
/* free access to the process ready queue */
pthread_mutex_unlock( &mutex_queue_access );
}
/* move process to ready queue (ie, schedule process) */
int sched_queue_proc( luaproc lp ) {
/* get exclusive access to the ready process queue */
pthread_mutex_lock( &mutex_queue_access );
/* add process to ready queue */
if ( list_add( lpready, list_new_node( lp )) != NULL ) {
/* set process status to ready */
luaproc_set_status( lp, LUAPROC_STAT_READY );
/* wake worker up */
pthread_cond_signal( &cond_wakeup_worker );
/* free access to the process ready queue */
pthread_mutex_unlock( &mutex_queue_access );
return LUAPROC_SCHED_QUEUE_PROC_OK;
}
else {
/* free access to the process ready queue */
pthread_mutex_unlock( &mutex_queue_access );
return LUAPROC_SCHED_QUEUE_PROC_ERR;
}
}
/* synchronize worker threads */
void sched_join_workerthreads( void ) {
////printf(" 0\n");
pthread_mutex_lock( &mutex_lp_count );
//printf(" 1 wait for procs to end\n");
/* wait until there is no more active lua processes */
while( lpcount != 0 ) {
//printf(" 1.0\n");
pthread_cond_wait( &cond_no_active_lp, &mutex_lp_count );
}
/* get exclusive access to the ready process queue */
////printf(" 2\n");
pthread_mutex_lock( &mutex_queue_access );
/* set the no more active lua processes flag to true */
////printf(" 3\n");
no_more_processes = TRUE;
/* wake ALL workers up */
//printf(" 4 wake up all workers.\n");
pthread_cond_broadcast( &cond_wakeup_worker );
/* free access to the process ready queue */
////printf(" 5\n");
pthread_mutex_unlock( &mutex_queue_access );
// We don't need this, as we only get here during shutdown. Linking this to EFL results in a hang otherwise anyway.
/* wait for (join) worker threads */
//printf(" 6 pthread_exit, waiting for workers to end\n");
// pthread_exit( NULL );
//printf("7\n");
pthread_mutex_unlock( &mutex_lp_count );
//printf("8\n");
}
/* increase active lua process count */
void sched_lpcount_inc( void ) {
//printf("inc procs++++++++++++++++++++++++++++++++++++++++\n");
pthread_mutex_lock( &mutex_lp_count );
lpcount++;
pthread_mutex_unlock( &mutex_lp_count );
}
/* decrease active lua process count */
void sched_lpcount_dec( void ) {
//printf("dec procs----------------------------------------\n");
pthread_mutex_lock( &mutex_lp_count );
lpcount--;
/* if count reaches zero, signal there are no more active processes */
if ( lpcount == 0 ) {
//printf("dec procs AND NONE LEFT000000000000000000000000000\n");
pthread_cond_signal( &cond_no_active_lp );
}
pthread_mutex_unlock( &mutex_lp_count );
}
/* create a new worker pthread */
int sched_create_worker( void ) {
pthread_t worker;
/* create a new pthread */
if ( pthread_create( &worker, NULL, workermain, NULL ) != 0 ) {
return LUAPROC_SCHED_PTHREAD_ERROR;
}
return LUAPROC_SCHED_OK;
}
|