aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/luaproc/luaproc.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libraries/luaproc/luaproc.c72
1 files changed, 50 insertions, 22 deletions
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[] = {
114 { NULL, NULL } 114 { NULL, NULL }
115}; 115};
116 116
117/*
117static void registerlib( lua_State *L, const char *name, lua_CFunction f ) { 118static void registerlib( lua_State *L, const char *name, lua_CFunction f ) {
118 lua_getglobal( L, "package" ); 119 lua_getglobal( L, "package" );
119 lua_getfield( L, -1, "preload" ); 120 lua_getfield( L, -1, "preload" );
@@ -121,8 +122,9 @@ static void registerlib( lua_State *L, const char *name, lua_CFunction f ) {
121 lua_setfield( L, -2, name ); 122 lua_setfield( L, -2, name );
122 lua_pop( L, 2 ); 123 lua_pop( L, 2 );
123} 124}
124 125*/
125static void openlibs( lua_State *L ) { 126static void openlibs( lua_State *L ) {
127/*
126 lua_cpcall( L, luaopen_base, NULL ); 128 lua_cpcall( L, luaopen_base, NULL );
127 lua_cpcall( L, luaopen_package, NULL ); 129 lua_cpcall( L, luaopen_package, NULL );
128 registerlib( L, "io", luaopen_io ); 130 registerlib( L, "io", luaopen_io );
@@ -131,6 +133,8 @@ static void openlibs( lua_State *L ) {
131 registerlib( L, "string", luaopen_string ); 133 registerlib( L, "string", luaopen_string );
132 registerlib( L, "math", luaopen_math ); 134 registerlib( L, "math", luaopen_math );
133 registerlib( L, "debug", luaopen_debug ); 135 registerlib( L, "debug", luaopen_debug );
136*/
137 luaL_openlibs(L);
134} 138}
135 139
136/* return status (boolean) indicating if lua process should be recycled */ 140/* return status (boolean) indicating if lua process should be recycled */
@@ -195,7 +199,7 @@ int luaproc_recycle_push( luaproc lp ) {
195} 199}
196 200
197/* create new luaproc */ 201/* create new luaproc */
198luaproc luaproc_new( const char *code, int destroyflag ) { 202static luaproc luaproc_new( const char *code, int destroyflag, int file) {
199 203
200 luaproc lp; 204 luaproc lp;
201 int ret; 205 int ret;
@@ -218,7 +222,10 @@ luaproc luaproc_new( const char *code, int destroyflag ) {
218 luaL_register( lpst, "luaproc", luaproc_funcs_child ); 222 luaL_register( lpst, "luaproc", luaproc_funcs_child );
219 223
220 /* load process' code */ 224 /* load process' code */
221 ret = luaL_loadstring( lpst, code ); 225 if (file)
226 ret = luaL_loadfile( lpst, code );
227 else
228 ret = luaL_loadstring( lpst, code );
222 /* in case of errors, destroy recently created lua process */ 229 /* in case of errors, destroy recently created lua process */
223 if ( ret != 0 ) { 230 if ( ret != 0 ) {
224 lua_close( lpst ); 231 lua_close( lpst );
@@ -231,7 +238,7 @@ luaproc luaproc_new( const char *code, int destroyflag ) {
231 238
232/* synchronize worker threads and exit */ 239/* synchronize worker threads and exit */
233static int luaproc_exit( lua_State *L ) { 240static int luaproc_exit( lua_State *L ) {
234 sched_join_workerthreads( ); 241 sched_join_workerthreads();
235 return 0; 242 return 0;
236} 243}
237 244
@@ -297,7 +304,7 @@ static int luaproc_destroy_worker( lua_State *L ) {
297 304
298 /* create new lua process with empty code and destroy worker flag set to true 305 /* create new lua process with empty code and destroy worker flag set to true
299 (ie, conclusion of lua process WILL result in worker thread destruction */ 306 (ie, conclusion of lua process WILL result in worker thread destruction */
300 lp = luaproc_new( "", TRUE ); 307 lp = luaproc_new( "", TRUE, FALSE );
301 308
302 /* ensure process creation was successfull */ 309 /* ensure process creation was successfull */
303 if ( lp == NULL ) { 310 if ( lp == NULL ) {
@@ -328,7 +335,7 @@ static int luaproc_destroy_worker( lua_State *L ) {
328} 335}
329 336
330/* recycle a lua process */ 337/* recycle a lua process */
331luaproc luaproc_recycle( luaproc lp, const char *code ) { 338static luaproc luaproc_recycle( luaproc lp, const char *code, int file ) {
332 339
333 int ret; 340 int ret;
334 341
@@ -351,12 +358,9 @@ luaproc luaproc_recycle( luaproc lp, const char *code ) {
351 return lp; 358 return lp;
352} 359}
353 360
354/* create and schedule a new lua process (luaproc.newproc) */
355static int luaproc_create_newproc( lua_State *L ) {
356
357 /* check if first argument is a string (lua code) */
358 const char *code = luaL_checkstring( L, 1 );
359 361
362int newProc(const char *code, int file)
363{
360 /* new lua process pointer */ 364 /* new lua process pointer */
361 luaproc lp; 365 luaproc lp;
362 366
@@ -365,21 +369,18 @@ static int luaproc_create_newproc( lua_State *L ) {
365 369
366 /* if there is a lua process available on the recycle queue, recycle it */ 370 /* if there is a lua process available on the recycle queue, recycle it */
367 if ( lp != NULL ) { 371 if ( lp != NULL ) {
368 lp = luaproc_recycle( lp, code ); 372 lp = luaproc_recycle( lp, code, file );
369 } 373 }
370 /* otherwise create a new one from scratch */ 374 /* otherwise create a new one from scratch */
371 else { 375 else {
372 /* create new lua process with destroy worker flag set to false 376 /* create new lua process with destroy worker flag set to false
373 (ie, conclusion of lua process will NOT result in worker thread destruction */ 377 (ie, conclusion of lua process will NOT result in worker thread destruction */
374 lp = luaproc_new( code, FALSE ); 378 lp = luaproc_new( code, FALSE, file );
375 } 379 }
376 380
377 /* ensure process creation was successfull */ 381 /* ensure process creation was successfull */
378 if ( lp == NULL ) { 382 if ( lp == NULL ) {
379 /* in case of errors return nil + error msg */ 383 return 1;
380 lua_pushnil( L );
381 lua_pushstring( L, "error loading code string" );
382 return 2;
383 } 384 }
384 385
385 /* increase active luaproc count */ 386 /* increase active luaproc count */
@@ -392,6 +393,26 @@ static int luaproc_create_newproc( lua_State *L ) {
392 sched_lpcount_dec(); 393 sched_lpcount_dec();
393 /* close lua_State */ 394 /* close lua_State */
394 lua_close( lp->lstate ); 395 lua_close( lp->lstate );
396 return 2;
397 }
398
399 return 0;
400}
401
402/* create and schedule a new lua process (luaproc.newproc) */
403static int luaproc_create_newproc( lua_State *L ) {
404
405 /* check if first argument is a string (lua code) */
406 const char *code = luaL_checkstring( L, 1 );
407
408 switch (newProc(code, FALSE))
409 {
410 case 1 :
411 /* in case of errors return nil + error msg */
412 lua_pushnil( L );
413 lua_pushstring( L, "error loading code string" );
414 return 2;
415 case 2 :
395 /* return nil + error msg */ 416 /* return nil + error msg */
396 lua_pushnil( L ); 417 lua_pushnil( L );
397 lua_pushstring( L, "error queuing process" ); 418 lua_pushstring( L, "error queuing process" );
@@ -635,17 +656,24 @@ static int luaproc_receive( lua_State *L ) {
635 } 656 }
636} 657}
637 658
638LUALIB_API int luaopen_luaproc( lua_State *L ) { 659void luaprocInit(void)
639 660{
640 /* register luaproc functions */
641 luaL_register( L, "luaproc", luaproc_funcs_parent );
642
643 /* initialize recycle list */ 661 /* initialize recycle list */
644 recyclelp = list_new(); 662 recyclelp = list_new();
645 663
646 /* initialize local scheduler */ 664 /* initialize local scheduler */
647 sched_init_local( LUAPROC_SCHED_DEFAULT_WORKER_THREADS ); 665 sched_init_local( LUAPROC_SCHED_DEFAULT_WORKER_THREADS );
666}
648 667
668void luaprocRegister(lua_State *L)
669{
670 /* register luaproc functions */
671 luaL_register( L, "luaproc", luaproc_funcs_parent );
672}
673
674LUALIB_API int luaopen_luaproc( lua_State *L ) {
675 luaprocRegister(L);
676 luaprocInit();
649 return 0; 677 return 0;
650} 678}
651 679