From 7acd25212c33eade9aa94bd9573221822b10188b Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sun, 5 Feb 2012 17:35:45 +1000 Subject: Move the heavy lifting part of luaproc message sending to it's own function. --- libraries/luaproc/luaproc.c | 61 +++++++++++++++++++++++++++++++++++++++++++++ libraries/luaproc/luaproc.h | 3 +++ 2 files changed, 64 insertions(+) (limited to 'libraries') 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 ) { return lp; } +const char *sendToChannelErrors[] = +{ + "non-existent channel", + "error scheduling process" +}; + +const char *sendToChannel(const char *chname, const char *message) +{ + const char *result = NULL; + channel chan; + luaproc dstlp; + + /* get exclusive access to operate on channels */ + pthread_mutex_lock(&mutex_channel); + + /* wait until channel is not in use */ + while( ((chan = channel_search(chname)) != NULL) && (pthread_mutex_trylock(channel_get_mutex(chan)) != 0 )) + { + pthread_cond_wait(channel_get_cond(chan), &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) + { + /* push the message onto the receivers stack */ + lua_pushstring( dstlp->lstate, message); + + dstlp->args = lua_gettop(dstlp->lstate) - 1; + + 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->lstate); + return sendToChannelErrors[1]; + } + + /* unlock channel access */ + luaproc_unlock_channel(chan); + } + + return result; +} + /* send a message to a lua process */ static int luaproc_send( lua_State *L ) { @@ -503,6 +562,8 @@ static int luaproc_send( lua_State *L ) { luaproc dstlp, self; const char *chname = luaL_checkstring( L, 1 ); +// TODO - use the above new function to do the heavy lifting. + /* get exclusive access to operate on channels */ pthread_mutex_lock( &mutex_channel ); 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 ); /* queue a luaproc that tried to send a message */ void luaproc_queue_sender( luaproc lp ); + +const char *sendToChannel(const char *chname, const char *message); + /* queue a luaproc that tried to receive a message */ void luaproc_queue_receiver( luaproc lp ); -- cgit v1.1