aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/luaproc/sched.c
blob: f19beeb120ff8b8bb81182f99cd76067104036bc (plain)
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
/***************************************************

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( ));

	/* main worker loop */
	while ( 1 ) {

		/* 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 );
		}

		/* pop the first node from the ready process queue */
		n = list_pop_head( lpready );

		/* ensure list pop succeeded before proceeding */
		if ( n != NULL ) {
			/* get the popped node's data content (ie, the lua process struct) */
			lp = (luaproc )list_data( n );
		}
		else {
			/* free access to the process ready queue */
			pthread_mutex_unlock( &mutex_queue_access );
			/* finished thread */
			pthread_exit( NULL );
		}

		/* free access to the process ready queue */
		pthread_mutex_unlock( &mutex_queue_access );

		/* execute the lua code specified in the lua process struct */
		procstat = lua_resume( luaproc_get_state( lp ), luaproc_get_args( lp ));

		/* reset the process argument count */
		luaproc_set_args( lp, 0 );

		/* check if process finished its whole execution */
		if ( procstat == 0 ) {

			/* destroy the corresponding list node */
			list_destroy_node( n );

			/* check if worker thread should be destroyed */
			destroyworker = luaproc_get_destroyworker( lp );

			/* set process status to finished */
			luaproc_set_status( lp, LUAPROC_STAT_FINISHED );

			/* check if lua process should be recycled and, if not, destroy it */
			if ( luaproc_recycle_push( lp ) == FALSE ) {
				lua_close( luaproc_get_state( lp ));
			}

			/* decrease active lua process count */
			sched_lpcount_dec();

			/* check if thread should be finished after lua process conclusion */
			if ( destroyworker ) {
				/* if so, finish thread */
				pthread_exit( NULL );
			}
		}

		/* check if process yielded */
		else if ( procstat == LUA_YIELD ) {

			/* if so, further check if yield originated from an unmatched send/recv operation */
			if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_SEND ) {
				/* 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 ) {
				/* 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 {
				/* 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 {
			/* 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();
		}
	}
}

/* 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 ) {

	pthread_mutex_lock( &mutex_lp_count );

	/* wait until there is no more active lua processes */
	while( lpcount != 0 ) {
		pthread_cond_wait( &cond_no_active_lp, &mutex_lp_count );
	}
	/* get exclusive access to the ready process queue */
	pthread_mutex_lock( &mutex_queue_access );
	/* set the no more active lua processes flag to true */
	no_more_processes = TRUE;
	/* wake ALL workers up */
	pthread_cond_broadcast( &cond_wakeup_worker );
	/* free access to the process ready queue */
	pthread_mutex_unlock( &mutex_queue_access );
	/* wait for (join) worker threads */
	pthread_exit( NULL );

	pthread_mutex_unlock( &mutex_lp_count );

}

/* increase active lua process count */
void sched_lpcount_inc( void ) {
	pthread_mutex_lock( &mutex_lp_count );
	lpcount++;
	pthread_mutex_unlock( &mutex_lp_count );
}

/* decrease active lua process count */
void sched_lpcount_dec( void ) {
	pthread_mutex_lock( &mutex_lp_count );
	lpcount--;
	/* if count reaches zero, signal there are no more active processes */
	if ( lpcount == 0 ) {
		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;
}