diff options
Diffstat (limited to 'src/keeper.c')
-rw-r--r-- | src/keeper.c | 78 |
1 files changed, 7 insertions, 71 deletions
diff --git a/src/keeper.c b/src/keeper.c index 4a5c913..8d9f7ec 100644 --- a/src/keeper.c +++ b/src/keeper.c | |||
@@ -51,7 +51,6 @@ | |||
51 | #include "tools.h" | 51 | #include "tools.h" |
52 | #include "keeper.h" | 52 | #include "keeper.h" |
53 | 53 | ||
54 | #if KEEPER_MODEL == KEEPER_MODEL_C | ||
55 | //################################################################################### | 54 | //################################################################################### |
56 | // Keeper implementation | 55 | // Keeper implementation |
57 | //################################################################################### | 56 | //################################################################################### |
@@ -207,10 +206,10 @@ int keeper_push_linda_storage( lua_State* L, void* ptr) | |||
207 | { | 206 | { |
208 | keeper_fifo* fifo = prepare_fifo_access( KL, -1); // storage key fifo | 207 | keeper_fifo* fifo = prepare_fifo_access( KL, -1); // storage key fifo |
209 | lua_pushvalue( KL, -2); // storage key fifo key | 208 | lua_pushvalue( KL, -2); // storage key fifo key |
210 | luaG_inter_move( KL, L, 1); // storage key fifo // out key | 209 | luaG_inter_move( KL, L, 1, eLM_FromKeeper); // storage key fifo // out key |
211 | STACK_MID( L, 2); | 210 | STACK_MID( L, 2); |
212 | lua_newtable( L); // out key keyout | 211 | lua_newtable( L); // out key keyout |
213 | luaG_inter_move( KL, L, 1); // storage key // out key keyout fifo | 212 | luaG_inter_move( KL, L, 1, eLM_FromKeeper); // storage key // out key keyout fifo |
214 | lua_pushinteger( L, fifo->first); // out key keyout fifo first | 213 | lua_pushinteger( L, fifo->first); // out key keyout fifo first |
215 | STACK_MID( L, 5); | 214 | STACK_MID( L, 5); |
216 | lua_setfield( L, -3, "first"); // out key keyout fifo | 215 | lua_setfield( L, -3, "first"); // out key keyout fifo |
@@ -512,7 +511,6 @@ int keepercall_count( lua_State* L) | |||
512 | } | 511 | } |
513 | return 1; | 512 | return 1; |
514 | } | 513 | } |
515 | #endif // KEEPER_MODEL == KEEPER_MODEL_C | ||
516 | 514 | ||
517 | //################################################################################### | 515 | //################################################################################### |
518 | // Keeper API, accessed from linda methods | 516 | // Keeper API, accessed from linda methods |
@@ -576,17 +574,11 @@ char const* init_keepers( lua_State* L, int _on_state_create, int const _nbKeepe | |||
576 | DEBUGSPEW_CODE( ++ debugspew_indent_depth); | 574 | DEBUGSPEW_CODE( ++ debugspew_indent_depth); |
577 | // We need to load all base libraries in the keeper states so that the transfer databases are populated properly | 575 | // We need to load all base libraries in the keeper states so that the transfer databases are populated properly |
578 | // | 576 | // |
579 | // 'io' for debugging messages, 'package' because we need to require modules exporting idfuncs | 577 | // we don't need any libs in the keeper states |
580 | // the others because they export functions that we may store in a keeper for transfer between lanes | 578 | K = luaG_newstate( L, _on_state_create, NULL); |
581 | K = luaG_newstate( L, _on_state_create, "K"); | ||
582 | 579 | ||
583 | STACK_CHECK( K); | 580 | STACK_CHECK( K); |
584 | 581 | ||
585 | // replace default 'package' contents with stuff gotten from the master state | ||
586 | lua_getglobal( L, "package"); | ||
587 | luaG_inter_copy_package( L, K, -1); | ||
588 | lua_pop( L, 1); | ||
589 | |||
590 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "### init_keepers %d END\n" INDENT_END, i)); | 582 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "### init_keepers %d END\n" INDENT_END, i)); |
591 | DEBUGSPEW_CODE( -- debugspew_indent_depth); | 583 | DEBUGSPEW_CODE( -- debugspew_indent_depth); |
592 | 584 | ||
@@ -596,32 +588,11 @@ char const* init_keepers( lua_State* L, int _on_state_create, int const _nbKeepe | |||
596 | lua_concat( K, 2); | 588 | lua_concat( K, 2); |
597 | lua_setglobal( K, "decoda_name"); | 589 | lua_setglobal( K, "decoda_name"); |
598 | 590 | ||
599 | #if KEEPER_MODEL == KEEPER_MODEL_C | ||
600 | // create the fifos table in the keeper state | 591 | // create the fifos table in the keeper state |
601 | lua_pushlightuserdata( K, fifos_key); | 592 | lua_pushlightuserdata( K, fifos_key); |
602 | lua_newtable( K); | 593 | lua_newtable( K); |
603 | lua_rawset( K, LUA_REGISTRYINDEX); | 594 | lua_rawset( K, LUA_REGISTRYINDEX); |
604 | #endif // KEEPER_MODEL == KEEPER_MODEL_C | 595 | |
605 | |||
606 | #if KEEPER_MODEL == KEEPER_MODEL_LUA | ||
607 | // use package.loaders[2] to find keeper microcode (NOTE: this works only if nobody tampered with the loaders table...) | ||
608 | lua_getglobal( K, "package"); // package | ||
609 | lua_getfield( K, -1, "loaders"); // package package.loaders | ||
610 | lua_rawgeti( K, -1, 2); // package package.loaders package.loaders[2] | ||
611 | lua_pushliteral( K, "lanes-keeper"); // package package.loaders package.loaders[2] "lanes-keeper" | ||
612 | STACK_MID( K, 4); | ||
613 | // first pcall loads lanes-keeper.lua, second one runs the chunk | ||
614 | if( lua_pcall( K, 1 /*args*/, 1 /*results*/, 0 /*errfunc*/) || lua_pcall( K, 0 /*args*/, 0 /*results*/, 0 /*errfunc*/)) | ||
615 | { | ||
616 | // LUA_ERRRUN / LUA_ERRMEM / LUA_ERRERR | ||
617 | // | ||
618 | char const* err = lua_tostring( K, -1); | ||
619 | assert( err); | ||
620 | return err; | ||
621 | } // package package.loaders | ||
622 | STACK_MID( K, 2); | ||
623 | lua_pop( K, 2); | ||
624 | #endif // KEEPER_MODEL == KEEPER_MODEL_LUA | ||
625 | STACK_END( K, 0); | 596 | STACK_END( K, 0); |
626 | MUTEX_INIT( &GKeepers[i].lock_); | 597 | MUTEX_INIT( &GKeepers[i].lock_); |
627 | GKeepers[i].L = K; | 598 | GKeepers[i].L = K; |
@@ -633,41 +604,6 @@ char const* init_keepers( lua_State* L, int _on_state_create, int const _nbKeepe | |||
633 | return NULL; // ok | 604 | return NULL; // ok |
634 | } | 605 | } |
635 | 606 | ||
636 | // cause each keeper state to populate its database of transferable functions with those from the specified module | ||
637 | // do do this we simply require the module inside the keeper state, then populate the lookup database | ||
638 | void populate_keepers( lua_State* L) | ||
639 | { | ||
640 | size_t name_len; | ||
641 | char const* name = luaL_checklstring( L, -1, &name_len); | ||
642 | int i; | ||
643 | |||
644 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "populate_keepers %s BEGIN\n" INDENT_END, name)); | ||
645 | DEBUGSPEW_CODE( ++ debugspew_indent_depth); | ||
646 | |||
647 | for( i = 0; i < GNbKeepers; ++ i) | ||
648 | { | ||
649 | lua_State* K = GKeepers[i].L; | ||
650 | int res; | ||
651 | MUTEX_LOCK( &GKeepers[i].lock_); | ||
652 | STACK_CHECK( K); | ||
653 | STACK_GROW( K, 2); | ||
654 | lua_getglobal( K, "require"); | ||
655 | lua_pushlstring( K, name, name_len); | ||
656 | res = lua_pcall( K, 1, 1, 0); | ||
657 | if( res != LUA_OK) | ||
658 | { | ||
659 | char const* err = luaL_checkstring( K, -1); | ||
660 | luaL_error( L, "error requiring '%s' in keeper state: %s", name, err); | ||
661 | } | ||
662 | // after requiring the module, register the functions it exported in our name<->function database | ||
663 | populate_func_lookup_table( K, -1, name); | ||
664 | lua_pop( K, 1); | ||
665 | STACK_END( K, 0); | ||
666 | MUTEX_UNLOCK( &GKeepers[i].lock_); | ||
667 | } | ||
668 | DEBUGSPEW_CODE( -- debugspew_indent_depth); | ||
669 | } | ||
670 | |||
671 | struct s_Keeper* keeper_acquire( void const* ptr) | 607 | struct s_Keeper* keeper_acquire( void const* ptr) |
672 | { | 608 | { |
673 | // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) | 609 | // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) |
@@ -747,12 +683,12 @@ int keeper_call( lua_State *K, keeper_api_t _func, lua_State *L, void *linda, ui | |||
747 | 683 | ||
748 | lua_pushlightuserdata( K, linda); | 684 | lua_pushlightuserdata( K, linda); |
749 | 685 | ||
750 | if( (args == 0) || luaG_inter_copy( L, K, args) == 0) // L->K | 686 | if( (args == 0) || luaG_inter_copy( L, K, args, eLM_ToKeeper) == 0) // L->K |
751 | { | 687 | { |
752 | lua_call( K, 1 + args, LUA_MULTRET); | 688 | lua_call( K, 1 + args, LUA_MULTRET); |
753 | 689 | ||
754 | retvals = lua_gettop( K) - Ktos; | 690 | retvals = lua_gettop( K) - Ktos; |
755 | if( (retvals > 0) && luaG_inter_move( K, L, retvals) != 0) // K->L | 691 | if( (retvals > 0) && luaG_inter_move( K, L, retvals, eLM_FromKeeper) != 0) // K->L |
756 | { | 692 | { |
757 | retvals = -1; | 693 | retvals = -1; |
758 | } | 694 | } |