aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libraries/luaproc/luaproc.c61
-rw-r--r--libraries/luaproc/luaproc.h2
2 files changed, 13 insertions, 50 deletions
diff --git a/libraries/luaproc/luaproc.c b/libraries/luaproc/luaproc.c
index 8f1005e..ee0764a 100644
--- a/libraries/luaproc/luaproc.c
+++ b/libraries/luaproc/luaproc.c
@@ -496,13 +496,15 @@ luaproc luaproc_getself( lua_State *L ) {
496 return lp; 496 return lp;
497} 497}
498 498
499/* error messages for the sendToChannel function */
499const char *sendToChannelErrors[] = 500const char *sendToChannelErrors[] =
500{ 501{
501 "non-existent channel", 502 "non-existent channel",
502 "error scheduling process" 503 "error scheduling process"
503}; 504};
504 505
505const char *sendToChannel(const char *chname, const char *message) 506/* send a message to a lua process */
507const char *sendToChannel(const char *chname, const char *message, luaproc *dst, channel *chn)
506{ 508{
507 const char *result = NULL; 509 const char *result = NULL;
508 channel chan; 510 channel chan;
@@ -551,7 +553,11 @@ const char *sendToChannel(const char *chname, const char *message)
551 /* unlock channel access */ 553 /* unlock channel access */
552 luaproc_unlock_channel(chan); 554 luaproc_unlock_channel(chan);
553 } 555 }
556 else if (dst)
557 dst = &dstlp;
554 558
559 if (chn)
560 chn = &chan;
555 return result; 561 return result;
556} 562}
557 563
@@ -561,59 +567,16 @@ static int luaproc_send( lua_State *L ) {
561 channel chan; 567 channel chan;
562 luaproc dstlp, self; 568 luaproc dstlp, self;
563 const char *chname = luaL_checkstring( L, 1 ); 569 const char *chname = luaL_checkstring( L, 1 );
570 const char *message = luaL_checkstring( L, 2 );
571 const char *result = sendToChannel(chname, message, &dstlp, &chan);
564 572
565// TODO - use the above new function to do the heavy lifting. 573 if (result) {
566
567 /* get exclusive access to operate on channels */
568 pthread_mutex_lock( &mutex_channel );
569
570 /* wait until channel is not in use */
571 while((( chan = channel_search( chname )) != NULL ) && ( pthread_mutex_trylock( channel_get_mutex( chan )) != 0 )) {
572 pthread_cond_wait( channel_get_cond( chan ), &mutex_channel );
573 }
574
575 /* free access to operate on channels */
576 pthread_mutex_unlock( &mutex_channel );
577
578 /* if channel is not found, return an error to Lua */
579 if ( chan == NULL ) {
580 lua_pushnil( L ); 574 lua_pushnil( L );
581 lua_pushstring( L, "non-existent channel" ); 575 lua_pushstring( L, result );
582 return 2; 576 return 2;
583 } 577 }
584 578
585 /* try to find a matching receiver */ 579 if ( dstlp == NULL ) {
586 dstlp = luaproc_dequeue_receiver( chan );
587
588 /* if a match is found, move values to it and (queue) wake it */
589 if ( dstlp != NULL ) {
590
591 /* move values between Lua states' stacks */
592 luaproc_movevalues( L, dstlp->lstate );
593
594 dstlp->args = lua_gettop( dstlp->lstate ) - 1;
595
596 if ( sched_queue_proc( dstlp ) != LUAPROC_SCHED_QUEUE_PROC_OK ) {
597
598 /* unlock channel access */
599 luaproc_unlock_channel( chan );
600
601 /* decrease active luaproc count */
602 sched_lpcount_dec();
603
604 /* close lua_State */
605 lua_close( dstlp->lstate );
606 lua_pushnil( L );
607 lua_pushstring( L, "error scheduling process" );
608 return 2;
609 }
610
611 /* unlock channel access */
612 luaproc_unlock_channel( chan );
613 }
614
615 /* otherwise queue (block) the sending process */
616 else {
617 580
618 self = luaproc_getself( L ); 581 self = luaproc_getself( L );
619 582
diff --git a/libraries/luaproc/luaproc.h b/libraries/luaproc/luaproc.h
index 2f3ed3f..a5dde0d 100644
--- a/libraries/luaproc/luaproc.h
+++ b/libraries/luaproc/luaproc.h
@@ -76,7 +76,7 @@ void luaproc_register_lib( lua_State *L );
76/* queue a luaproc that tried to send a message */ 76/* queue a luaproc that tried to send a message */
77void luaproc_queue_sender( luaproc lp ); 77void luaproc_queue_sender( luaproc lp );
78 78
79const char *sendToChannel(const char *chname, const char *message); 79const char *sendToChannel(const char *chname, const char *message, luaproc *dst, channel *chn);
80 80
81/* queue a luaproc that tried to receive a message */ 81/* queue a luaproc that tried to receive a message */
82void luaproc_queue_receiver( luaproc lp ); 82void luaproc_queue_receiver( luaproc lp );