From 3236320311f15640083c5bf420bcdeab725ff242 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Wed, 25 Jan 2012 02:29:35 +1000 Subject: Hack together LuaSL, LuaJIT, and luaproc. It's frankenstein right now. lol --- libraries/README | 3 ++ libraries/luajit-2.0/src/libluajit-5.1.so.2 | 1 + libraries/luaproc/luaproc.c | 72 ++++++++++++++++++++--------- libraries/luaproc/luaproc.h | 5 ++ libraries/luaproc/sched.c | 44 +++++++++++++++++- 5 files changed, 102 insertions(+), 23 deletions(-) create mode 120000 libraries/luajit-2.0/src/libluajit-5.1.so.2 (limited to 'libraries') diff --git a/libraries/README b/libraries/README index 3f6e7c7..d24f7d2 100644 --- a/libraries/README +++ b/libraries/README @@ -6,3 +6,6 @@ should go here to. Makes it easy to compile it all. Note that in some cases, there might be changes. Such changes should be documented. +luaproc has been hacked up a bit, and will continue to be hacked up. +Merging it into LuaSL, and converting it to EFL. + diff --git a/libraries/luajit-2.0/src/libluajit-5.1.so.2 b/libraries/luajit-2.0/src/libluajit-5.1.so.2 new file mode 120000 index 0000000..deaddec --- /dev/null +++ b/libraries/luajit-2.0/src/libluajit-5.1.so.2 @@ -0,0 +1 @@ +libluajit.so \ No newline at end of file diff --git a/libraries/luaproc/luaproc.c b/libraries/luaproc/luaproc.c index 2ec5b31..ff18723 100644 --- a/libraries/luaproc/luaproc.c +++ b/libraries/luaproc/luaproc.c @@ -114,6 +114,7 @@ static const struct luaL_reg luaproc_funcs_child[] = { { NULL, NULL } }; +/* static void registerlib( lua_State *L, const char *name, lua_CFunction f ) { lua_getglobal( L, "package" ); lua_getfield( L, -1, "preload" ); @@ -121,8 +122,9 @@ static void registerlib( lua_State *L, const char *name, lua_CFunction 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 ); @@ -131,6 +133,8 @@ static void openlibs( lua_State *L ) { 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 */ @@ -195,7 +199,7 @@ int luaproc_recycle_push( luaproc lp ) { } /* create new luaproc */ -luaproc luaproc_new( const char *code, int destroyflag ) { +static luaproc luaproc_new( const char *code, int destroyflag, int file) { luaproc lp; int ret; @@ -218,7 +222,10 @@ luaproc luaproc_new( const char *code, int destroyflag ) { luaL_register( lpst, "luaproc", luaproc_funcs_child ); /* load process' code */ - ret = luaL_loadstring( lpst, code ); + if (file) + ret = luaL_loadfile( lpst, code ); + else + ret = luaL_loadstring( lpst, code ); /* in case of errors, destroy recently created lua process */ if ( ret != 0 ) { lua_close( lpst ); @@ -231,7 +238,7 @@ luaproc luaproc_new( const char *code, int destroyflag ) { /* synchronize worker threads and exit */ static int luaproc_exit( lua_State *L ) { - sched_join_workerthreads( ); + sched_join_workerthreads(); return 0; } @@ -297,7 +304,7 @@ static int luaproc_destroy_worker( lua_State *L ) { /* 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 ); + lp = luaproc_new( "", TRUE, FALSE ); /* ensure process creation was successfull */ if ( lp == NULL ) { @@ -328,7 +335,7 @@ static int luaproc_destroy_worker( lua_State *L ) { } /* recycle a lua process */ -luaproc luaproc_recycle( luaproc lp, const char *code ) { +static luaproc luaproc_recycle( luaproc lp, const char *code, int file ) { int ret; @@ -351,12 +358,9 @@ luaproc luaproc_recycle( luaproc lp, const char *code ) { return lp; } -/* 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 ); +int newProc(const char *code, int file) +{ /* new lua process pointer */ luaproc lp; @@ -365,21 +369,18 @@ static int luaproc_create_newproc( lua_State *L ) { /* if there is a lua process available on the recycle queue, recycle it */ if ( lp != NULL ) { - lp = luaproc_recycle( lp, code ); + lp = luaproc_recycle( lp, code, file ); } /* otherwise create a new one from scratch */ else { /* create new lua process with destroy worker flag set to false (ie, conclusion of lua process will NOT result in worker thread destruction */ - lp = luaproc_new( code, FALSE ); + lp = luaproc_new( code, FALSE, file ); } /* ensure process creation was successfull */ if ( lp == NULL ) { - /* in case of errors return nil + error msg */ - lua_pushnil( L ); - lua_pushstring( L, "error loading code string" ); - return 2; + return 1; } /* increase active luaproc count */ @@ -392,6 +393,26 @@ static int luaproc_create_newproc( lua_State *L ) { sched_lpcount_dec(); /* close lua_State */ lua_close( lp->lstate ); + return 2; + } + + 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)) + { + 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" ); @@ -635,17 +656,24 @@ static int luaproc_receive( lua_State *L ) { } } -LUALIB_API int luaopen_luaproc( lua_State *L ) { - - /* register luaproc functions */ - luaL_register( L, "luaproc", luaproc_funcs_parent ); - +void luaprocInit(void) +{ /* initialize recycle list */ recyclelp = list_new(); /* initialize local scheduler */ 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; } diff --git a/libraries/luaproc/luaproc.h b/libraries/luaproc/luaproc.h index 86617a9..4c71685 100644 --- a/libraries/luaproc/luaproc.h +++ b/libraries/luaproc/luaproc.h @@ -44,6 +44,11 @@ THE SOFTWARE. /* lua process pointer type */ typedef struct stluaproc *luaproc; +void luaprocInit(void); +void luaprocRegister(lua_State *L); +int newProc(const char *code, int file); + + /* return a process' status */ int luaproc_get_status( luaproc lp ); diff --git a/libraries/luaproc/sched.c b/libraries/luaproc/sched.c index f19beeb..474c82b 100644 --- a/libraries/luaproc/sched.c +++ b/libraries/luaproc/sched.c @@ -83,9 +83,11 @@ void *workermain( void *args ) { /* detach thread so resources are freed as soon as thread exits (no further joining) */ pthread_detach( pthread_self( )); +//printf("NEW WORKER\n"); /* main worker loop */ while ( 1 ) { +//printf("a\n"); /* get exclusive access to the ready process queue */ pthread_mutex_lock( &mutex_queue_access ); @@ -94,62 +96,83 @@ void *workermain( void *args ) { pthread_cond_wait( &cond_wakeup_worker, &mutex_queue_access ); } +////printf("b\n"); /* pop the first node from the ready process queue */ n = list_pop_head( lpready ); +////printf("c\n"); /* ensure list pop succeeded before proceeding */ if ( n != NULL ) { +//printf("c.0\n"); /* get the popped node's data content (ie, the lua process struct) */ lp = (luaproc )list_data( n ); } else { +////printf("c.1\n"); /* free access to the process ready queue */ pthread_mutex_unlock( &mutex_queue_access ); /* finished thread */ +//printf("c.2 pthread_exit\n"); pthread_exit( NULL ); +//printf("c.3\n"); } +////printf("d\n"); /* free access to the process ready queue */ pthread_mutex_unlock( &mutex_queue_access ); +//printf("e lua_resum\n"); /* execute the lua code specified in the lua process struct */ procstat = lua_resume( luaproc_get_state( lp ), luaproc_get_args( lp )); +//printf("f\n"); /* reset the process argument count */ luaproc_set_args( lp, 0 ); +////printf("g\n"); /* check if process finished its whole execution */ if ( procstat == 0 ) { +//printf("g.0\n"); /* destroy the corresponding list node */ list_destroy_node( n ); +////printf("g.1\n"); /* check if worker thread should be destroyed */ destroyworker = luaproc_get_destroyworker( lp ); +////printf("g.2 proc finished\n"); /* set process status to finished */ luaproc_set_status( lp, LUAPROC_STAT_FINISHED ); +////printf("g.3\n"); /* check if lua process should be recycled and, if not, destroy it */ if ( luaproc_recycle_push( lp ) == FALSE ) { +//printf("g.3.0 lua_close\n"); lua_close( luaproc_get_state( lp )); } +////printf("g.4\n"); /* decrease active lua process count */ sched_lpcount_dec(); +////printf("g.5\n"); /* check if thread should be finished after lua process conclusion */ if ( destroyworker ) { +//printf("g.5.0 pthread_exit\n"); /* if so, finish thread */ pthread_exit( NULL ); } +//printf("g.6\n"); } /* check if process yielded */ else if ( procstat == LUA_YIELD ) { +//printf("??????????????h.0\n"); /* if so, further check if yield originated from an unmatched send/recv operation */ if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_SEND ) { +//printf("??????????????h.1\n"); /* queue blocked lua process on corresponding channel */ luaproc_queue_sender( lp ); /* unlock channel access */ @@ -159,6 +182,7 @@ void *workermain( void *args ) { } else if ( luaproc_get_status( lp ) == LUAPROC_STAT_BLOCKED_RECV ) { +//printf("??????????????h.2\n"); /* queue blocked lua process on corresponding channel */ luaproc_queue_receiver( lp ); /* unlock channel access */ @@ -169,6 +193,7 @@ void *workermain( void *args ) { /* or if yield resulted from an explicit call to coroutine.yield in the lua code being executed */ else { +//printf("??????????????h.3\n"); /* get exclusive access to the ready process queue */ pthread_mutex_lock( &mutex_queue_access ); /* re-insert the job at the end of the ready process queue */ @@ -180,6 +205,7 @@ void *workermain( void *args ) { /* check if there was any execution error (LUA_ERRRUN, LUA_ERRSYNTAX, LUA_ERRMEM or LUA_ERRERR) */ else { +//printf("??????????????i.0\n"); /* destroy the corresponding node */ list_destroy_node( n ); /* print error message */ @@ -189,6 +215,7 @@ void *workermain( void *args ) { /* decrease active lua process count */ sched_lpcount_dec(); } +//printf("END\n"); } } @@ -260,29 +287,42 @@ int sched_queue_proc( luaproc lp ) { /* synchronize worker threads */ void sched_join_workerthreads( void ) { +////printf(" 0\n"); pthread_mutex_lock( &mutex_lp_count ); +//printf(" 1 wait for procs to end\n"); /* wait until there is no more active lua processes */ while( lpcount != 0 ) { +//printf(" 1.0\n"); pthread_cond_wait( &cond_no_active_lp, &mutex_lp_count ); } /* get exclusive access to the ready process queue */ +////printf(" 2\n"); pthread_mutex_lock( &mutex_queue_access ); /* set the no more active lua processes flag to true */ +////printf(" 3\n"); no_more_processes = TRUE; /* wake ALL workers up */ +//printf(" 4 wake up all workers.\n"); pthread_cond_broadcast( &cond_wakeup_worker ); /* free access to the process ready queue */ +////printf(" 5\n"); pthread_mutex_unlock( &mutex_queue_access ); + +// We don't need this, as we only get here during shutdown. Linking this to EFL results in a hang otherwise anyway. /* wait for (join) worker threads */ - pthread_exit( NULL ); +//printf(" 6 pthread_exit, waiting for workers to end\n"); +// pthread_exit( NULL ); +//printf("7\n"); pthread_mutex_unlock( &mutex_lp_count ); +//printf("8\n"); } /* increase active lua process count */ void sched_lpcount_inc( void ) { +//printf("inc procs++++++++++++++++++++++++++++++++++++++++\n"); pthread_mutex_lock( &mutex_lp_count ); lpcount++; pthread_mutex_unlock( &mutex_lp_count ); @@ -290,10 +330,12 @@ void sched_lpcount_inc( void ) { /* decrease active lua process count */ void sched_lpcount_dec( void ) { +//printf("dec procs----------------------------------------\n"); pthread_mutex_lock( &mutex_lp_count ); lpcount--; /* if count reaches zero, signal there are no more active processes */ if ( lpcount == 0 ) { +//printf("dec procs AND NONE LEFT000000000000000000000000000\n"); pthread_cond_signal( &cond_no_active_lp ); } pthread_mutex_unlock( &mutex_lp_count ); -- cgit v1.1