diff options
-rw-r--r-- | libraries/luaproc/luaproc.c | 61 | ||||
-rw-r--r-- | libraries/luaproc/luaproc.h | 3 |
2 files changed, 64 insertions, 0 deletions
diff --git a/libraries/luaproc/luaproc.c b/libraries/luaproc/luaproc.c index ff18723..4770afd 100644 --- a/libraries/luaproc/luaproc.c +++ b/libraries/luaproc/luaproc.c | |||
@@ -496,6 +496,65 @@ luaproc luaproc_getself( lua_State *L ) { | |||
496 | return lp; | 496 | return lp; |
497 | } | 497 | } |
498 | 498 | ||
499 | const char *sendToChannelErrors[] = | ||
500 | { | ||
501 | "non-existent channel", | ||
502 | "error scheduling process" | ||
503 | }; | ||
504 | |||
505 | const char *sendToChannel(const char *chname, const char *message) | ||
506 | { | ||
507 | const char *result = NULL; | ||
508 | channel chan; | ||
509 | luaproc dstlp; | ||
510 | |||
511 | /* get exclusive access to operate on channels */ | ||
512 | pthread_mutex_lock(&mutex_channel); | ||
513 | |||
514 | /* wait until channel is not in use */ | ||
515 | while( ((chan = channel_search(chname)) != NULL) && (pthread_mutex_trylock(channel_get_mutex(chan)) != 0 )) | ||
516 | { | ||
517 | pthread_cond_wait(channel_get_cond(chan), &mutex_channel); | ||
518 | } | ||
519 | |||
520 | /* free access to operate on channels */ | ||
521 | pthread_mutex_unlock(&mutex_channel); | ||
522 | |||
523 | /* if channel is not found, return an error */ | ||
524 | if (chan == NULL) | ||
525 | return sendToChannelErrors[0]; | ||
526 | |||
527 | /* try to find a matching receiver */ | ||
528 | dstlp = luaproc_dequeue_receiver(chan); | ||
529 | |||
530 | /* if a match is found, send the message to it and (queue) wake it */ | ||
531 | if (dstlp != NULL) | ||
532 | { | ||
533 | /* push the message onto the receivers stack */ | ||
534 | lua_pushstring( dstlp->lstate, message); | ||
535 | |||
536 | dstlp->args = lua_gettop(dstlp->lstate) - 1; | ||
537 | |||
538 | if (sched_queue_proc(dstlp) != LUAPROC_SCHED_QUEUE_PROC_OK) | ||
539 | { | ||
540 | /* unlock channel access */ | ||
541 | luaproc_unlock_channel(chan); | ||
542 | |||
543 | /* decrease active luaproc count */ | ||
544 | sched_lpcount_dec(); | ||
545 | |||
546 | /* close lua_State */ | ||
547 | lua_close(dstlp->lstate); | ||
548 | return sendToChannelErrors[1]; | ||
549 | } | ||
550 | |||
551 | /* unlock channel access */ | ||
552 | luaproc_unlock_channel(chan); | ||
553 | } | ||
554 | |||
555 | return result; | ||
556 | } | ||
557 | |||
499 | /* send a message to a lua process */ | 558 | /* send a message to a lua process */ |
500 | static int luaproc_send( lua_State *L ) { | 559 | static int luaproc_send( lua_State *L ) { |
501 | 560 | ||
@@ -503,6 +562,8 @@ static int luaproc_send( lua_State *L ) { | |||
503 | luaproc dstlp, self; | 562 | luaproc dstlp, self; |
504 | const char *chname = luaL_checkstring( L, 1 ); | 563 | const char *chname = luaL_checkstring( L, 1 ); |
505 | 564 | ||
565 | // TODO - use the above new function to do the heavy lifting. | ||
566 | |||
506 | /* get exclusive access to operate on channels */ | 567 | /* get exclusive access to operate on channels */ |
507 | pthread_mutex_lock( &mutex_channel ); | 568 | pthread_mutex_lock( &mutex_channel ); |
508 | 569 | ||
diff --git a/libraries/luaproc/luaproc.h b/libraries/luaproc/luaproc.h index 4c71685..d700e86 100644 --- a/libraries/luaproc/luaproc.h +++ b/libraries/luaproc/luaproc.h | |||
@@ -76,6 +76,9 @@ 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 */ |
77 | void luaproc_queue_sender( luaproc lp ); | 77 | void luaproc_queue_sender( luaproc lp ); |
78 | 78 | ||
79 | |||
80 | const char *sendToChannel(const char *chname, const char *message); | ||
81 | |||
79 | /* queue a luaproc that tried to receive a message */ | 82 | /* queue a luaproc that tried to receive a message */ |
80 | void luaproc_queue_receiver( luaproc lp ); | 83 | void luaproc_queue_receiver( luaproc lp ); |
81 | 84 | ||