From 9645a86fbcb16ca83c5f4ff0dae0925ac71bb46b Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 25 Oct 2018 14:03:32 +0200 Subject: Fix Lanes build by reorganizing types around a bit --- src/deep.c | 36 ++++++++-------- src/deep.h | 22 +++++++++- src/keeper.c | 28 ++++++------ src/keeper.h | 23 +++++++--- src/lanes.c | 134 +++++++++++++++++++++++++++++---------------------------- src/tools.c | 56 ++++++++++++------------ src/tools.h | 46 +++++++------------- src/universe.c | 14 +++--- src/universe.h | 24 +++++++---- 9 files changed, 203 insertions(+), 180 deletions(-) (limited to 'src') diff --git a/src/deep.c b/src/deep.c index 737ebe9..e9b7db5 100644 --- a/src/deep.c +++ b/src/deep.c @@ -32,11 +32,6 @@ THE SOFTWARE. =============================================================================== */ -#include "compat.h" -#include "tools.h" -#include "universe.h" -#include "deep.h" - #include #include #include @@ -45,6 +40,11 @@ THE SOFTWARE. #include #endif +#include "compat.h" +#include "deep.h" +#include "tools.h" +#include "universe.h" + /*-- Metatable copying --*/ /* @@ -179,7 +179,7 @@ static inline luaG_IdFunction get_idfunc( lua_State* L, int index, enum eLookupM // when looking inside a keeper, we are 100% sure the object is a deep userdata if( mode_ == eLM_FromKeeper) { - struct DEEP_PRELUDE** proxy = (struct DEEP_PRELUDE**) lua_touserdata( L, index); + DeepPrelude** proxy = (DeepPrelude**) lua_touserdata( L, index); // we can (and must) cast and fetch the internally stored idfunc return (*proxy)->idfunc; } @@ -208,7 +208,7 @@ static inline luaG_IdFunction get_idfunc( lua_State* L, int index, enum eLookupM } -void free_deep_prelude( lua_State* L, struct DEEP_PRELUDE* prelude_) +void free_deep_prelude( lua_State* L, DeepPrelude* prelude_) { // Call 'idfunc( "delete", deep_ptr )' to make deep cleanup lua_pushlightuserdata( L, prelude_->deep); @@ -226,9 +226,9 @@ void free_deep_prelude( lua_State* L, struct DEEP_PRELUDE* prelude_) */ static int deep_userdata_gc( lua_State* L) { - struct DEEP_PRELUDE** proxy = (struct DEEP_PRELUDE**) lua_touserdata( L, 1); - struct DEEP_PRELUDE* p = *proxy; - struct s_Universe* U = universe_get( L); + DeepPrelude** proxy = (DeepPrelude**) lua_touserdata( L, 1); + DeepPrelude* p = *proxy; + Universe* U = universe_get( L); int v; // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded @@ -270,9 +270,9 @@ static int deep_userdata_gc( lua_State* L) * used in this Lua state (metatable, registring it). Otherwise, increments the * reference count. */ -char const* push_deep_proxy( struct s_Universe* U, lua_State* L, struct DEEP_PRELUDE* prelude, enum eLookupMode mode_) +char const* push_deep_proxy( Universe* U, lua_State* L, DeepPrelude* prelude, enum eLookupMode mode_) { - struct DEEP_PRELUDE** proxy; + DeepPrelude** proxy; // Check if a proxy already exists push_registry_subtable_mode( L, DEEP_PROXY_CACHE_KEY, "v"); // DPC @@ -297,7 +297,7 @@ char const* push_deep_proxy( struct s_Universe* U, lua_State* L, struct DEEP_PRE STACK_GROW( L, 7); STACK_CHECK( L); - proxy = lua_newuserdata( L, sizeof(struct DEEP_PRELUDE*)); // DPC proxy + proxy = lua_newuserdata( L, sizeof(DeepPrelude*)); // DPC proxy ASSERT_L( proxy); *proxy = prelude; @@ -454,7 +454,7 @@ char const* push_deep_proxy( struct s_Universe* U, lua_State* L, struct DEEP_PRE int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction idfunc) { char const* errmsg; - struct DEEP_PRELUDE* prelude = DEEP_MALLOC( sizeof(struct DEEP_PRELUDE)); + DeepPrelude* prelude = DEEP_MALLOC( sizeof( DeepPrelude)); if( prelude == NULL) { return luaL_error( L, "couldn't not allocate deep prelude: out of memory"); @@ -496,7 +496,7 @@ int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction idfunc) */ void* luaG_todeep( lua_State* L, luaG_IdFunction idfunc, int index) { - struct DEEP_PRELUDE** proxy; + DeepPrelude** proxy; STACK_CHECK( L); // ensure it is actually a deep userdata @@ -505,7 +505,7 @@ void* luaG_todeep( lua_State* L, luaG_IdFunction idfunc, int index) return NULL; // no metatable, or wrong kind } - proxy = (struct DEEP_PRELUDE**) lua_touserdata( L, index); + proxy = (DeepPrelude**) lua_touserdata( L, index); STACK_END( L, 0); return (*proxy)->deep; @@ -519,7 +519,7 @@ void* luaG_todeep( lua_State* L, luaG_IdFunction idfunc, int index) * the id function of the copied value, or NULL for non-deep userdata * (not copied) */ -luaG_IdFunction copydeep( struct s_Universe* U, lua_State* L, lua_State* L2, int index, enum eLookupMode mode_) +luaG_IdFunction copydeep( Universe* U, lua_State* L, lua_State* L2, int index, enum eLookupMode mode_) { char const* errmsg; luaG_IdFunction idfunc = get_idfunc( L, index, mode_); @@ -528,7 +528,7 @@ luaG_IdFunction copydeep( struct s_Universe* U, lua_State* L, lua_State* L2, int return NULL; // not a deep userdata } - errmsg = push_deep_proxy( U, L2, *(struct DEEP_PRELUDE**) lua_touserdata( L, index), mode_); + errmsg = push_deep_proxy( U, L2, *(DeepPrelude**) lua_touserdata( L, index), mode_); if( errmsg != NULL) { // raise the error in the proper state (not the keeper) diff --git a/src/deep.h b/src/deep.h index 8e999d6..366c5a8 100644 --- a/src/deep.h +++ b/src/deep.h @@ -6,14 +6,19 @@ * said modules will have to link against lanes (it is not really possible to separate the 'deep userdata' implementation from the rest of Lanes) */ - #include "lua.h" +// forwards +struct s_Universe; +typedef struct s_Universe Universe; + +#if !defined LANES_API // when deep is compiled standalone outside Lanes #if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) #define LANES_API __declspec(dllexport) #else #define LANES_API #endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) +#endif // LANES_API enum eDeepOp { @@ -25,6 +30,21 @@ enum eDeepOp typedef void* (*luaG_IdFunction)( lua_State* L, enum eDeepOp op_); +// ################################################################################################ + +// this is pointed to by full userdata proxies, and allocated with malloc() to survive any lua_State lifetime +struct s_DeepPrelude +{ + volatile int refcount; + void* deep; + // when stored in a keeper state, the full userdata doesn't have a metatable, so we need direct access to the idfunc + luaG_IdFunction idfunc; +}; +typedef struct s_DeepPrelude DeepPrelude; + +char const* push_deep_proxy( Universe* U, lua_State* L, DeepPrelude* prelude, enum eLookupMode mode_); +void free_deep_prelude( lua_State* L, DeepPrelude* prelude_); + extern LANES_API int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction idfunc); extern LANES_API void* luaG_todeep( lua_State* L, luaG_IdFunction idfunc, int index); extern LANES_API void luaG_pushdeepversion( lua_State* L); diff --git a/src/keeper.c b/src/keeper.c index dbf083f..94cf8d6 100644 --- a/src/keeper.c +++ b/src/keeper.c @@ -42,12 +42,12 @@ #include #include #include +#include -#include "threading.h" +#include "keeper.h" #include "compat.h" #include "tools.h" #include "universe.h" -#include "keeper.h" //################################################################################### // Keeper implementation @@ -184,9 +184,9 @@ static void push_table( lua_State* L, int idx_) STACK_END( L, 1); } -int keeper_push_linda_storage( struct s_Universe* U, lua_State* L, void* ptr_, ptrdiff_t magic_) +int keeper_push_linda_storage( Universe* U, lua_State* L, void* ptr_, ptrdiff_t magic_) { - struct s_Keeper* const K = keeper_acquire( U->keepers, magic_); + Keeper* const K = keeper_acquire( U->keepers, magic_); lua_State* const KL = K ? K->L : NULL; if( KL == NULL) return 0; STACK_GROW( KL, 4); @@ -576,7 +576,7 @@ int keepercall_count( lua_State* L) */ // called as __gc for the keepers array userdata -void close_keepers( struct s_Universe* U, lua_State* L) +void close_keepers( Universe* U, lua_State* L) { if( U->keepers != NULL) { @@ -609,7 +609,7 @@ void close_keepers( struct s_Universe* U, lua_State* L) { void* allocUD; lua_Alloc allocF = lua_getallocf( L, &allocUD); - allocF( allocUD, U->keepers, sizeof( struct s_Keepers) + (nbKeepers - 1) * sizeof(struct s_Keeper), 0); + allocF( allocUD, U->keepers, sizeof( Keepers) + (nbKeepers - 1) * sizeof( Keeper), 0); U->keepers = NULL; } } @@ -626,7 +626,7 @@ void close_keepers( struct s_Universe* U, lua_State* L) * function never fails. * settings table is at position 1 on the stack */ -void init_keepers( struct s_Universe* U, lua_State* L) +void init_keepers( Universe* U, lua_State* L) { int i; int nb_keepers; @@ -639,10 +639,10 @@ void init_keepers( struct s_Universe* U, lua_State* L) lua_pop( L, 1); // assert( nb_keepers >= 1); - // struct s_Keepers contains an array of 1 s_Keeper, adjust for the actual number of keeper states + // Keepers contains an array of 1 s_Keeper, adjust for the actual number of keeper states { - size_t const bytes = sizeof( struct s_Keepers) + (nb_keepers - 1) * sizeof(struct s_Keeper); - U->keepers = (struct s_Keepers*) allocF( allocUD, NULL, 0, bytes); + size_t const bytes = sizeof( Keepers) + (nb_keepers - 1) * sizeof( Keeper); + U->keepers = (Keepers*) allocF( allocUD, NULL, 0, bytes); if( U->keepers == NULL) { (void) luaL_error( L, "init_keepers() failed while creating keeper array; out of memory"); @@ -713,7 +713,7 @@ void init_keepers( struct s_Universe* U, lua_State* L) STACK_END( L, 0); } -struct s_Keeper* keeper_acquire( struct s_Keepers* keepers_, ptrdiff_t magic_) +Keeper* keeper_acquire( Keepers* keepers_, ptrdiff_t magic_) { int const nbKeepers = keepers_->nb_keepers; // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) @@ -731,7 +731,7 @@ struct s_Keeper* keeper_acquire( struct s_Keepers* keepers_, ptrdiff_t magic_) * have to cast to unsigned long to avoid compilation warnings about loss of data when converting pointer-to-integer */ unsigned int i = (unsigned int)((magic_ >> KEEPER_MAGIC_SHIFT) % nbKeepers); - struct s_Keeper* K = &keepers_->keeper_array[i]; + Keeper* K = &keepers_->keeper_array[i]; MUTEX_LOCK( &K->keeper_cs); //++ K->count; @@ -739,7 +739,7 @@ struct s_Keeper* keeper_acquire( struct s_Keepers* keepers_, ptrdiff_t magic_) } } -void keeper_release( struct s_Keeper* K) +void keeper_release( Keeper* K) { //-- K->count; if( K) MUTEX_UNLOCK( &K->keeper_cs); @@ -778,7 +778,7 @@ void keeper_toggle_nil_sentinels( lua_State* L, int val_i_, enum eLookupMode mod * * Returns: number of return values (pushed to 'L') or -1 in case of error */ -int keeper_call( struct s_Universe* U, lua_State* K, keeper_api_t func_, lua_State* L, void* linda, uint_t starting_index) +int keeper_call( Universe* U, lua_State* K, keeper_api_t func_, lua_State* L, void* linda, uint_t starting_index) { int const args = starting_index ? (lua_gettop( L) - starting_index + 1) : 0; int const Ktos = lua_gettop( K); diff --git a/src/keeper.h b/src/keeper.h index 7dbbc16..ac275c1 100644 --- a/src/keeper.h +++ b/src/keeper.h @@ -1,27 +1,36 @@ #if !defined( __keeper_h__) #define __keeper_h__ 1 +#include "lua.h" +#include "threading.h" + +// forwards +struct s_Universe; +typedef struct s_Universe Universe; + struct s_Keeper { MUTEX_T keeper_cs; lua_State* L; //int count; }; +typedef struct s_Keeper Keeper; struct s_Keepers { int nb_keepers; - struct s_Keeper keeper_array[1]; + Keeper keeper_array[1]; }; +typedef struct s_Keepers Keepers; -void init_keepers( struct s_Universe* U, lua_State* L); -void close_keepers( struct s_Universe* U, lua_State* L); +void init_keepers( Universe* U, lua_State* L); +void close_keepers( Universe* U, lua_State* L); -struct s_Keeper* keeper_acquire( struct s_Keepers* keepers_, ptrdiff_t magic_); +Keeper* keeper_acquire( Keepers* keepers_, ptrdiff_t magic_); #define KEEPER_MAGIC_SHIFT 3 -void keeper_release( struct s_Keeper* K); +void keeper_release( Keeper* K); void keeper_toggle_nil_sentinels( lua_State* L, int val_i_, enum eLookupMode const mode_); -int keeper_push_linda_storage( struct s_Universe* U, lua_State* L, void* ptr_, ptrdiff_t magic_); +int keeper_push_linda_storage( Universe* U, lua_State* L, void* ptr_, ptrdiff_t magic_); #define NIL_SENTINEL ((void*)keeper_toggle_nil_sentinels) @@ -38,6 +47,6 @@ int keepercall_get( lua_State* L); int keepercall_set( lua_State* L); int keepercall_count( lua_State* L); -int keeper_call( struct s_Universe* U, lua_State* K, keeper_api_t _func, lua_State* L, void* linda, uint_t starting_index); +int keeper_call( Universe* U, lua_State* K, keeper_api_t _func, lua_State* L, void* linda, uint_t starting_index); #endif // __keeper_h__ \ No newline at end of file diff --git a/src/lanes.c b/src/lanes.c index 3268c8b..0a04d88 100644 --- a/src/lanes.c +++ b/src/lanes.c @@ -85,13 +85,14 @@ THE SOFTWARE. #include #include #include +#include +#include "lanes.h" #include "threading.h" #include "compat.h" #include "tools.h" #include "universe.h" #include "keeper.h" -#include "lanes.h" #if !(defined( PLATFORM_XBOX) || defined( PLATFORM_WIN32) || defined( PLATFORM_POCKETPC)) # include @@ -122,7 +123,7 @@ enum e_cancel_request // NOTE: values to be changed by either thread, during execution, without // locking, are marked "volatile" // -struct s_lane +struct s_Lane { THREAD_T thread; // @@ -132,7 +133,7 @@ struct s_lane char const* debug_name; lua_State* L; - struct s_Universe* U; + Universe* U; // // M: prepares the state, and reads results // S: while S is running, M must keep out of modifying the state @@ -172,29 +173,30 @@ struct s_lane // M: sets to NORMAL, if issued a kill changes to KILLED // S: not used - struct s_lane* volatile selfdestruct_next; + struct s_Lane* volatile selfdestruct_next; // // M: sets to non-NULL if facing lane handle '__gc' cycle but the lane // is still running // S: cleans up after itself if non-NULL at lane exit #if HAVE_LANE_TRACKING - struct s_lane* volatile tracking_next; + struct s_Lane* volatile tracking_next; #endif // HAVE_LANE_TRACKING // // For tracking only }; +typedef struct s_Lane Lane; // To allow free-running threads (longer lifespan than the handle's) -// 'struct s_lane' are malloc/free'd and the handle only carries a pointer. +// 'Lane' are malloc/free'd and the handle only carries a pointer. // This is not deep userdata since the handle's not portable among lanes. // -#define lua_toLane( L, i) (*((struct s_lane**) luaL_checkudata( L, i, "Lane"))) +#define lua_toLane( L, i) (*((Lane**) luaL_checkudata( L, i, "Lane"))) #define CANCEL_TEST_KEY ((void*)get_lane_from_registry) // used as registry key -static inline struct s_lane* get_lane_from_registry( lua_State* L) +static inline Lane* get_lane_from_registry( lua_State* L) { - struct s_lane* s; + Lane* s; STACK_GROW( L, 1); STACK_CHECK( L); lua_pushlightuserdata( L, CANCEL_TEST_KEY); @@ -206,7 +208,7 @@ static inline struct s_lane* get_lane_from_registry( lua_State* L) } // intern the debug name in the specified lua state so that the pointer remains valid when the lane's state is closed -static void securize_debug_threadname( lua_State* L, struct s_lane* s) +static void securize_debug_threadname( lua_State* L, Lane* s) { STACK_CHECK( L); STACK_GROW( L, 3); @@ -231,7 +233,7 @@ static void securize_debug_threadname( lua_State* L, struct s_lane* s) */ static inline enum e_cancel_request cancel_test( lua_State* L) { - struct s_lane* const s = get_lane_from_registry( L); + Lane* const s = get_lane_from_registry( L); // 's' is NULL for the original main state (and no-one can cancel that) return s ? s->cancel_request : CANCEL_NONE; } @@ -320,15 +322,15 @@ static bool_t push_registry_table( lua_State* L, void* key, bool_t create) #if HAVE_LANE_TRACKING -// The chain is ended by '(struct s_lane*)(-1)', not NULL: +// The chain is ended by '(Lane*)(-1)', not NULL: // 'tracking_first -> ... -> ... -> (-1)' -#define TRACKING_END ((struct s_lane *)(-1)) +#define TRACKING_END ((Lane *)(-1)) /* * Add the lane to tracking chain; the ones still running at the end of the * whole process will be cancelled. */ -static void tracking_add( struct s_lane* s) +static void tracking_add( Lane* s) { MUTEX_LOCK( &s->U->tracking_cs); @@ -344,7 +346,7 @@ static void tracking_add( struct s_lane* s) /* * A free-running lane has ended; remove it from tracking chain */ -static bool_t tracking_remove( struct s_lane* s) +static bool_t tracking_remove( Lane* s) { bool_t found = FALSE; MUTEX_LOCK( &s->U->tracking_cs); @@ -355,7 +357,7 @@ static bool_t tracking_remove( struct s_lane* s) // if( s->tracking_next != NULL) { - struct s_lane** ref = (struct s_lane**) &s->U->tracking_first; + Lane** ref = (Lane**) &s->U->tracking_first; while( *ref != TRACKING_END) { @@ -366,7 +368,7 @@ static bool_t tracking_remove( struct s_lane* s) found = TRUE; break; } - ref = (struct s_lane**) &((*ref)->tracking_next); + ref = (Lane**) &((*ref)->tracking_next); } assert( found); } @@ -380,7 +382,7 @@ static bool_t tracking_remove( struct s_lane* s) //--- // low-level cleanup -static void lane_cleanup( struct s_lane* s) +static void lane_cleanup( Lane* s) { // Clean up after a (finished) thread // @@ -414,7 +416,7 @@ struct s_Linda { SIGNAL_T read_happened; SIGNAL_T write_happened; - struct s_Universe* U; // the universe this linda belongs to + Universe* U; // the universe this linda belongs to ptrdiff_t group; // a group to control keeper allocation between lindas enum e_cancel_request simulate_cancel; char name[1]; @@ -504,8 +506,8 @@ LUAG_FUNC( linda_send) { bool_t try_again = TRUE; - struct s_lane* const s = get_lane_from_registry( L); - struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); + Lane* const s = get_lane_from_registry( L); + Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); lua_State* KL = K ? K->L : NULL; // need to do this for 'STACK_CHECK' if( KL == NULL) return 0; STACK_CHECK( KL); @@ -666,8 +668,8 @@ LUAG_FUNC( linda_receive) { bool_t try_again = TRUE; - struct s_lane* const s = get_lane_from_registry( L); - struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); + Lane* const s = get_lane_from_registry( L); + Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); if( K == NULL) return 0; for( ;;) { @@ -770,7 +772,7 @@ LUAG_FUNC( linda_set) check_key_types( L, 2, 2); { - struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); + Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); if( K == NULL) return 0; if( linda->simulate_cancel == CANCEL_NONE) @@ -826,7 +828,7 @@ LUAG_FUNC( linda_count) check_key_types( L, 2, lua_gettop( L)); { - struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); + Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); if( K == NULL) return 0; pushed = keeper_call( linda->U, K->L, KEEPER_API( count), L, linda, 2); keeper_release( K); @@ -855,7 +857,7 @@ LUAG_FUNC( linda_get) // make sure the key is of a valid type (throws an error if not the case) check_key_types( L, 2, 2); { - struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); + Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); if( K == NULL) return 0; if( linda->simulate_cancel == CANCEL_NONE) @@ -904,7 +906,7 @@ LUAG_FUNC( linda_limit) check_key_types( L, 2, 2); { - struct s_Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); + Keeper* K = keeper_acquire( linda->U->keepers, LINDA_KEEPER_HASHSEED( linda)); if( K == NULL) return 0; if( linda->simulate_cancel == CANCEL_NONE) @@ -939,7 +941,7 @@ LUAG_FUNC( linda_cancel) { struct s_Linda* linda = lua_toLinda( L, 1); char const* who = luaL_optstring( L, 2, "both"); - struct s_Keeper* K; + Keeper* K; // make sure we got 3 arguments: the linda, a key and a limit luaL_argcheck( L, lua_gettop( L) <= 2, 2, "wrong number of arguments"); @@ -1171,7 +1173,7 @@ static void* linda_id( lua_State* L, enum eDeepOp op_) case eDO_delete: { - struct s_Keeper* K; + Keeper* K; struct s_Linda* linda = lua_touserdata( L, 1); ASSERT_L( linda); @@ -1435,7 +1437,7 @@ typedef enum CR_Killed } cancel_result; -static cancel_result thread_cancel( lua_State* L, struct s_lane* s, double secs, bool_t force, double waitkill_timeout_) +static cancel_result thread_cancel( lua_State* L, Lane* s, double secs, bool_t force, double waitkill_timeout_) { cancel_result result; @@ -1512,16 +1514,16 @@ static cancel_result thread_cancel( lua_State* L, struct s_lane* s, double secs, // // Protects modifying the selfdestruct chain -#define SELFDESTRUCT_END ((struct s_lane*)(-1)) +#define SELFDESTRUCT_END ((Lane*)(-1)) // - // The chain is ended by '(struct s_lane*)(-1)', not NULL: + // The chain is ended by '(Lane*)(-1)', not NULL: // 'selfdestruct_first -> ... -> ... -> (-1)' /* * Add the lane to selfdestruct chain; the ones still running at the end of the * whole process will be cancelled. */ -static void selfdestruct_add( struct s_lane* s) +static void selfdestruct_add( Lane* s) { MUTEX_LOCK( &s->U->selfdestruct_cs); assert( s->selfdestruct_next == NULL); @@ -1534,7 +1536,7 @@ static void selfdestruct_add( struct s_lane* s) /* * A free-running lane has ended; remove it from selfdestruct chain */ -static bool_t selfdestruct_remove( struct s_lane* s) +static bool_t selfdestruct_remove( Lane* s) { bool_t found = FALSE; MUTEX_LOCK( &s->U->selfdestruct_cs); @@ -1545,7 +1547,7 @@ static bool_t selfdestruct_remove( struct s_lane* s) // if( s->selfdestruct_next != NULL) { - struct s_lane** ref = (struct s_lane**) &s->U->selfdestruct_first; + Lane** ref = (Lane**) &s->U->selfdestruct_first; while( *ref != SELFDESTRUCT_END ) { @@ -1558,7 +1560,7 @@ static bool_t selfdestruct_remove( struct s_lane* s) found = TRUE; break; } - ref = (struct s_lane**) &((*ref)->selfdestruct_next); + ref = (Lane**) &((*ref)->selfdestruct_next); } assert( found); } @@ -1591,7 +1593,7 @@ void * protected_lua_Alloc( void *ud, void *ptr, size_t osize, size_t nsize) */ static int selfdestruct_gc( lua_State* L) { - struct s_Universe* U = (struct s_Universe*) lua_touserdata( L, 1); + Universe* U = (Universe*) lua_touserdata( L, 1); while( U->selfdestruct_first != SELFDESTRUCT_END) // true at most once! { @@ -1599,7 +1601,7 @@ static int selfdestruct_gc( lua_State* L) // MUTEX_LOCK( &U->selfdestruct_cs); { - struct s_lane* s = U->selfdestruct_first; + Lane* s = U->selfdestruct_first; while( s != SELFDESTRUCT_END) { // attempt a regular unforced hard cancel with a small timeout @@ -1645,7 +1647,7 @@ static int selfdestruct_gc( lua_State* L) double t_now = 0.0; MUTEX_LOCK( &U->selfdestruct_cs); { - struct s_lane* s = U->selfdestruct_first; + Lane* s = U->selfdestruct_first; while( s != SELFDESTRUCT_END) { if( s->cancel_request == CANCEL_HARD) @@ -1689,10 +1691,10 @@ static int selfdestruct_gc( lua_State* L) // these are not running, and the state can be closed MUTEX_LOCK( &U->selfdestruct_cs); { - struct s_lane* s = U->selfdestruct_first; + Lane* s = U->selfdestruct_first; while( s != SELFDESTRUCT_END) { - struct s_lane* next_s = s->selfdestruct_next; + Lane* next_s = s->selfdestruct_next; s->selfdestruct_next = NULL; // detach from selfdestruct chain if( !THREAD_ISNULL( s->thread)) // can be NULL if previous 'soft' termination succeeded { @@ -1719,7 +1721,7 @@ static int selfdestruct_gc( lua_State* L) lua_settop( L, 0); // no need to mutex-protect this as all threads in the universe are gone at that point -- U->timer_deep->refcount; // should be 0 now - free_deep_prelude( L, (struct DEEP_PRELUDE*) U->timer_deep); + free_deep_prelude( L, (DeepPrelude*) U->timer_deep); U->timer_deep = NULL; close_keepers( U, L); @@ -1959,7 +1961,7 @@ static void push_stack_trace( lua_State* L, int rc_, int stk_base_) LUAG_FUNC( set_debug_threadname) { // C s_lane structure is a light userdata upvalue - struct s_lane* s = lua_touserdata( L, lua_upvalueindex( 1)); + Lane* s = lua_touserdata( L, lua_upvalueindex( 1)); luaL_checktype( L, -1, LUA_TSTRING); // "name" // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... lua_pushlightuserdata( L, LG_set_debug_threadname); // "name" lud @@ -1975,7 +1977,7 @@ LUAG_FUNC( set_debug_threadname) LUAG_FUNC( get_debug_threadname) { - struct s_lane* const s = lua_toLane( L, 1); + Lane* const s = lua_toLane( L, 1); luaL_argcheck( L, lua_gettop( L) == 1, 2, "too many arguments"); lua_pushstring( L, s->debug_name); return 1; @@ -2031,7 +2033,7 @@ static char const* get_errcode_name( int _code) #if THREADWAIT_METHOD == THREADWAIT_CONDVAR // implies THREADAPI == THREADAPI_PTHREAD static void thread_cleanup_handler( void* opaque) { - struct s_lane* s= (struct s_lane*) opaque; + Lane* s= (Lane*) opaque; MUTEX_LOCK( &s->done_lock); s->status = CANCELLED; SIGNAL_ONE( &s->done_signal); // wake up master (while 's->done_lock' is on) @@ -2041,12 +2043,12 @@ static void thread_cleanup_handler( void* opaque) static THREAD_RETURN_T THREAD_CALLCONV lane_main( void* vs) { - struct s_lane* s = (struct s_lane*) vs; + Lane* s = (Lane*) vs; int rc, rc2; lua_State* L = s->L; // Called with the lane function and arguments on the stack int const nargs = lua_gettop( L) - 1; - DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); + DEBUGSPEW_CODE( Universe* U = universe_get( L)); THREAD_MAKE_ASYNCH_CANCELLABLE(); THREAD_CLEANUP_PUSH( thread_cleanup_handler, s); s->status = RUNNING; // PENDING -> RUNNING @@ -2144,7 +2146,7 @@ LUAG_FUNC( require) { char const* name = lua_tostring( L, 1); int const nargs = lua_gettop( L); - DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); + DEBUGSPEW_CODE( Universe* U = universe_get( L)); STACK_CHECK( L); DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END, name)); DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); @@ -2170,7 +2172,7 @@ LUAG_FUNC( register) // ignore extra parameters, just in case lua_settop( L, 2); luaL_argcheck( L, (mod_type == LUA_TTABLE) || (mod_type == LUA_TFUNCTION), 2, "unexpected module type"); - DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); + DEBUGSPEW_CODE( Universe* U = universe_get( L)); STACK_CHECK( L); // "name" mod_table DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); @@ -2200,8 +2202,8 @@ LUAG_FUNC( thread_gc); LUAG_FUNC( lane_new) { lua_State* L2; - struct s_lane* s; - struct s_lane** ud; + Lane* s; + Lane** ud; char const* libs_str = lua_tostring( L, 2); uint_t cancelstep_idx = luaG_optunsigned( L, 3, 0); @@ -2213,7 +2215,7 @@ LUAG_FUNC( lane_new) #define FIXED_ARGS 8 int const nargs = lua_gettop(L) - FIXED_ARGS; - struct s_Universe* U = universe_get( L); + Universe* U = universe_get( L); ASSERT_L( nargs >= 0); // public Lanes API accepts a generic range -3/+3 @@ -2386,8 +2388,8 @@ LUAG_FUNC( lane_new) // 's' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread) // - ud = lua_newuserdata( L, sizeof( struct s_lane*)); // func libs cancelstep priority globals package required gc_cb lane - s = *ud = (struct s_lane*) malloc( sizeof( struct s_lane)); + ud = lua_newuserdata( L, sizeof( Lane*)); // func libs cancelstep priority globals package required gc_cb lane + s = *ud = (Lane*) malloc( sizeof( Lane)); if( s == NULL) { return luaL_error( L, "could not create lane: out of memory"); @@ -2470,7 +2472,7 @@ LUAG_FUNC( lane_new) LUAG_FUNC( thread_gc) { bool_t have_gc_cb = FALSE; - struct s_lane* s = lua_toLane( L, 1); // ud + Lane* s = lua_toLane( L, 1); // ud // if there a gc callback? lua_getuservalue( L, 1); // ud uservalue @@ -2547,7 +2549,7 @@ LUAG_FUNC( thread_gc) // lane_h:cancel( [timeout] [, force [, forcekill_timeout]]) LUAG_FUNC( thread_cancel) { - struct s_lane* s = lua_toLane( L, 1); + Lane* s = lua_toLane( L, 1); double secs = 0.0; int force_i = 2; int forcekill_timeout_i = 3; @@ -2604,7 +2606,7 @@ LUAG_FUNC( thread_cancel) // / "error" finished at an error, error value is there // / "cancelled" execution cancelled by M (state gone) // -static char const * thread_status_string( struct s_lane* s) +static char const * thread_status_string( Lane* s) { enum e_status st = s->status; // read just once (volatile) char const* str = @@ -2618,7 +2620,7 @@ static char const * thread_status_string( struct s_lane* s) return str; } -static int push_thread_status( lua_State* L, struct s_lane* s) +static int push_thread_status( lua_State* L, Lane* s) { char const* const str = thread_status_string( s); ASSERT_L( str); @@ -2638,7 +2640,7 @@ static int push_thread_status( lua_State* L, struct s_lane* s) // LUAG_FUNC( thread_join) { - struct s_lane* const s = lua_toLane( L, 1); + Lane* const s = lua_toLane( L, 1); double wait_secs = luaL_optnumber( L, 2, -1.0); lua_State* L2 = s->L; int ret; @@ -2661,7 +2663,7 @@ LUAG_FUNC( thread_join) } else { - struct s_Universe* U = universe_get( L); + Universe* U = universe_get( L); // debug_name is a pointer to string possibly interned in the lane's state, that no longer exists when the state is closed // so store it in the userdata uservalue at a key that can't possibly collide securize_debug_threadname( L, s); @@ -2722,7 +2724,7 @@ LUAG_FUNC( thread_index) int const UD = 1; int const KEY = 2; int const USR = 3; - struct s_lane* const s = lua_toLane( L, UD); + Lane* const s = lua_toLane( L, UD); ASSERT_L( lua_gettop( L) == 2); STACK_GROW( L, 8); // up to 8 positions are needed in case of error propagation @@ -2873,14 +2875,14 @@ LUAG_FUNC( thread_index) LUAG_FUNC( threads) { int const top = lua_gettop( L); - struct s_Universe* U = universe_get( L); + Universe* U = universe_get( L); // List _all_ still running threads // MUTEX_LOCK( &U->tracking_cs); if( U->tracking_first && U->tracking_first != TRACKING_END) { - struct s_lane* s = U->tracking_first; + Lane* s = U->tracking_first; lua_newtable( L); // {} while( s != TRACKING_END) { @@ -3025,7 +3027,7 @@ static volatile long s_initCount = 0; // param 1: settings table LUAG_FUNC( configure) { - struct s_Universe* U = universe_get( L); + Universe* U = universe_get( L); bool_t const from_master_state = (U == NULL); char const* name = luaL_checkstring( L, lua_upvalueindex( 1)); _ASSERT_L( L, lua_type( L, 1) == LUA_TTABLE); @@ -3129,7 +3131,7 @@ LUAG_FUNC( configure) STACK_MID( L, 1); // Proxy userdata contents is only a 'DEEP_PRELUDE*' pointer - U->timer_deep = *(struct DEEP_PRELUDE**) lua_touserdata( L, -1); + U->timer_deep = *(DeepPrelude**) lua_touserdata( L, -1); ASSERT_L( U->timer_deep && (U->timer_deep->refcount == 1) && U->timer_deep->deep && U->timer_deep->idfunc == linda_id); // increment refcount that this linda remains alive as long as the universe is. ++ U->timer_deep->refcount; @@ -3159,7 +3161,7 @@ LUAG_FUNC( configure) { char const* errmsg; - errmsg = push_deep_proxy( U, L, (struct DEEP_PRELUDE*) U->timer_deep, eLM_LaneBody);// settings M timer_deep + errmsg = push_deep_proxy( U, L, (DeepPrelude*) U->timer_deep, eLM_LaneBody);// settings M timer_deep if( errmsg != NULL) { return luaL_error( L, errmsg); diff --git a/src/tools.c b/src/tools.c index 59037e6..3e3efeb 100644 --- a/src/tools.c +++ b/src/tools.c @@ -31,22 +31,22 @@ THE SOFTWARE. =============================================================================== */ -#include "compat.h" -#include "universe.h" -#include "tools.h" -#include "keeper.h" -#include "lanes.h" - #include #include #include #include #if !defined(__APPLE__) #include -#endif +#endif // __APPLE__ + +#include "tools.h" +#include "compat.h" +#include "universe.h" +#include "keeper.h" +#include "lanes.h" // functions implemented in deep.c -extern luaG_IdFunction copydeep( struct s_Universe* U, lua_State* L, lua_State* L2, int index, enum eLookupMode mode_); +extern luaG_IdFunction copydeep( Universe* U, lua_State* L, lua_State* L2, int index, enum eLookupMode mode_); extern void push_registry_subtable( lua_State* L, void* key_); char const* const CONFIG_REGKEY = "ee932492-a654-4506-9da8-f16540bdb5d4"; @@ -105,7 +105,7 @@ void luaG_dump( lua_State* L) fprintf( stderr, "\n"); } -void initialize_on_state_create( struct s_Universe* U, lua_State* L) +void initialize_on_state_create( Universe* U, lua_State* L) { STACK_CHECK( L); lua_getfield( L, -1, "on_state_create"); // settings on_state_create|nil @@ -139,7 +139,7 @@ void initialize_on_state_create( struct s_Universe* U, lua_State* L) // ################################################################################################ // just like lua_xmove, args are (from, to) -void luaG_copy_one_time_settings( struct s_Universe* U, lua_State* L, lua_State* L2) +void luaG_copy_one_time_settings( Universe* U, lua_State* L, lua_State* L2) { STACK_GROW( L, 1); // copy settings from from source to destination registry @@ -198,7 +198,7 @@ static const luaL_Reg libs[] = { NULL, NULL } }; -static void open1lib( struct s_Universe* U, lua_State* L, char const* name_, size_t len_, lua_State* from_) +static void open1lib( Universe* U, lua_State* L, char const* name_, size_t len_, lua_State* from_) { int i; for( i = 0; libs[i].name; ++ i) @@ -341,7 +341,7 @@ static void update_lookup_entry( lua_State* L, int _ctx_base, int _depth) size_t prevNameLength, newNameLength; char const* prevName; DEBUGSPEW_CODE( char const *newName); - DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); + DEBUGSPEW_CODE( Universe* U = universe_get( L)); STACK_CHECK( L); // first, raise an error if the function is already known @@ -412,7 +412,7 @@ static void populate_func_lookup_table_recur( lua_State* L, int _ctx_base, int _ int const cache = _ctx_base + 2; // we need to remember subtables to process them after functions encountered at the current depth (breadth-first search) int const breadth_first_cache = lua_gettop( L) + 1; - DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); + DEBUGSPEW_CODE( Universe* U = universe_get( L)); STACK_GROW( L, 6); // slot _i contains a table where we search for functions (or a full userdata with a metatable) @@ -530,7 +530,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_) int const ctx_base = lua_gettop( L) + 1; int const in_base = lua_absindex( L, _i); int start_depth = 0; - DEBUGSPEW_CODE( struct s_Universe* U = universe_get( L)); + DEBUGSPEW_CODE( Universe* U = universe_get( L)); DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "NULL")); DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); STACK_GROW( L, 3); @@ -585,7 +585,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_) DEBUGSPEW_CODE( -- U->debugspew_indent_depth); } -void call_on_state_create( struct s_Universe* U, lua_State* L, lua_State* from_, enum eLookupMode mode_) +void call_on_state_create( Universe* U, lua_State* L, lua_State* from_, enum eLookupMode mode_) { if( U->on_state_create_func != NULL) { @@ -630,7 +630,7 @@ void call_on_state_create( struct s_Universe* U, lua_State* L, lua_State* from_, * *NOT* called for keeper states! * */ -lua_State* luaG_newstate( struct s_Universe* U, lua_State* from_, char const* libs_) +lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_) { // re-use alloc function from the originating state #if PROPAGATE_ALLOCF @@ -761,7 +761,7 @@ lua_State* luaG_newstate( struct s_Universe* U, lua_State* from_, char const* li /* * Get a unique ID for metatable at [i]. */ -static uint_t get_mt_id( struct s_Universe* U, lua_State* L, int i) +static uint_t get_mt_id( Universe* U, lua_State* L, int i) { uint_t id; @@ -830,7 +830,7 @@ static int table_lookup_sentinel( lua_State* L) */ static char const* find_lookup_name( lua_State* L, uint_t i, enum eLookupMode mode_, char const* upName_, size_t* len_) { - DEBUGSPEW_CODE( struct s_Universe* const U = universe_get( L)); + DEBUGSPEW_CODE( Universe* const U = universe_get( L)); char const* fqn; ASSERT_L( lua_isfunction( L, i) || lua_istable( L, i)); // ... v ... STACK_CHECK( L); @@ -1276,9 +1276,9 @@ enum e_vt VT_KEY, VT_METATABLE }; -static bool_t inter_copy_one_( struct s_Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i, enum e_vt value_type, enum eLookupMode mode_, char const* upName_); +static bool_t inter_copy_one_( Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i, enum e_vt value_type, enum eLookupMode mode_, char const* upName_); -static void inter_copy_func( struct s_Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i, enum eLookupMode mode_, char const* upName_) +static void inter_copy_func( Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i, enum eLookupMode mode_, char const* upName_) { int n, needToPush; luaL_Buffer b; @@ -1427,7 +1427,7 @@ static void inter_copy_func( struct s_Universe* U, lua_State* L2, uint_t L2_cach * * Always pushes a function to 'L2'. */ -static void push_cached_func( struct s_Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i, enum eLookupMode mode_, char const* upName_) +static void push_cached_func( Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i, enum eLookupMode mode_, char const* upName_) { FuncSubType funcSubType; /*lua_CFunction cfunc =*/ luaG_tocfunction( L, i, &funcSubType); // NULL for LuaJIT-fast && bytecode functions @@ -1480,7 +1480,7 @@ static void push_cached_func( struct s_Universe* U, lua_State* L2, uint_t L2_cac } } -static void inter_copy_keyvaluepair( struct s_Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, enum e_vt vt, enum eLookupMode mode_, char const* upName_) +static void inter_copy_keyvaluepair( Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, enum e_vt vt, enum eLookupMode mode_, char const* upName_) { uint_t val_i = lua_gettop( L); uint_t key_i = val_i - 1; @@ -1542,7 +1542,7 @@ static void inter_copy_keyvaluepair( struct s_Universe* U, lua_State* L2, uint_t * * Returns TRUE if value was pushed, FALSE if its type is non-supported. */ -static bool_t inter_copy_one_( struct s_Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i, enum e_vt vt, enum eLookupMode mode_, char const* upName_) +static bool_t inter_copy_one_( Universe* U, lua_State* L2, uint_t L2_cache_i, lua_State* L, uint_t i, enum e_vt vt, enum eLookupMode mode_, char const* upName_) { bool_t ret = TRUE; bool_t ignore = FALSE; @@ -1809,7 +1809,7 @@ static bool_t inter_copy_one_( struct s_Universe* U, lua_State* L2, uint_t L2_ca * * Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'. */ -int luaG_inter_copy( struct s_Universe* U, lua_State* L, lua_State* L2, uint_t n, enum eLookupMode mode_) +int luaG_inter_copy( Universe* U, lua_State* L, lua_State* L2, uint_t n, enum eLookupMode mode_) { uint_t top_L = lua_gettop( L); uint_t top_L2 = lua_gettop( L2); @@ -1867,14 +1867,14 @@ int luaG_inter_copy( struct s_Universe* U, lua_State* L, lua_State* L2, uint_t n } -int luaG_inter_move( struct s_Universe* U, lua_State* L, lua_State* L2, uint_t n, enum eLookupMode mode_) +int luaG_inter_move( Universe* U, lua_State* L, lua_State* L2, uint_t n, enum eLookupMode mode_) { int ret = luaG_inter_copy( U, L, L2, n, mode_); lua_pop( L, (int) n); return ret; } -int luaG_inter_copy_package( struct s_Universe* U, lua_State* L, lua_State* L2, int package_idx_, enum eLookupMode mode_) +int luaG_inter_copy_package( Universe* U, lua_State* L, lua_State* L2, int package_idx_, enum eLookupMode mode_) { DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); @@ -1941,7 +1941,7 @@ int luaG_new_require( lua_State* L) { int rc, i; int args = lua_gettop( L); - struct s_Universe* U = universe_get( L); + Universe* U = universe_get( L); //char const* modname = luaL_checkstring( L, 1); STACK_GROW( L, args + 1); @@ -1974,7 +1974,7 @@ int luaG_new_require( lua_State* L) /* * Serialize calls to 'require', if it exists */ -void serialize_require( struct s_Universe* U, lua_State* L) +void serialize_require( Universe* U, lua_State* L) { STACK_GROW( L, 1); STACK_CHECK( L); diff --git a/src/tools.h b/src/tools.h index 9155747..ac69074 100644 --- a/src/tools.h +++ b/src/tools.h @@ -1,28 +1,15 @@ -/* -* TOOLS.H -*/ -#ifndef TOOLS_H -#define TOOLS_H +#ifndef __LANES_TOOLS_H__ +#define __LANES_TOOLS_H__ -#include "lauxlib.h" +//#include "lauxlib.h" #include "threading.h" #include "deep.h" - // MUTEX_T - -#include #include "macros_and_utils.h" -// ################################################################################################ - -// this is pointed to by full userdata proxies, and allocated with malloc() to survive any lua_State lifetime -struct DEEP_PRELUDE -{ - volatile int refcount; - void* deep; - // when stored in a keeper state, the full userdata doesn't have a metatable, so we need direct access to the idfunc - luaG_IdFunction idfunc; -}; +// forwards +struct s_Universe; +typedef struct s_Universe Universe; // ################################################################################################ @@ -33,8 +20,8 @@ struct DEEP_PRELUDE void luaG_dump( lua_State* L ); -lua_State* luaG_newstate( struct s_Universe* U, lua_State* _from, char const* libs); -void luaG_copy_one_time_settings( struct s_Universe* U, lua_State* L, lua_State* L2); +lua_State* luaG_newstate( Universe* U, lua_State* _from, char const* libs); +void luaG_copy_one_time_settings( Universe* U, lua_State* L, lua_State* L2); // ################################################################################################ @@ -45,25 +32,22 @@ enum eLookupMode eLM_FromKeeper // send a function from a keeper state to a lane }; -char const* push_deep_proxy( struct s_Universe* U, lua_State* L, struct DEEP_PRELUDE* prelude, enum eLookupMode mode_); -void free_deep_prelude( lua_State* L, struct DEEP_PRELUDE* prelude_); - -int luaG_inter_copy_package( struct s_Universe* U, lua_State* L, lua_State* L2, int package_idx_, enum eLookupMode mode_); +int luaG_inter_copy_package( Universe* U, lua_State* L, lua_State* L2, int package_idx_, enum eLookupMode mode_); -int luaG_inter_copy( struct s_Universe* U, lua_State* L, lua_State* L2, uint_t n, enum eLookupMode mode_); -int luaG_inter_move( struct s_Universe* U, lua_State* L, lua_State* L2, uint_t n, enum eLookupMode mode_); +int luaG_inter_copy( Universe* U, lua_State* L, lua_State* L2, uint_t n, enum eLookupMode mode_); +int luaG_inter_move( Universe* U, lua_State* L, lua_State* L2, uint_t n, enum eLookupMode mode_); int luaG_nameof( lua_State* L); int luaG_new_require( lua_State* L); void populate_func_lookup_table( lua_State* L, int _i, char const* _name); -void serialize_require( struct s_Universe* U, lua_State *L); -void initialize_on_state_create( struct s_Universe* U, lua_State* L); -void call_on_state_create( struct s_Universe* U, lua_State* L, lua_State* from_, enum eLookupMode mode_); +void serialize_require( Universe* U, lua_State *L); +void initialize_on_state_create( Universe* U, lua_State* L); +void call_on_state_create( Universe* U, lua_State* L, lua_State* from_, enum eLookupMode mode_); // ################################################################################################ extern char const* const CONFIG_REGKEY; extern char const* const LOOKUP_REGKEY; -#endif // TOOLS_H +#endif // __LANES_TOOLS_H__ diff --git a/src/universe.c b/src/universe.c index ba78396..8bcdcfe 100644 --- a/src/universe.c +++ b/src/universe.c @@ -28,19 +28,19 @@ THE SOFTWARE. =============================================================================== */ +#include "universe.h" #include "compat.h" #include "macros_and_utils.h" -#include "universe.h" // crc64/we of string "UNIVERSE_REGKEY" generated at https://www.nitrxgen.net/hashgen/ static void* const UNIVERSE_REGKEY = ((void*)0x9f877b2cf078f17f); // ################################################################################################ -struct s_Universe* universe_create( lua_State* L) +Universe* universe_create( lua_State* L) { - struct s_Universe* U = (struct s_Universe*) lua_newuserdata( L, sizeof(struct s_Universe)); // universe - memset( U, 0, sizeof( struct s_Universe)); + Universe* U = (Universe*) lua_newuserdata( L, sizeof(Universe)); // universe + memset( U, 0, sizeof( Universe)); lua_pushlightuserdata( L, UNIVERSE_REGKEY); // universe UNIVERSE_REGKEY lua_pushvalue( L, -2); // universe UNIVERSE_REGKEY universe lua_rawset( L, LUA_REGISTRYINDEX); // universe @@ -49,7 +49,7 @@ struct s_Universe* universe_create( lua_State* L) // ################################################################################################ -void universe_store( lua_State* L, struct s_Universe* U) +void universe_store( lua_State* L, Universe* U) { STACK_CHECK( L); lua_pushlightuserdata( L, UNIVERSE_REGKEY); @@ -60,9 +60,9 @@ void universe_store( lua_State* L, struct s_Universe* U) // ################################################################################################ -struct s_Universe* universe_get( lua_State* L) +Universe* universe_get( lua_State* L) { - struct s_Universe* universe; + Universe* universe; STACK_GROW( L, 2); STACK_CHECK( L); lua_pushlightuserdata( L, UNIVERSE_REGKEY); diff --git a/src/universe.h b/src/universe.h index 0ca5bf7..a75cead 100644 --- a/src/universe.h +++ b/src/universe.h @@ -6,7 +6,14 @@ #include "lua.h" #include "threading.h" -// MUTEX_T + +// forwards +struct s_DeepPrelude; +typedef struct s_DeepPrelude DeepPrelude; +struct s_Keepers; +typedef struct s_Keepers Keepers; +struct s_Lane; +typedef struct s_Lane Lane; // ################################################################################################ @@ -27,15 +34,15 @@ struct s_Universe lua_CFunction on_state_create_func; - struct s_Keepers* keepers; + Keepers* keepers; // Initialized by 'init_once_LOCKED()': the deep userdata Linda object // used for timers (each lane will get a proxy to this) - volatile struct DEEP_PRELUDE* timer_deep; // = NULL + volatile DeepPrelude* timer_deep; // = NULL #if HAVE_LANE_TRACKING MUTEX_T tracking_cs; - struct s_lane* volatile tracking_first; // will change to TRACKING_END if we want to activate tracking + Lane* volatile tracking_first; // will change to TRACKING_END if we want to activate tracking #endif // HAVE_LANE_TRACKING MUTEX_T selfdestruct_cs; @@ -53,14 +60,15 @@ struct s_Universe int debugspew_indent_depth; #endif // USE_DEBUG_SPEW - struct s_lane* volatile selfdestruct_first; + Lane* volatile selfdestruct_first; // After a lane has removed itself from the chain, it still performs some processing. // The terminal desinit sequence should wait for all such processing to terminate before force-killing threads int volatile selfdestructing_count; }; +typedef struct s_Universe Universe; -struct s_Universe* universe_get( lua_State* L); -struct s_Universe* universe_create( lua_State* L); -void universe_store( lua_State* L, struct s_Universe* U); +Universe* universe_get( lua_State* L); +Universe* universe_create( lua_State* L); +void universe_store( lua_State* L, Universe* U); #endif // UNIVERSE_H -- cgit v1.2.3-55-g6feb