diff options
author | David Walter Seikel | 2012-02-05 17:35:45 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-02-05 17:35:45 +1000 |
commit | 7acd25212c33eade9aa94bd9573221822b10188b (patch) | |
tree | 3419c3d073b2a92036be5f6f4b11330fc544c9f2 /libraries/luaproc/luaproc.c | |
parent | Implement the script main loop, and the script UUID, which we call SID, thoug... (diff) | |
download | SledjHamr-7acd25212c33eade9aa94bd9573221822b10188b.zip SledjHamr-7acd25212c33eade9aa94bd9573221822b10188b.tar.gz SledjHamr-7acd25212c33eade9aa94bd9573221822b10188b.tar.bz2 SledjHamr-7acd25212c33eade9aa94bd9573221822b10188b.tar.xz |
Move the heavy lifting part of luaproc message sending to it's own function.
Diffstat (limited to '')
-rw-r--r-- | libraries/luaproc/luaproc.c | 61 |
1 files changed, 61 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 | ||