aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LuaSL_threads.c
blob: 4a7397e0035360f3d690d8f68d4e7059d4647288 (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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/* This code is heavily based on luaproc.
 *
 * The luaproc copyright notice and license is -

 ***************************************************

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.

 ****************************************************
 *
 * Additions and changes Copyright 2012 by David Seikel, using the above license.
 */


/* This is a redesign of luaproc.  The design goals and notes -
 *
 * In general use EFL where it is useful.
 * One fixed unique message channel per script.
 *   No need for channel.c / .h, we are not using that sort of arbitrary channels.
 *   FIFO queue on message channels, seems the C socket queue is not enough.
 *     On the other hand, could just peel messages of the socket queue, then shove them on the scripts queue.
 *   Probably one fixed unique message channel per object, which each script in the object shares.
 *     But might be better to handle that C side anyway.
 * Better integration with LuaSL.
 * Use ecore threads instead of raw pthreads.
 *   Ecore threads pretty much wraps pthreads on posix, but has Windows support to.
 * Merge in the edje Lua code, and keep an eye on that, coz we might want to actually add this to edje Lua in the future.
 * Use my coding standards, or EFL ones.  Pffft.
 *
 */

#include "LuaSL.h"


#define CHANNEL_MAX_NAME_LENGTH 255

#define CHANNEL_DESTROYED 0

/* ready process queue insertion status */
#define LUAPROC_SCHED_QUEUE_PROC_OK	0
#define LUAPROC_SCHED_QUEUE_PROC_ERR	-1

/* scheduler default number of worker threads */
#define LUAPROC_SCHED_DEFAULT_WORKER_THREADS	1

/* process is idle */
#define LUAPROC_STAT_IDLE		0
/* process is ready to run */
#define LUAPROC_STAT_READY		1
/* process is blocked on send */
#define LUAPROC_STAT_BLOCKED_SEND	2
/* process is blocked on receive */
#define LUAPROC_STAT_BLOCKED_RECV	3


/* message channel */
struct stchannel {
	Eina_Clist send;
	Eina_Clist recv;
	pthread_mutex_t *mutex;
	pthread_cond_t *in_use;
};

typedef struct
{
    Eina_Clist node;
    lua_State *L;
} recycled;

/*********
* globals
*********/

/* global channel lua_State mutex */
pthread_mutex_t mutex_channel_lstate = PTHREAD_MUTEX_INITIALIZER;

/* global where channels will be stored */
Eina_Hash *channels;

/* ready process list */
Eina_Clist lpready;

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

/* channel operations mutex */
pthread_mutex_t mutex_channel = PTHREAD_MUTEX_INITIALIZER;

/* recycle list mutex */
pthread_mutex_t mutex_recycle_list = PTHREAD_MUTEX_INITIALIZER;

/* recycled lua process list */
Eina_Clist recyclelp;


/******************************
* library functions prototypes
******************************/
/* send a message to a lua process */
static int luaproc_send( lua_State *L );
/* receive a message from a lua process */
static int luaproc_receive( lua_State *L );
/* create a new channel */
static int luaproc_create_channel( lua_State *L );
/* send a message back to the main loop */
static int luaproc_send_back( lua_State *L );

/* luaproc function registration array - main (parent) functions */
static const struct luaL_reg luaproc_funcs_parent[] = {
	{ "sendback", luaproc_send_back },
	{ NULL, NULL }
};

/* luaproc function registration array - newproc (child) functions */
static const struct luaL_reg luaproc_funcs_child[] = {
	{ "send", luaproc_send },
	{ "receive", luaproc_receive },
	{ "newchannel", luaproc_create_channel },
	{ "sendback", luaproc_send_back },
	{ NULL, NULL }
};


/* queue a lua process sending a message without a matching receiver */
static void luaproc_queue_sender(script *lp)
{
    eina_clist_add_tail(&(lp->chan->send), &(lp->node));
}

/* dequeue a lua process sending a message with a receiver match */
static script *luaproc_dequeue_sender(channel chan)
{
    script *lp;

    if ((lp = (script *) eina_clist_head(&(chan->send))))
	eina_clist_remove(&(lp->node));

    return lp;
}

/* queue a luc process receiving a message without a matching sender */
static void luaproc_queue_receiver(script *lp)
{
    eina_clist_add_tail(&(lp->chan->recv), &(lp->node));
}

/* dequeue a lua process receiving a message with a sender match */
static script *luaproc_dequeue_receiver(channel chan)
{
    script *lp;

    if ((lp = (script *) eina_clist_head(&(chan->recv))))
	eina_clist_remove(&(lp->node));

    return lp;
}

/* unlock access to a channel */
static void luaproc_unlock_channel(channel chan)
{
    /* get exclusive access to operate on channels */
    pthread_mutex_lock(&mutex_channel);
    /* unlock channel access */
    pthread_mutex_unlock(chan->mutex);
    /* signal channel not in use */
    pthread_cond_signal(chan->in_use);
    /* free access to operate on channels */
    pthread_mutex_unlock(&mutex_channel);
}

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

/* decrease active lua process count */
static 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);
}

/* worker thread main function */
static void *workermain( void *args ) {

	script *lp;
	int procstat;

	/* 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 (( eina_clist_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 */
		if ((lp = (script *) eina_clist_head(&lpready)))
		    eina_clist_remove(&(lp->node));
		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(lp->L, lp->args);

		/* reset the process argument count */
		lp->args = 0;

		/* check if process finished its whole execution, then recycle it */
		if (procstat == 0)
		{
		    recycled *trash = malloc(sizeof(recycled));

		    if (trash)
		    {
			trash->L = lp->L;
			pthread_mutex_lock(&mutex_recycle_list);
			eina_clist_add_tail(&recyclelp, &(trash->node));
			pthread_mutex_unlock(&mutex_recycle_list);
			sched_lpcount_dec();
		    }
		    lua_close(lp->L);
//		    if (lp->timer)
//			ecore_timer_del(lp->timer);
		    free(lp);
		}

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

			/* if so, further check if yield originated from an unmatched send/recv operation */
			if ( lp->status == LUAPROC_STAT_BLOCKED_SEND ) {
				/* queue blocked lua process on corresponding channel */
				luaproc_queue_sender( lp );
				/* unlock channel access */
				luaproc_unlock_channel(lp->chan);
			}

			else if ( lp->status == LUAPROC_STAT_BLOCKED_RECV ) {
				/* queue blocked lua process on corresponding channel */
				luaproc_queue_receiver( lp );
				/* unlock channel access */
				luaproc_unlock_channel(lp->chan);
			}

			/* 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 */
				eina_clist_add_tail(&lpready, &(lp->node));
				/* 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 {
			/* print error message */
			fprintf( stderr, "close lua_State (error: %s)\n", luaL_checkstring(lp->L, -1 ));
			/* close lua state */
			lua_close(lp->L);
			/* decrease active lua process count */
			sched_lpcount_dec();
		}
	}
}

/* move process to ready queue (ie, schedule process) */
static int sched_queue_proc( script *lp ) {

	/* get exclusive access to the ready process queue */
	pthread_mutex_lock( &mutex_queue_access );

	/* add process to ready queue */
	eina_clist_add_tail(&lpready, &(lp->node));

	lp->status = 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;
}

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

// 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 */
//	pthread_exit( NULL );

	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;
}



void newProc(const char *code, int file, script *lp)
{
    int ret;
    recycled *trash;

    // Try to recycle a Lua state, otherwise create one from scratch.
    pthread_mutex_lock(&mutex_recycle_list);
    /* pop list head */
    if ((trash = (recycled *) eina_clist_head(&recyclelp)))
    {
	eina_clist_remove(&(trash->node));
	lp->L = trash->L;
	free(trash);
    }
    pthread_mutex_unlock(&mutex_recycle_list);

    if (NULL == lp->L)
    {
	lp->L = luaL_newstate();

	/* store the script struct in its own Lua state */
	lua_pushlightuserdata(lp->L, lp);
	lua_setfield(lp->L, LUA_REGISTRYINDEX, "_SELF");
	luaL_openlibs(lp->L);
	luaL_register(lp->L, "luaproc", luaproc_funcs_child);
    }

    lp->status = LUAPROC_STAT_IDLE;
    lp->args = 0;
    lp->chan = NULL;
    eina_clist_element_init(&(lp->node));
    eina_clist_init(&(lp->messages));

    /* load process' code */
    if (file)
	ret = luaL_loadfile(lp->L, code);
    else
	ret = luaL_loadstring(lp->L, code);

    /* in case of errors, destroy Lua process */
    if (ret != 0)
    {
	lua_close(lp->L);
	lp->L = NULL;
    }

    if (lp->L)
    {
	sched_lpcount_inc();

	/* schedule luaproc */
	if (sched_queue_proc(lp) != LUAPROC_SCHED_QUEUE_PROC_OK)
	{
	    printf( "[luaproc] error queueing Lua process\n" );
	    sched_lpcount_dec();
	    lua_close(lp->L);
	}
    }
}

/* moves values between lua states' stacks */
static void luaproc_movevalues( lua_State *Lfrom, lua_State *Lto ) {

	int i;
	int n = lua_gettop( Lfrom );

	/* move values between lua states' stacks */
	for ( i = 2; i <= n; i++ ) {
		lua_pushstring( Lto, lua_tostring( Lfrom, i ));
	}
}

/* return the lua process associated with a given lua state */
static script *luaproc_getself(lua_State *L)
{
    script *lp;

    lua_getfield(L, LUA_REGISTRYINDEX, "_SELF");
    lp = (script *) lua_touserdata(L, -1);
    lua_pop(L, 1);
    return lp;
}

/* create a new channel */
static int luaproc_create_channel(lua_State *L)
{
    const char *name = luaL_checkstring(L, 1);
    channel chan;

    /* get exclusive access to operate on channels */
    pthread_mutex_lock(&mutex_channel);

    /* check if channel exists */
    if (eina_hash_find(channels, name) != NULL)
    {
	/* free access to operate on channels */
	pthread_mutex_unlock(&mutex_channel);
	/* return an error to lua */
	lua_pushnil(L);
	lua_pushstring(L, "channel already exists");
	return 2;
    }

    /* get exclusive access to the channel table */
    pthread_mutex_lock(&mutex_channel_lstate);

    /* create a new channel */
    chan = (channel) calloc(1, sizeof(struct stchannel));
    eina_clist_init(&(chan->send));
    eina_clist_init(&(chan->recv));
    chan->mutex  = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
    pthread_mutex_init( chan->mutex, NULL );
    chan->in_use = (pthread_cond_t *) malloc(sizeof(pthread_cond_t));
    pthread_cond_init(chan->in_use, NULL);
    eina_hash_add(channels, name, chan);

    /* let others access the channel table */
    pthread_mutex_unlock(&mutex_channel_lstate);

    /* free access to operate on channels */
    pthread_mutex_unlock(&mutex_channel);

    lua_pushboolean(L, TRUE);
    return 1;
}

/* send a message to the client process */
static int luaproc_send_back( lua_State *L ) {

	script *self;
	const char *message = luaL_checkstring( L, 1 );

	self = luaproc_getself(L);
	if (self)
	{
	    scriptMessage *sm = calloc(1, sizeof(scriptMessage));

	    if (sm)
	    {
		eina_clist_element_init(&(sm->node));
		sm->script = self;
		strcpy((char *) sm->message, message);
		ecore_main_loop_thread_safe_call_async(scriptSendBack, sm);
	    }
	}

	return 0;
}

/* error messages for the sendToChannel function */
const char *sendToChannelErrors[] =
{
    "non-existent channel",
    "error scheduling process"
};

/* send a message to a lua process */
const char *sendToChannel(gameGlobals *game, const char *chname, const char *message, script **dst, channel *chn)
{
    const char *result = NULL;
    channel chan;
    script *dstlp;
    scriptMessage *sm = NULL;

    /* get exclusive access to operate on channels */
    pthread_mutex_lock(&mutex_channel);

    // Add the message to the queue.
    if ((dstlp = eina_hash_find(game->scripts, chname)))
    {
	if ((sm = malloc(sizeof(scriptMessage))))
	{
	    sm->script = dstlp;
	    strcpy((char *) sm->message, message);
	    eina_clist_add_tail(&(sm->script->messages), &(sm->node));
	}
    }

    /* wait until channel is not in use */
    while( ((chan = eina_hash_find(channels, chname)) != NULL) && (pthread_mutex_trylock(chan->mutex) != 0 ))
    {
	pthread_cond_wait(chan->in_use, &mutex_channel);
    }

    /* free access to operate on channels */
    pthread_mutex_unlock(&mutex_channel);

    /* if channel is not found, return an error */
    if (chan == NULL)
	return sendToChannelErrors[0];

    /* try to find a matching receiver */
    dstlp = luaproc_dequeue_receiver(chan);

    /* if a match is found, send the message to it and (queue) wake it */
    if (dstlp != NULL)
    {
	scriptMessage *msg = (scriptMessage *) eina_clist_head(&(dstlp->messages));

	// See if there's a message on the queue.  Note, this may not be the same as the incoming message, if there was already a queue.
	if (msg)
	{
	    eina_clist_remove(&(msg->node));
	    message = msg->message;
	}
	/* push the message onto the receivers stack */
	lua_pushstring(dstlp->L, message);
	dstlp->args = lua_gettop(dstlp->L) - 1;
	if (msg)
	    free(msg);

	if (sched_queue_proc(dstlp) != LUAPROC_SCHED_QUEUE_PROC_OK)
	{
	    /* unlock channel access */
	    luaproc_unlock_channel(chan);

	    /* decrease active luaproc count */
	    sched_lpcount_dec();

	    /* close lua_State */
	    lua_close(dstlp->L);
	    return sendToChannelErrors[1];
	}

	/* unlock channel access */
	luaproc_unlock_channel(chan);
    }
    else if (dst)
	*dst = dstlp;

    if (chn)
	chn = &chan;
    return result;
}

/* send a message to a lua process */
static int luaproc_send( lua_State *L ) {

	channel chan;
	script *dstlp, *self = luaproc_getself(L);
	const char *chname = luaL_checkstring( L, 1 );
	const char *message = luaL_checkstring( L, 2 );
	const char *result = sendToChannel(self->game, chname, message, &dstlp, &chan);

	if (result) {
		lua_pushnil( L );
		lua_pushstring( L, result );
		return 2;
	}

	if ( dstlp == NULL ) {

		if ( self != NULL ) {
			self->status = LUAPROC_STAT_BLOCKED_SEND;
			self->chan = chan;
		}

		/* just yield the lua process, channel unlocking will be done by the scheduler */
		return lua_yield( L, lua_gettop( L ));
	}

	lua_pushboolean( L, TRUE );
	return 1;
}

/* receive a message from a lua process */
static int luaproc_receive( lua_State *L ) {

	channel chan;
	script *srclp, *self;
	const char *chname = luaL_checkstring( L, 1 );
	scriptMessage *msg;

	// First check if there are queued messages, and grab one.
	self = luaproc_getself(L);
	if ((msg = (scriptMessage *) eina_clist_head(&(self->messages))))
	{
	    eina_clist_remove(&(msg->node));
	    lua_pushstring(L, msg->message);
	    free(msg);
	    return lua_gettop(L) - 1;
	}

	/* get exclusive access to operate on channels */
	pthread_mutex_lock( &mutex_channel );

	/* wait until channel is not in use */
	while((( chan = eina_hash_find(channels, chname)) != NULL ) && ( pthread_mutex_trylock(chan->mutex) != 0 )) {
		pthread_cond_wait(chan->in_use, &mutex_channel );
	}

	/* free access to operate on channels */
	pthread_mutex_unlock( &mutex_channel );

	/* if channel is not found, free access to operate on channels and return an error to Lua */
	if ( chan == NULL ) {
		lua_pushnil( L );
		lua_pushstring( L, "non-existent channel" );
		return 2;
	}

	/* try to find a matching sender */
	srclp = luaproc_dequeue_sender( chan );

	/* if a match is found, get values from it and (queue) wake it */
	if ( srclp != NULL ) {

		/* move values between Lua states' stacks */
		luaproc_movevalues( srclp->L, L );

		/* return to sender indicanting message was sent */
		lua_pushboolean( srclp->L, TRUE );
		srclp->args = 1;

		if ( sched_queue_proc( srclp ) != LUAPROC_SCHED_QUEUE_PROC_OK ) {

			/* unlock channel access */
			luaproc_unlock_channel( chan );

			/* decrease active luaproc count */
			sched_lpcount_dec();

			/* close lua_State */
			lua_close( srclp->L );
			lua_pushnil( L );
			lua_pushstring( L, "error scheduling process" );
			return 2;
		}

		/* unlock channel access */
		luaproc_unlock_channel( chan );

		return lua_gettop( L ) - 1;
	}

	/* otherwise queue (block) the receiving process (sync) or return immediatly (async) */
	else {

		/* if trying an asynchronous receive, unlock channel access and return an error */
		if ( lua_toboolean( L, 2 )) {
			/* unlock channel access */
			luaproc_unlock_channel( chan );
			/* return an error */
			lua_pushnil( L );
			lua_pushfstring( L, "no senders waiting on channel %s", chname );
			return 2;
		}

		/* otherwise (synchronous receive) simply block process */
		else {

			if ( self != NULL ) {
				self->status = LUAPROC_STAT_BLOCKED_RECV;
				self->chan = chan;
			}

			/* just yield the lua process, channel unlocking will be done by the scheduler */
			return lua_yield( L, lua_gettop( L ));
		}
	}
}

void luaprocInit(void)
{
    eina_clist_init(&recyclelp);

    int tid;
    pthread_t worker;

    eina_clist_init(&lpready);
    channels = eina_hash_string_superfast_new(NULL);

    /* create initial worker threads */
    for (tid = 0; tid < LUAPROC_SCHED_DEFAULT_WORKER_THREADS; tid++)
    {
	pthread_create( &worker, NULL, workermain, NULL);
    }
}