From db634cda56ca8eca2ddab728d1a95dc9fbd36eba Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sat, 25 Feb 2012 01:36:02 +1000 Subject: Remove a whole bunch of stuff we don't need from our luaproc fork, and rearrange what's left. --- LuaSL/src/LuaSL_threads.c | 733 ++++++++++++++-------------------------------- 1 file changed, 212 insertions(+), 521 deletions(-) (limited to 'LuaSL/src/LuaSL_threads.c') diff --git a/LuaSL/src/LuaSL_threads.c b/LuaSL/src/LuaSL_threads.c index a15b506..a8a1681 100644 --- a/LuaSL/src/LuaSL_threads.c +++ b/LuaSL/src/LuaSL_threads.c @@ -32,42 +32,47 @@ THE SOFTWARE. /* This is a redesign of luaproc. The design goals and notes - * - * Use ecore threads instead of raw pthreads. - * Ecore threads pretty much wraps pthreads on posix, but has Windows support to. * In general use EFL where it is useful. * One fixed unique message channel per script. - * 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. * 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. * Merge the luaproc structure with the script structure. + * 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. - * Get rid of luaproc.lua, should not need it. * Use my coding standards, or EFL ones. Pffft. * */ #include "LuaSL.h" -#include -#include -#include -#include -#include -#include +#define CHANNEL_MAX_NAME_LENGTH 255 -/********* -* globals -*********/ +#define CHANNEL_DESTROYED 0 -/* global channel lua_State mutex */ -pthread_mutex_t mutex_channel_lstate = PTHREAD_MUTEX_INITIALIZER; +/* 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 +/* process is finished */ +#define LUAPROC_STAT_FINISHED 4 -/* global lua_State where channel hash table will be stored */ -lua_State *chanls = NULL; /* message channel */ struct stchannel { @@ -77,7 +82,26 @@ struct stchannel { pthread_cond_t *in_use; }; +/* lua process */ +struct stluaproc { + Eina_Clist node; + lua_State *lstate; + int stat; + int args; + channel chan; + void *data; +}; + + +/********* +* globals +*********/ + +/* global channel lua_State mutex */ +pthread_mutex_t mutex_channel_lstate = PTHREAD_MUTEX_INITIALIZER; +/* global lua_State where channel hash table will be stored */ +lua_State *chanls = NULL; /* ready process list */ Eina_Clist lpready; @@ -100,8 +124,6 @@ int lpcount = 0; /* no more lua processes flag */ int no_more_processes = FALSE; - - /* channel operations mutex */ pthread_mutex_t mutex_channel = PTHREAD_MUTEX_INITIALIZER; @@ -114,65 +136,30 @@ Eina_Clist recyclelp; /* maximum lua processes to recycle */ int recyclemax = 0; -/* lua process */ -struct stluaproc { - Eina_Clist node; - lua_State *lstate; - int stat; - int args; - channel chan; - int destroyworker; - void *data; - Ecore_Cb callback; -}; - - /****************************** * library functions prototypes ******************************/ -/* create a new lua process */ -static int luaproc_create_newproc( lua_State *L ); /* 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 ); -/* destroy a channel */ -static int luaproc_destroy_channel( lua_State *L ); -/* wait until all luaprocs have finished and exit */ -static int luaproc_exit( lua_State *L ); -/* create a new worker */ -static int luaproc_create_worker( lua_State *L ); -/* destroy a worker */ -static int luaproc_destroy_worker( lua_State *L ); -/* set amount of lua processes that should be recycled (ie, reused) */ -static int luaproc_recycle_set( 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[] = { - { "newproc", luaproc_create_newproc }, - { "exit", luaproc_exit }, - { "createworker", luaproc_create_worker }, - { "destroyworker", luaproc_destroy_worker }, - { "recycle", luaproc_recycle_set }, { "sendback", luaproc_send_back }, { NULL, NULL } }; /* luaproc function registration array - newproc (child) functions */ static const struct luaL_reg luaproc_funcs_child[] = { - { "newproc", luaproc_create_newproc }, { "send", luaproc_send }, { "receive", luaproc_receive }, { "newchannel", luaproc_create_channel }, - { "delchannel", luaproc_destroy_channel }, - { "createworker", luaproc_create_worker }, - { "destroyworker", luaproc_destroy_worker }, - { "recycle", luaproc_recycle_set }, { "sendback", luaproc_send_back }, { NULL, NULL } }; @@ -181,14 +168,14 @@ static const struct luaL_reg luaproc_funcs_child[] = { /* initialize channel table */ -void channel_init( void ) { +static void channel_init( void ) { chanls = luaL_newstate(); lua_newtable( chanls ); lua_setglobal( chanls, "channeltb" ); } /* create new channel */ -channel channel_create( const char *cname ) { +static channel channel_create( const char *cname ) { channel chan; @@ -214,26 +201,8 @@ channel channel_create( const char *cname ) { return chan; } -/* destroy a channel */ -int channel_destroy( channel chan, const char *chname ) { - - /* get exclusive access to the channel table */ - pthread_mutex_lock( &mutex_channel_lstate ); - - lua_getglobal( chanls, "channeltb"); - lua_pushstring( chanls, chname ); - lua_pushnil( chanls ); - lua_settable( chanls, -3 ); - lua_pop( chanls, 1 ); - - /* let others access the channel table */ - pthread_mutex_unlock( &mutex_channel_lstate ); - - return CHANNEL_DESTROYED; -} - /* search for and return a channel with a given name */ -channel channel_search( const char *cname ) { +static channel channel_search( const char *cname ) { channel chan; @@ -257,34 +226,187 @@ channel channel_search( const char *cname ) { } /* return a channel's send queue */ -Eina_Clist *channel_get_sendq( channel chan ) { +static Eina_Clist *channel_get_sendq( channel chan ) { return &chan->send; } /* return a channel's receive queue */ -Eina_Clist *channel_get_recvq( channel chan ) { +static Eina_Clist *channel_get_recvq( channel chan ) { return &chan->recv; } /* return a channel's mutex */ -pthread_mutex_t *channel_get_mutex( channel chan ) { +static pthread_mutex_t *channel_get_mutex( channel chan ) { return chan->mutex; } /* return a channel's conditional variable */ -pthread_cond_t *channel_get_cond( channel chan ) { +static pthread_cond_t *channel_get_cond( channel chan ) { return chan->in_use; } +/* return status (boolean) indicating if lua process should be recycled */ +static luaproc luaproc_recycle_pop( void ) { + + luaproc lp; + + /* get exclusive access to operate on recycle list */ + pthread_mutex_lock( &mutex_recycle_list ); + + /* check if there are any lua processes on recycle list */ + if ( eina_clist_count( &recyclelp ) > 0 ) { + /* pop list head */ + if ((lp = (luaproc) eina_clist_head(&recyclelp))) + eina_clist_remove(&(lp->node)); + /* free access to operate on recycle list */ + pthread_mutex_unlock( &mutex_recycle_list ); + /* return associated luaproc */ + return lp; + } + + /* free access to operate on recycle list */ + pthread_mutex_unlock( &mutex_recycle_list ); + + /* if no lua processes are available simply return null */ + return NULL; +} + +/* check if lua process should be recycled and, in case so, add it to the recycle list */ +static int luaproc_recycle_push( luaproc lp ) { + + /* get exclusive access to operate on recycle list */ + pthread_mutex_lock( &mutex_recycle_list ); + + /* check if amount of lua processes currently on recycle list is greater than + or equal to the maximum amount of lua processes that should be recycled */ + if ( eina_clist_count( &recyclelp ) >= recyclemax ) { + /* free access to operate on recycle list */ + pthread_mutex_unlock( &mutex_recycle_list ); + /* if so, lua process should NOT be recycled and should be destroyed */ + return FALSE; + } + /* otherwise, lua process should be added to recycle list */ + eina_clist_add_tail( &recyclelp, &(lp->node) ); + /* free access to operate on recycle list */ + pthread_mutex_unlock( &mutex_recycle_list ); + /* since lua process will be recycled, it should not be destroyed */ + return TRUE; +} + +/* queue a lua process sending a message without a matching receiver */ +static void luaproc_queue_sender( luaproc lp ) { + /* add the sending process to this process' send queue */ + eina_clist_add_tail( channel_get_sendq( lp->chan ), &(lp->node)); +} + +/* dequeue a lua process sending a message with a receiver match */ +static luaproc luaproc_dequeue_sender( channel chan ) { + + luaproc lp; + + if ( eina_clist_count( channel_get_sendq( chan )) > 0 ) { + /* get first node from channel's send queue */ + if ((lp = (luaproc) eina_clist_head(channel_get_sendq( chan )))) + eina_clist_remove(&(lp->node)); + /* return associated luaproc */ + return lp; + } + + return NULL; +} + +/* queue a luc process receiving a message without a matching sender */ +static void luaproc_queue_receiver( luaproc lp ) { + /* add the receiving process to this process' receive queue */ + eina_clist_add_tail( channel_get_recvq( lp->chan ), &(lp->node)); +} + +/* dequeue a lua process receiving a message with a sender match */ +static luaproc luaproc_dequeue_receiver( channel chan ) { + + luaproc lp; + + if ( eina_clist_count( channel_get_recvq( chan )) > 0 ) { + /* get first node from channel's recv queue */ + if ((lp = (luaproc) eina_clist_head(channel_get_recvq( chan )))) + eina_clist_remove(&(lp->node)); + /* return associated luaproc */ + return lp; + } + + return NULL; +} + +/* return a process' status */ +static int luaproc_get_status( luaproc lp ) { + return lp->stat; +} + +/* set a process' status */ +static void luaproc_set_status( luaproc lp, int status ) { + lp->stat = status; +} + +/* return a process' state */ +static lua_State *luaproc_get_state( luaproc lp ) { + return lp->lstate; +} + +/* return the number of arguments expected by a given process */ +static int luaproc_get_args( luaproc lp ) { + return lp->args; +} + +/* set the number of arguments expected by a given process */ +static void luaproc_set_args( luaproc lp, int n ) { + lp->args = n; +} + + +/* return the channel where the corresponding luaproc is blocked at */ +static channel luaproc_get_channel( luaproc lp ) { + return lp->chan; +} + +/* 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( channel_get_mutex( chan )); + /* signal channel not in use */ + pthread_cond_signal( channel_get_cond( chan )); + /* 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 */ -void *workermain( void *args ) { +static void *workermain( void *args ) { luaproc lp; int procstat; - int destroyworker; /* detach thread so resources are freed as soon as thread exits (no further joining) */ pthread_detach( pthread_self( )); @@ -322,9 +444,6 @@ void *workermain( void *args ) { /* check if process finished its whole execution */ if ( procstat == 0 ) { - /* check if worker thread should be destroyed */ - destroyworker = luaproc_get_destroyworker( lp ); - /* set process status to finished */ luaproc_set_status( lp, LUAPROC_STAT_FINISHED ); @@ -336,11 +455,6 @@ void *workermain( void *args ) { /* decrease active lua process count */ sched_lpcount_dec(); - /* check if thread should be finished after lua process conclusion */ - if ( destroyworker ) { - /* if so, finish thread */ - pthread_exit( NULL ); - } } /* check if process yielded */ @@ -385,14 +499,13 @@ void *workermain( void *args ) { } /* local scheduler initialization */ -int sched_init_local( int numworkers ) { +static int sched_init_local( int numworkers ) { int tid; int workercount = 0; pthread_t worker; /* initialize ready process list */ -// lpready = list_new(); eina_clist_init(&lpready); /* initialize channels */ @@ -412,17 +525,8 @@ int sched_init_local( int numworkers ) { return LUAPROC_SCHED_OK; } -/* exit scheduler */ -void sched_exit( void ) { - - /* get exclusive access to the ready process queue */ - pthread_mutex_lock( &mutex_queue_access ); - /* free access to the process ready queue */ - pthread_mutex_unlock( &mutex_queue_access ); -} - /* move process to ready queue (ie, schedule process) */ -int sched_queue_proc( luaproc lp ) { +static int sched_queue_proc( luaproc lp ) { /* get exclusive access to the ready process queue */ pthread_mutex_lock( &mutex_queue_access ); @@ -467,24 +571,6 @@ void sched_join_workerthreads( void ) { } -/* increase active lua process count */ -void sched_lpcount_inc( void ) { - pthread_mutex_lock( &mutex_lp_count ); - lpcount++; - pthread_mutex_unlock( &mutex_lp_count ); -} - -/* decrease active lua process count */ -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 ); -} - /* create a new worker pthread */ int sched_create_worker( void ) { @@ -498,80 +584,10 @@ int sched_create_worker( void ) { return LUAPROC_SCHED_OK; } - - - -/* -static void registerlib( lua_State *L, const char *name, lua_CFunction f ) { - lua_getglobal( L, "package" ); - lua_getfield( L, -1, "preload" ); - lua_pushcfunction( L, f ); - lua_setfield( L, -2, name ); - lua_pop( L, 2 ); -} -*/ static void openlibs( lua_State *L ) { -/* - lua_cpcall( L, luaopen_base, NULL ); - lua_cpcall( L, luaopen_package, NULL ); - registerlib( L, "io", luaopen_io ); - registerlib( L, "os", luaopen_os ); - registerlib( L, "table", luaopen_table ); - registerlib( L, "string", luaopen_string ); - registerlib( L, "math", luaopen_math ); - registerlib( L, "debug", luaopen_debug ); -*/ luaL_openlibs(L); } -/* return status (boolean) indicating if lua process should be recycled */ -luaproc luaproc_recycle_pop( void ) { - - luaproc lp; - - /* get exclusive access to operate on recycle list */ - pthread_mutex_lock( &mutex_recycle_list ); - - /* check if there are any lua processes on recycle list */ - if ( eina_clist_count( &recyclelp ) > 0 ) { - /* pop list head */ - if ((lp = (luaproc) eina_clist_head(&recyclelp))) - eina_clist_remove(&(lp->node)); - /* free access to operate on recycle list */ - pthread_mutex_unlock( &mutex_recycle_list ); - /* return associated luaproc */ - return lp; - } - - /* free access to operate on recycle list */ - pthread_mutex_unlock( &mutex_recycle_list ); - - /* if no lua processes are available simply return null */ - return NULL; -} - -/* check if lua process should be recycled and, in case so, add it to the recycle list */ -int luaproc_recycle_push( luaproc lp ) { - - /* get exclusive access to operate on recycle list */ - pthread_mutex_lock( &mutex_recycle_list ); - - /* check if amount of lua processes currently on recycle list is greater than - or equal to the maximum amount of lua processes that should be recycled */ - if ( eina_clist_count( &recyclelp ) >= recyclemax ) { - /* free access to operate on recycle list */ - pthread_mutex_unlock( &mutex_recycle_list ); - /* if so, lua process should NOT be recycled and should be destroyed */ - return FALSE; - } - /* otherwise, lua process should be added to recycle list */ - eina_clist_add_tail( &recyclelp, &(lp->node) ); - /* free access to operate on recycle list */ - pthread_mutex_unlock( &mutex_recycle_list ); - /* since lua process will be recycled, it should not be destroyed */ - return TRUE; -} - /* create new luaproc */ static luaproc luaproc_new( const char *code, int destroyflag, int file) { @@ -588,7 +604,6 @@ static luaproc luaproc_new( const char *code, int destroyflag, int file) { lp->stat = LUAPROC_STAT_IDLE; lp->args = 0; lp->chan = NULL; - lp->destroyworker = destroyflag; /* load standard libraries */ openlibs( lpst ); @@ -611,99 +626,6 @@ static luaproc luaproc_new( const char *code, int destroyflag, int file) { return lp; } -/* synchronize worker threads and exit */ -static int luaproc_exit( lua_State *L ) { - sched_join_workerthreads(); - return 0; -} - -/* create a new worker pthread */ -static int luaproc_create_worker( lua_State *L ) { - - if ( sched_create_worker( ) != LUAPROC_SCHED_OK ) { - lua_pushnil( L ); - lua_pushstring( L, "error creating worker" ); - return 2; - } - - lua_pushboolean( L, TRUE ); - return 1; -} - -/* set amount of lua processes that should be recycled (ie, reused) */ -static int luaproc_recycle_set( lua_State *L ) { - - luaproc lp; - int max = luaL_checkint( L, 1 ); - - /* check if function argument represents a reasonable value */ - if ( max < 0 ) { - /* in case of errors return nil + error msg */ - lua_pushnil( L ); - lua_pushstring( L, "error setting recycle limit to negative value" ); - return 2; - } - - /* get exclusive access to operate on recycle list */ - pthread_mutex_lock( &mutex_recycle_list ); - - /* set maximum lua processes that should be recycled */ - recyclemax = max; - - /* destroy recycle list excessive nodes (and corresponding lua processes) */ - while ( eina_clist_count( &recyclelp ) > max ) { - if ((lp = (luaproc) eina_clist_head(&recyclelp))) - eina_clist_remove(&(lp->node)); - /* close associated lua_State */ - lua_close( lp->lstate ); - } - - /* free access to operate on recycle list */ - pthread_mutex_unlock( &mutex_recycle_list ); - - lua_pushboolean( L, TRUE ); - return 1; -} - - -/* destroy a worker pthread */ -static int luaproc_destroy_worker( lua_State *L ) { - - /* new lua process pointer */ - luaproc lp; - - /* create new lua process with empty code and destroy worker flag set to true - (ie, conclusion of lua process WILL result in worker thread destruction */ - lp = luaproc_new( "", TRUE, FALSE ); - - /* ensure process creation was successfull */ - if ( lp == NULL ) { - /* in case of errors return nil + error msg */ - lua_pushnil( L ); - lua_pushstring( L, "error destroying worker" ); - return 2; - } - - /* increase active luaproc count */ - sched_lpcount_inc(); - - /* schedule luaproc */ - if ( sched_queue_proc( lp ) != LUAPROC_SCHED_QUEUE_PROC_OK ) { - printf( "[luaproc] error queueing Lua process\n" ); - /* decrease active luaproc count */ - sched_lpcount_dec(); - /* close lua_State */ - lua_close( lp->lstate ); - /* return nil + error msg */ - lua_pushnil( L ); - lua_pushstring( L, "error destroying worker" ); - return 2; - } - - lua_pushboolean( L, TRUE ); - return 1; -} - /* recycle a lua process */ static luaproc luaproc_recycle( luaproc lp, const char *code, int file ) { @@ -713,7 +635,6 @@ static luaproc luaproc_recycle( luaproc lp, const char *code, int file ) { lp->stat = LUAPROC_STAT_IDLE; lp->args = 0; lp->chan = NULL; - lp->destroyworker = FALSE; /* load process' code */ ret = luaL_loadstring( lp->lstate, code ); @@ -728,8 +649,7 @@ static luaproc luaproc_recycle( luaproc lp, const char *code, int file ) { return lp; } - -int newProc(const char *code, int file, Ecore_Cb callback, void *data) +int newProc(const char *code, int file, void *data) { /* new lua process pointer */ luaproc lp; @@ -753,9 +673,8 @@ int newProc(const char *code, int file, Ecore_Cb callback, void *data) return 1; } - /* Stash any data and callback given to us. */ + /* Stash any data given to us. */ lp->data = data; - lp->callback = callback; /* increase active luaproc count */ sched_lpcount_inc(); @@ -773,76 +692,8 @@ int newProc(const char *code, int file, Ecore_Cb callback, void *data) return 0; } -/* create and schedule a new lua process (luaproc.newproc) */ -static int luaproc_create_newproc( lua_State *L ) { - - /* check if first argument is a string (lua code) */ - const char *code = luaL_checkstring( L, 1 ); - - switch (newProc(code, FALSE, NULL, NULL)) - { - case 1 : - /* in case of errors return nil + error msg */ - lua_pushnil( L ); - lua_pushstring( L, "error loading code string" ); - return 2; - case 2 : - /* return nil + error msg */ - lua_pushnil( L ); - lua_pushstring( L, "error queuing process" ); - return 2; - } - - lua_pushboolean( L, TRUE ); - return 1; -} - -/* queue a lua process sending a message without a matching receiver */ -void luaproc_queue_sender( luaproc lp ) { - /* add the sending process to this process' send queue */ - eina_clist_add_tail( channel_get_sendq( lp->chan ), &(lp->node)); -} - -/* dequeue a lua process sending a message with a receiver match */ -luaproc luaproc_dequeue_sender( channel chan ) { - - luaproc lp; - - if ( eina_clist_count( channel_get_sendq( chan )) > 0 ) { - /* get first node from channel's send queue */ - if ((lp = (luaproc) eina_clist_head(channel_get_sendq( chan )))) - eina_clist_remove(&(lp->node)); - /* return associated luaproc */ - return lp; - } - - return NULL; -} - -/* queue a luc process receiving a message without a matching sender */ -void luaproc_queue_receiver( luaproc lp ) { - /* add the receiving process to this process' receive queue */ - eina_clist_add_tail( channel_get_recvq( lp->chan ), &(lp->node)); -} - -/* dequeue a lua process receiving a message with a sender match */ -luaproc luaproc_dequeue_receiver( channel chan ) { - - luaproc lp; - - if ( eina_clist_count( channel_get_recvq( chan )) > 0 ) { - /* get first node from channel's recv queue */ - if ((lp = (luaproc) eina_clist_head(channel_get_recvq( chan )))) - eina_clist_remove(&(lp->node)); - /* return associated luaproc */ - return lp; - } - - return NULL; -} - /* moves values between lua states' stacks */ -void luaproc_movevalues( lua_State *Lfrom, lua_State *Lto ) { +static void luaproc_movevalues( lua_State *Lfrom, lua_State *Lto ) { int i; int n = lua_gettop( Lfrom ); @@ -854,7 +705,7 @@ void luaproc_movevalues( lua_State *Lfrom, lua_State *Lto ) { } /* return the lua process associated with a given lua state */ -luaproc luaproc_getself( lua_State *L ) { +static luaproc luaproc_getself( lua_State *L ) { luaproc lp; lua_getfield( L, LUA_REGISTRYINDEX, "_SELF" ); lp = (luaproc )lua_touserdata( L, -1 ); @@ -869,7 +720,7 @@ static int luaproc_send_back( lua_State *L ) { const char *message = luaL_checkstring( L, 1 ); self = luaproc_getself( L ); - if (self && self->callback && self->data) + if (self && self->data) { scriptMessage *sm = calloc(1, sizeof(scriptMessage)); @@ -877,7 +728,7 @@ static int luaproc_send_back( lua_State *L ) { { sm->script = self->data; strcpy((char *) sm->message, message); - ecore_main_loop_thread_safe_call_async(self->callback, sm); + ecore_main_loop_thread_safe_call_async(scriptSendBack, sm); } } @@ -1079,43 +930,6 @@ void luaprocInit(void) sched_init_local( LUAPROC_SCHED_DEFAULT_WORKER_THREADS ); } -void luaprocRegister(lua_State *L) -{ - /* register luaproc functions */ - luaL_register( L, "luaproc", luaproc_funcs_parent ); -} - -LUALIB_API int luaopen_luaproc( lua_State *L ) { - luaprocRegister(L); - luaprocInit(); - return 0; -} - -/* return a process' status */ -int luaproc_get_status( luaproc lp ) { - return lp->stat; -} - -/* set a process' status */ -void luaproc_set_status( luaproc lp, int status ) { - lp->stat = status; -} - -/* return a process' state */ -lua_State *luaproc_get_state( luaproc lp ) { - return lp->lstate; -} - -/* return the number of arguments expected by a given process */ -int luaproc_get_args( luaproc lp ) { - return lp->args; -} - -/* set the number of arguments expected by a given process */ -void luaproc_set_args( luaproc lp, int n ) { - lp->args = n; -} - /* create a new channel */ static int luaproc_create_channel( lua_State *L ) { @@ -1144,126 +958,3 @@ static int luaproc_create_channel( lua_State *L ) { return 1; } - -/* destroy a channel */ -static int luaproc_destroy_channel( lua_State *L ) { - - channel chan; - luaproc lp; - pthread_mutex_t *chmutex; - pthread_cond_t *chcond; - const char *chname = luaL_checkstring( L, 1 ); - - - /* 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 to Lua */ - if ( chan == NULL ) { - lua_pushnil( L ); - lua_pushstring( L, "non-existent channel" ); - return 2; - } - - /* get channel's mutex and conditional pointers */ - chmutex = channel_get_mutex( chan ); - chcond = channel_get_cond( chan ); - - /* search for processes waiting to send a message on this channel */ - while (( lp = (luaproc) eina_clist_head( channel_get_sendq( chan ))) != NULL ) { - eina_clist_remove(&(lp->node)); - - /* return an error so the processe knows the channel was destroyed before the message was sent */ - lua_settop( lp->lstate, 0 ); - lua_pushnil( lp->lstate ); - lua_pushstring( lp->lstate, "channel destroyed while waiting for receiver" ); - lp->args = 2; - - /* schedule the process for execution */ - if ( sched_queue_proc( lp ) != LUAPROC_SCHED_QUEUE_PROC_OK ) { - - /* decrease active luaproc count */ - sched_lpcount_dec(); - - /* close lua_State */ - lua_close( lp->lstate ); - } - } - - /* search for processes waiting to receive a message on this channel */ - while (( lp = (luaproc) eina_clist_head( channel_get_recvq( chan ))) != NULL ) { - eina_clist_remove(&(lp->node)); - - /* return an error so the processe knows the channel was destroyed before the message was received */ - lua_settop( lp->lstate, 0 ); - lua_pushnil( lp->lstate ); - lua_pushstring( lp->lstate, "channel destroyed while waiting for sender" ); - lp->args = 2; - - /* schedule the process for execution */ - if ( sched_queue_proc( lp ) != LUAPROC_SCHED_QUEUE_PROC_OK ) { - - /* decrease active luaproc count */ - sched_lpcount_dec(); - - /* close lua_State */ - lua_close( lp->lstate ); - } - } - - /* get exclusive access to operate on channels */ - pthread_mutex_lock( &mutex_channel ); - /* destroy channel */ - channel_destroy( chan, chname ); - /* broadcast channel not in use */ - pthread_cond_broadcast( chcond ); - /* unlock channel access */ - pthread_mutex_unlock( chmutex ); - /* destroy channel mutex and conditional */ - pthread_mutex_destroy( chmutex ); - pthread_cond_destroy( chcond ); - /* free memory used by channel mutex and conditional */ - free( chmutex ); - free( chcond ); - /* free access to operate on channels */ - pthread_mutex_unlock( &mutex_channel ); - - lua_pushboolean( L, TRUE ); - - return 1; -} - -/* register luaproc's functions in a lua_State */ -void luaproc_register_funcs( lua_State *L ) { - luaL_register( L, "luaproc", luaproc_funcs_child ); -} - -/* return the channel where the corresponding luaproc is blocked at */ -channel luaproc_get_channel( luaproc lp ) { - return lp->chan; -} - -/* unlock access to a channel */ -void luaproc_unlock_channel( channel chan ) { - /* get exclusive access to operate on channels */ - pthread_mutex_lock( &mutex_channel ); - /* unlock channel access */ - pthread_mutex_unlock( channel_get_mutex( chan )); - /* signal channel not in use */ - pthread_cond_signal( channel_get_cond( chan )); - /* free access to operate on channels */ - pthread_mutex_unlock( &mutex_channel ); -} - -/* return status (boolean) indicating if worker thread should be destroyed after luaproc execution */ -int luaproc_get_destroyworker( luaproc lp ) { - return lp->destroyworker; -} -- cgit v1.1