From 4343dc14c1db7ef9e5d57ed1d40740439e1c51d6 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Fri, 26 Apr 2024 10:17:09 +0200 Subject: Formalize a few coding style rules, start to enforce them for constants --- src/cancel.cpp | 2 +- src/cancel.h | 6 +- src/deep.cpp | 18 +++--- src/deep.h | 6 +- src/keeper.cpp | 72 +++++++++++----------- src/keeper.h | 4 +- src/lanes.cpp | 170 +++++++++++++++++++++++++++------------------------- src/lanes_private.h | 4 +- src/lanesconf.h | 26 ++++++++ src/linda.cpp | 42 ++++++------- src/linda.h | 1 + src/state.cpp | 12 ++-- src/threading.cpp | 2 +- src/threading.h | 12 ++-- src/tools.cpp | 74 +++++++++++------------ src/tools.h | 8 +-- src/universe.cpp | 16 ++--- src/universe.h | 1 + 18 files changed, 251 insertions(+), 225 deletions(-) (limited to 'src') diff --git a/src/cancel.cpp b/src/cancel.cpp index 5bcf79d..0ec5eb5 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -53,7 +53,7 @@ THE SOFTWARE. */ [[nodiscard]] static inline CancelRequest cancel_test(lua_State* L) { - Lane* const lane{ LANE_POINTER_REGKEY.readLightUserDataValue(L) }; + Lane* const lane{ kLanePointerRegKey.readLightUserDataValue(L) }; // 'lane' is nullptr for the original main state (and no-one can cancel that) return lane ? lane->cancel_request : CancelRequest::None; } diff --git a/src/cancel.h b/src/cancel.h index a25c078..d5c3f7b 100644 --- a/src/cancel.h +++ b/src/cancel.h @@ -44,8 +44,8 @@ enum class CancelOp MaskCount = LUA_MASKCOUNT, }; -// crc64/we of string "CANCEL_ERROR" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey CANCEL_ERROR{ 0xE97D41626CC97577ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel +// xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator +static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel [[nodiscard]] CancelOp which_cancel_op(char const* op_string_); [[nodiscard]] CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hook_count_, lua_Duration secs_, bool wake_lindas_); @@ -53,7 +53,7 @@ static constexpr UniqueKey CANCEL_ERROR{ 0xE97D41626CC97577ull, "lanes.cancel_er [[noreturn]] static inline void raise_cancel_error(lua_State* L) { STACK_GROW(L, 1); - CANCEL_ERROR.pushKey(L); // special error value + kCancelError.pushKey(L); // special error value raise_lua_error(L); // doesn't return } diff --git a/src/deep.cpp b/src/deep.cpp index 241cb95..d7efe68 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -53,14 +53,14 @@ THE SOFTWARE. * metatable -> factory * factory -> metatable */ -// crc64/we of string "DEEP_LOOKUP_KEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey DEEP_LOOKUP_KEY{ 0x9FB9B4F3F633D83Dull }; +// xxh64 of string "kDeepLookupRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kDeepLookupRegKey{ 0xC6788345703C6059ull }; /* * The deep proxy cache is a weak valued table listing all deep UD proxies indexed by the deep UD that they are proxying - * crc64/we of string "DEEP_PROXY_CACHE_KEY" generated at http://www.nitrxgen.net/hashgen/ + * xxh64 of string "kDeepProxyCacheRegKey" generated at https://www.pelock.com/products/hash-calculator */ -static constexpr RegistryUniqueKey DEEP_PROXY_CACHE_KEY{ 0x05773D6FC26BE106ull }; +static constexpr RegistryUniqueKey kDeepProxyCacheRegKey{ 0xEBCD49AE1A3DD35Eull }; /* * Sets up [-1]<->[-2] two-way lookups, and ensures the lookup table exists. @@ -70,7 +70,7 @@ static void set_deep_lookup(lua_State* L) { STACK_GROW( L, 3); STACK_CHECK_START_REL(L, 2); // a b - push_registry_subtable( L, DEEP_LOOKUP_KEY); // a b {} + push_registry_subtable( L, kDeepLookupRegKey); // a b {} STACK_CHECK( L, 3); lua_insert( L, -3); // {} a b lua_pushvalue( L, -1); // {} a b b @@ -91,7 +91,7 @@ static void get_deep_lookup(lua_State* L) { STACK_GROW( L, 1); STACK_CHECK_START_REL(L, 1); // a - DEEP_LOOKUP_KEY.pushValue(L); // a {} + kDeepLookupRegKey.pushValue(L); // a {} if (!lua_isnil( L, -1)) { lua_insert( L, -2); // {} a @@ -194,7 +194,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) char const* DeepFactory::PushDeepProxy(DestState L, DeepPrelude* prelude, int nuv_, LookupMode mode_) { // Check if a proxy already exists - push_registry_subtable_mode(L, DEEP_PROXY_CACHE_KEY, "v"); // DPC + push_registry_subtable_mode(L, kDeepProxyCacheRegKey, "v"); // DPC lua_pushlightuserdata(L, prelude); // DPC deep lua_rawget(L, -2); // DPC proxy if (!lua_isnil(L, -1)) @@ -354,11 +354,11 @@ int DeepFactory::pushDeepUserdata(DestState L, int nuv_) const return luaL_error( L, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); } - if (prelude->m_magic != DEEP_VERSION) + if (prelude->m_magic != kDeepVersion) { // just in case, don't leak the newly allocated deep userdata object deleteDeepObjectInternal(L, prelude); - return luaL_error( L, "Bad Deep Factory: DEEP_VERSION is incorrect, rebuild your implementation with the latest deep implementation"); + return luaL_error( L, "Bad Deep Factory: kDeepVersion is incorrect, rebuild your implementation with the latest deep implementation"); } LUA_ASSERT(L, prelude->m_refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 diff --git a/src/deep.h b/src/deep.h index 28069fe..673e93a 100644 --- a/src/deep.h +++ b/src/deep.h @@ -30,14 +30,14 @@ enum class LookupMode // ################################################################################################# -// xxh64 of string "DEEP_VERSION_3" generated at https://www.pelock.com/products/hash-calculator -static constexpr UniqueKey DEEP_VERSION{ 0xB2CC0FD9C0AE9674ull, "DEEP_VERSION_3" }; +// xxh64 of string "kDeepVersion_1" generated at https://www.pelock.com/products/hash-calculator +static constexpr UniqueKey kDeepVersion{ 0x91171AEC6641E9DBull, "kDeepVersion" }; // should be used as header for deep userdata // a deep userdata is a full userdata that stores a single pointer to the actual DeepPrelude-derived object struct DeepPrelude { - UniqueKey const m_magic{ DEEP_VERSION }; + UniqueKey const m_magic{ kDeepVersion }; // when stored in a keeper state, the full userdata doesn't have a metatable, so we need direct access to the factory class DeepFactory& m_factory; // data is destroyed when refcount is 0 diff --git a/src/keeper.cpp b/src/keeper.cpp index c306d58..682d70d 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -73,7 +73,7 @@ class keeper_fifo } }; -static constexpr int CONTENTS_TABLE{ 1 }; +static constexpr int kContentsTableIndex{ 1 }; // ################################################################################################# @@ -86,7 +86,7 @@ static constexpr int CONTENTS_TABLE{ 1 }; idx_ = lua_absindex(L, idx_); STACK_GROW(L, 1); // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around - lua_getiuservalue(L, idx_, CONTENTS_TABLE); + lua_getiuservalue(L, idx_, kContentsTableIndex); lua_replace(L, idx_); } return fifo; @@ -103,7 +103,7 @@ static constexpr int CONTENTS_TABLE{ 1 }; keeper_fifo* const fifo{ new (L) keeper_fifo{} }; STACK_CHECK(L, 1); lua_newtable(L); - lua_setiuservalue(L, -2, CONTENTS_TABLE); + lua_setiuservalue(L, -2, kContentsTableIndex); STACK_CHECK(L, 1); return fifo; } @@ -182,14 +182,14 @@ static void fifo_pop( lua_State* L, keeper_fifo* fifo_, int count_) // in: linda_ud expected at stack slot idx // out: fifos[ud] -// crc64/we of string "FIFOS_KEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey FIFOS_KEY{ 0xDCE50BBC351CD465ull }; +// xxh64 of string "kFifosRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kFifosRegKey{ 0x37F11CE5A6D191AAull }; static void push_table(lua_State* L, int idx_) { STACK_GROW(L, 5); STACK_CHECK_START_REL(L, 0); idx_ = lua_absindex(L, idx_); - FIFOS_KEY.pushValue(L); // ud fifos + kFifosRegKey.pushValue(L); // ud fifos lua_pushvalue(L, idx_); // ud fifos ud lua_rawget(L, -2); // ud fifos fifos[ud] STACK_CHECK(L, 2); @@ -217,7 +217,7 @@ int keeper_push_linda_storage(Linda& linda_, DestState L) return 0; STACK_GROW(KL, 4); // KEEPER MAIN STACK_CHECK_START_REL(KL, 0); - FIFOS_KEY.pushValue(KL); // fifos + kFifosRegKey.pushValue(KL); // fifos lua_pushlightuserdata(KL, &linda_); // fifos ud lua_rawget(KL, -2); // fifos storage lua_remove(KL, -2); // storage @@ -267,7 +267,7 @@ int keepercall_clear(lua_State* L) { STACK_GROW(L, 3); STACK_CHECK_START_REL(L, 0); - FIFOS_KEY.pushValue(L); // ud fifos + kFifosRegKey.pushValue(L); // ud fifos lua_pushvalue(L, 1); // ud fifos ud lua_pushnil(L); // ud fifos ud nil lua_rawset(L, -3); // ud fifos @@ -428,31 +428,31 @@ int keepercall_set(lua_State* L) STACK_GROW(L, 6); // retrieve fifos associated with the linda - push_table(L, 1); // ud key [val [, ...]] fifos - lua_replace(L, 1); // fifos key [val [, ...]] + push_table(L, 1); // ud key [val [, ...]] fifos + lua_replace(L, 1); // fifos key [val [, ...]] // make sure we have a value on the stack - if (lua_gettop(L) == 2) // fifos key + if (lua_gettop(L) == 2) // fifos key { - lua_pushvalue(L, -1); // fifos key key - lua_rawget(L, 1); // fifos key fifo|nil + lua_pushvalue(L, -1); // fifos key key + lua_rawget(L, 1); // fifos key fifo|nil // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! keeper_fifo* const fifo{ keeper_fifo::getPtr(L, -1) }; if (fifo != nullptr) // might be nullptr if we set a nonexistent key to nil { // fifos key fifo if (fifo->limit < 0) // fifo limit value is the default (unlimited): we can totally remove it { - lua_pop(L, 1); // fifos key - lua_pushnil(L); // fifos key nil - lua_rawset(L, -3); // fifos + lua_pop(L, 1); // fifos key + lua_pushnil(L); // fifos key nil + lua_rawset(L, -3); // fifos } else { // we create room if the fifo was full but it is no longer the case should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit); - lua_remove(L, -2); // fifos fifo - lua_newtable(L); // fifos fifo {} - lua_setiuservalue(L, -2, CONTENTS_TABLE); // fifos fifo + lua_remove(L, -2); // fifos fifo + lua_newtable(L); // fifos fifo {} + lua_setiuservalue(L, -2, kContentsTableIndex); // fifos fifo fifo->first = 1; fifo->count = 0; } @@ -461,32 +461,32 @@ int keepercall_set(lua_State* L) else // set/replace contents stored at the specified key? { int const count{ lua_gettop(L) - 2 }; // number of items we want to store - lua_pushvalue(L, 2); // fifos key [val [, ...]] key - lua_rawget(L, 1); // fifos key [val [, ...]] fifo|nil + lua_pushvalue(L, 2); // fifos key [val [, ...]] key + lua_rawget(L, 1); // fifos key [val [, ...]] fifo|nil keeper_fifo* fifo{ keeper_fifo::getPtr(L, -1) }; if (fifo == nullptr) // can be nullptr if we store a value at a new key - { // fifos key [val [, ...]] nil + { // fifos key [val [, ...]] nil // no need to wake writers in that case, because a writer can't wait on an inexistent key - lua_pop(L, 1); // fifos key [val [, ...]] - std::ignore = fifo_new(KeeperState{ L }); // fifos key [val [, ...]] fifo - lua_pushvalue(L, 2); // fifos key [val [, ...]] fifo key - lua_pushvalue(L, -2); // fifos key [val [, ...]] fifo key fifo - lua_rawset(L, 1); // fifos key [val [, ...]] fifo + lua_pop(L, 1); // fifos key [val [, ...]] + std::ignore = fifo_new(KeeperState{ L }); // fifos key [val [, ...]] fifo + lua_pushvalue(L, 2); // fifos key [val [, ...]] fifo key + lua_pushvalue(L, -2); // fifos key [val [, ...]] fifo key fifo + lua_rawset(L, 1); // fifos key [val [, ...]] fifo } else // the fifo exists, we just want to update its contents - { // fifos key [val [, ...]] fifo + { // fifos key [val [, ...]] fifo // we create room if the fifo was full but it is no longer the case should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit) && (count < fifo->limit); // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! - lua_newtable(L); // fifos key [val [, ...]] fifo {} - lua_setiuservalue(L, -2, CONTENTS_TABLE); // fifos key [val [, ...]] fifo + lua_newtable(L); // fifos key [val [, ...]] fifo {} + lua_setiuservalue(L, -2, kContentsTableIndex); // fifos key [val [, ...]] fifo fifo->first = 1; fifo->count = 0; } - fifo = prepare_fifo_access(L, -1); // fifos key [val [, ...]] fifotbl + fifo = prepare_fifo_access(L, -1); // fifos key [val [, ...]] fifotbl // move the fifo below the values we want to store - lua_insert(L, 3); // fifos key fifotbl [val [, ...]] - fifo_push(L, fifo, count); // fifos key fifotbl + lua_insert(L, 3); // fifos key fifotbl [val [, ...]] + fifo_push(L, fifo, count); // fifos key fifotbl } return should_wake_writers ? (lua_pushboolean(L, 1), 1) : 0; } @@ -742,7 +742,7 @@ void init_keepers(Universe* U, lua_State* L) lua_pushfstring(K, "Keeper #%d", i + 1); // "Keeper #n" lua_setglobal(K, "decoda_name"); // // create the fifos table in the keeper state - FIFOS_KEY.setValue(K, [](lua_State* L) { lua_newtable(L); }); + kFifosRegKey.setValue(K, [](lua_State* L) { lua_newtable(L); }); STACK_CHECK(K, 0); } STACK_CHECK(L, 0); @@ -785,13 +785,13 @@ void keeper_toggle_nil_sentinels(lua_State* L, int val_i_, LookupMode const mode { if (lua_isnil(L, i)) { - NIL_SENTINEL.pushKey(L); + kNilSentinel.pushKey(L); lua_replace(L, i); } } else { - if (NIL_SENTINEL.equals(L, i)) + if (kNilSentinel.equals(L, i)) { lua_pushnil(L); lua_replace(L, i); diff --git a/src/keeper.h b/src/keeper.h index 4c79fd7..27a4cff 100644 --- a/src/keeper.h +++ b/src/keeper.h @@ -35,8 +35,8 @@ struct Keepers Keeper keeper_array[1]; }; -// crc64/we of string "NIL_SENTINEL" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey NIL_SENTINEL{ 0x7EAAFA003A1D11A1ull, "linda.null" }; +// xxh64 of string "kNilSentinel" generated at https://www.pelock.com/products/hash-calculator +static constexpr UniqueKey kNilSentinel{ 0xC457D4EDDB05B5E4ull, "lanes.null" }; void init_keepers(Universe* U, lua_State* L); void close_keepers(Universe* U); diff --git a/src/lanes.cpp b/src/lanes.cpp index d549449..1a4e8b7 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -189,7 +189,7 @@ static void lane_main(Lane* lane); void Lane::startThread(int priority_) { m_thread = std::jthread([this]() { lane_main(this); }); - if (priority_ != THREAD_PRIO_DEFAULT) + if (priority_ != kThreadPrioDefault) { JTHREAD_SET_PRIORITY(m_thread, priority_, U->m_sudo); } @@ -218,8 +218,8 @@ static void securize_debug_threadname(lua_State* L, Lane* lane_) #if ERROR_FULL_STACK [[nodiscard]] static int lane_error(lua_State* L); -// crc64/we of string "STACKTRACE_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey STACKTRACE_REGKEY{ 0x534AF7D3226A429Full }; +// xxh64 of string "kStackTraceRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kStackTraceRegKey{ 0x3F327747CACAA904ull }; #endif // ERROR_FULL_STACK /* @@ -230,8 +230,8 @@ static constexpr RegistryUniqueKey STACKTRACE_REGKEY{ 0x534AF7D3226A429Full }; * error (and maybe stack trace) parameters to the finalizer functions would * anyways complicate that approach. */ -// crc64/we of string "FINALIZER_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey FINALIZER_REGKEY{ 0x188FCCB8BF348E09ull }; +// xxh64 of string "kFinalizerRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kFinalizerRegKey{ 0xFE936BFAA718FEEAull }; // ################################################################################################# @@ -260,13 +260,13 @@ static void push_finalizers_table(lua_State* L) STACK_GROW(L, 3); STACK_CHECK_START_REL(L, 0); - FINALIZER_REGKEY.pushValue(L); // ? + kFinalizerRegKey.pushValue(L); // ? if (lua_isnil(L, -1)) // nil? { lua_pop(L, 1); // // store a newly created table in the registry, but leave it on the stack too lua_newtable(L); // t - FINALIZER_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); // t + kFinalizerRegKey.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); // t } STACK_CHECK(L, 1); } @@ -312,12 +312,12 @@ static void push_stack_trace(lua_State* L, int rc_, int stk_base_) // fetch the call stack table from the registry where the handler stored it STACK_GROW(L, 1); // yields nil if no stack was generated (in case of cancellation for example) - STACKTRACE_REGKEY.pushValue(L); // err trace|nil + kStackTraceRegKey.pushValue(L); // err trace|nil STACK_CHECK(L, 1); - // For cancellation the error message is CANCEL_ERROR, and a stack trace isn't placed + // For cancellation the error message is kCancelError, and a stack trace isn't placed // For other errors, the message can be whatever was thrown, and we should have a stack trace table - LUA_ASSERT(L, lua_type(L, 1 + stk_base_) == (CANCEL_ERROR.equals(L, stk_base_) ? LUA_TNIL : LUA_TTABLE)); + LUA_ASSERT(L, lua_type(L, 1 + stk_base_) == (kCancelError.equals(L, stk_base_) ? LUA_TNIL : LUA_TTABLE)); // Just leaving the stack trace table on the stack is enough to get it through to the master. break; } @@ -326,8 +326,8 @@ static void push_stack_trace(lua_State* L, int rc_, int stk_base_) case LUA_ERRMEM: // memory allocation error (handler not called) case LUA_ERRERR: // error while running the error handler (if any, for example an out-of-memory condition) default: - // we should have a single value which is either a string (the error message) or CANCEL_ERROR - LUA_ASSERT(L, (lua_gettop(L) == stk_base_) && ((lua_type(L, stk_base_) == LUA_TSTRING) || CANCEL_ERROR.equals(L, stk_base_))); + // we should have a single value which is either a string (the error message) or kCancelError + LUA_ASSERT(L, (lua_gettop(L) == stk_base_) && ((lua_type(L, stk_base_) == LUA_TSTRING) || kCancelError.equals(L, stk_base_))); break; } } @@ -349,7 +349,7 @@ static void push_stack_trace(lua_State* L, int rc_, int stk_base_) [[nodiscard]] static int run_finalizers(lua_State* L, int lua_rc_) { - FINALIZER_REGKEY.pushValue(L); // ... finalizers? + kFinalizerRegKey.pushValue(L); // ... finalizers? if (lua_isnil(L, -1)) { lua_pop(L, 1); @@ -373,7 +373,7 @@ static void push_stack_trace(lua_State* L, int rc_, int stk_base_) LUA_ASSERT(L, finalizers_index == 2 || finalizers_index == 3); //char const* err_msg = lua_tostring(L, 1); lua_pushvalue(L, 1); // ... finalizers lane_error finalizer err_msg - // note we don't always have a stack trace for example when CANCEL_ERROR, or when we got an error that doesn't call our handler, such as LUA_ERRMEM + // note we don't always have a stack trace for example when kCancelError, or when we got an error that doesn't call our handler, such as LUA_ERRMEM if (finalizers_index == 3) { lua_pushvalue(L, 2); // ... finalizers lane_error finalizer err_msg stack_trace @@ -633,8 +633,8 @@ LUAG_FUNC( set_singlethreaded) */ #if ERROR_FULL_STACK -// crc64/we of string "EXTENDED_STACKTRACE_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey EXTENDED_STACKTRACE_REGKEY{ 0x2357C69A7C92C936ull }; // used as registry key +// xxh64 of string "kExtendedStackTraceRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kExtendedStackTraceRegKey{ 0x38147AD48FB426E2ull }; // used as registry key LUAG_FUNC( set_error_reporting) { @@ -648,7 +648,7 @@ LUAG_FUNC( set_error_reporting) return luaL_error(L, "unsupported error reporting model %s", mode); } - EXTENDED_STACKTRACE_REGKEY.setValue(L, [extended](lua_State* L) { lua_pushboolean(L, extended ? 1 : 0); }); + kExtendedStackTraceRegKey.setValue(L, [extended](lua_State* L) { lua_pushboolean(L, extended ? 1 : 0); }); return 0; } @@ -659,16 +659,16 @@ LUAG_FUNC( set_error_reporting) // Don't do stack survey for cancelled lanes. // - if (CANCEL_ERROR.equals(L, 1)) + if (kCancelError.equals(L, 1)) { return 1; // just pass on } STACK_GROW(L, 3); - bool const extended{ EXTENDED_STACKTRACE_REGKEY.readBoolValue(L) }; + bool const extended{ kExtendedStackTraceRegKey.readBoolValue(L) }; STACK_CHECK(L, 1); - // Place stack trace at 'registry[STACKTRACE_REGKEY]' for the 'lua_pcall()' + // Place stack trace at 'registry[kStackTraceRegKey]' for the 'lua_pcall()' // caller to fetch. This bypasses the Lua 5.1 limitation of only one // return value from error handler to 'lua_pcall()' caller. @@ -719,7 +719,7 @@ LUAG_FUNC( set_error_reporting) } // store the stack trace table in the registry - STACKTRACE_REGKEY.setValue(L, [](lua_State* L) { lua_insert(L, -2); }); // some_error + kStackTraceRegKey.setValue(L, [](lua_State* L) { lua_insert(L, -2); }); // some_error STACK_CHECK(L, 1); return 1; // the untouched error value @@ -767,9 +767,9 @@ LUAG_FUNC(set_thread_priority) // public Lanes API accepts a generic range -3/+3 // that will be remapped into the platform-specific scheduler priority scheme // On some platforms, -3 is equivalent to -2 and +3 to +2 - if (prio < THREAD_PRIO_MIN || prio > THREAD_PRIO_MAX) + if (prio < kThreadPrioMin || prio > kThreadPrioMax) { - return luaL_error(L, "priority out of range: %d..+%d (%d)", THREAD_PRIO_MIN, THREAD_PRIO_MAX, prio); + return luaL_error(L, "priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, prio); } THREAD_SET_PRIORITY(static_cast(prio), universe_get(L)->m_sudo); return 0; @@ -870,7 +870,7 @@ static void lane_main(Lane* lane) // in case of error and if it exists, fetch stack trace from registry and push it push_stack_trace(L, rc, 1); // retvals|error [trace] - DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "Lane %p body: %s (%s)\n" INDENT_END, L, get_errcode_name(rc), CANCEL_ERROR.equals(L, 1) ? "cancelled" : lua_typename(L, lua_type(L, 1)))); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "Lane %p body: %s (%s)\n" INDENT_END, L, get_errcode_name(rc), kCancelError.equals(L, 1) ? "cancelled" : lua_typename(L, lua_type(L, 1)))); // Call finalizers, if the script has set them up. // int rc2{ run_finalizers(L, rc) }; @@ -901,7 +901,7 @@ static void lane_main(Lane* lane) { // leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them - Lane::Status st = (rc == LUA_OK) ? Lane::Done : CANCEL_ERROR.equals(L, 1) ? Lane::Cancelled : Lane::Error; + Lane::Status st = (rc == LUA_OK) ? Lane::Done : kCancelError.equals(L, 1) ? Lane::Cancelled : Lane::Error; { // 'm_done_mutex' protects the -> Done|Error|Cancelled state change @@ -959,8 +959,8 @@ LUAG_FUNC(register) // ################################################################################################# -// crc64/we of string "GCCB_KEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey GCCB_KEY{ 0xCFB1F046EF074E88ull }; +// xxh64 of string "kLaneGC" generated at https://www.pelock.com/products/hash-calculator +static constexpr UniqueKey kLaneGC{ 0x5D6122141727F960ull }; //--- // lane_ud = lane_new( function @@ -978,23 +978,23 @@ LUAG_FUNC(lane_new) { char const* const libs_str{ lua_tostring(L, 2) }; bool const have_priority{ !lua_isnoneornil(L, 3) }; - int const priority{ have_priority ? (int) lua_tointeger(L, 3) : THREAD_PRIO_DEFAULT }; + int const priority{ have_priority ? (int) lua_tointeger(L, 3) : kThreadPrioDefault }; int const globals_idx{ lua_isnoneornil(L, 4) ? 0 : 4 }; int const package_idx{ lua_isnoneornil(L, 5) ? 0 : 5 }; int const required_idx{ lua_isnoneornil(L, 6) ? 0 : 6 }; int const gc_cb_idx{ lua_isnoneornil(L, 7) ? 0 : 7 }; - static constexpr int FIXED_ARGS{ 7 }; - int const nargs{ lua_gettop(L) - FIXED_ARGS }; + static constexpr int kFixedArgsIdx{ 7 }; + int const nargs{ lua_gettop(L) - kFixedArgsIdx }; Universe* const U{ universe_get(L) }; LUA_ASSERT(L, nargs >= 0); // public Lanes API accepts a generic range -3/+3 // that will be remapped into the platform-specific scheduler priority scheme // On some platforms, -3 is equivalent to -2 and +3 to +2 - if (have_priority && (priority < THREAD_PRIO_MIN || priority > THREAD_PRIO_MAX)) + if (have_priority && (priority < kThreadPrioMin || priority > kThreadPrioMax)) { - return luaL_error(L, "Priority out of range: %d..+%d (%d)", THREAD_PRIO_MIN, THREAD_PRIO_MAX, priority); + return luaL_error(L, "Priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, priority); } /* --- Create and prepare the sub state --- */ @@ -1039,7 +1039,7 @@ LUAG_FUNC(lane_new) prepareUserData(); // leave a single cancel_error on the stack for the caller lua_settop(m_lane->L, 0); - CANCEL_ERROR.pushKey(m_lane->L); + kCancelError.pushKey(m_lane->L); { std::lock_guard lock{ m_lane->m_done_mutex }; m_lane->m_status = Lane::Cancelled; @@ -1073,7 +1073,7 @@ LUAG_FUNC(lane_new) // Store the gc_cb callback in the uservalue if (m_gc_cb_idx > 0) { - GCCB_KEY.pushKey(m_L); // ... lane uv k + kLaneGC.pushKey(m_L); // ... lane uv k lua_pushvalue(m_L, m_gc_cb_idx); // ... lane uv k gc_cb lua_rawset(m_L, -3); // ... lane uv } @@ -1243,10 +1243,10 @@ LUAG_FUNC(lane_new) } } STACK_CHECK(L, -nargs); - LUA_ASSERT(L, lua_gettop( L) == FIXED_ARGS); + LUA_ASSERT(L, lua_gettop(L) == kFixedArgsIdx); // Store 'lane' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive). - LANE_POINTER_REGKEY.setValue(L2, [lane](lua_State* L) { lua_pushlightuserdata(L, lane); }); // func [... args ...] + kLanePointerRegKey.setValue(L2, [lane](lua_State* L_) { lua_pushlightuserdata(L_, lane); }); // func [... args ...] STACK_CHECK(L2, 1 + nargs); STACK_CHECK_RESET_REL(L, 0); @@ -1278,7 +1278,7 @@ LUAG_FUNC(lane_new) // if there a gc callback? lua_getiuservalue(L, 1, 1); // ud uservalue - GCCB_KEY.pushKey(L); // ud uservalue __gc + kLaneGC.pushKey(L); // ud uservalue __gc lua_rawget(L, -2); // ud uservalue gc_cb|nil if (!lua_isnil(L, -1)) { @@ -1441,9 +1441,9 @@ LUAG_FUNC(thread_join) return ret; } +// ################################################################################################# -//--- -// thread_index( ud, key) -> value +// lane:__index(key,usr) -> value // // If key is found in the environment, return it // If key is numeric, wait until the thread returns and populate the environment with the return values @@ -1452,44 +1452,43 @@ LUAG_FUNC(thread_join) // Else raise an error LUAG_FUNC(thread_index) { - static constexpr int UD{ 1 }; - static constexpr int KEY{ 2 }; - static constexpr int USR{ 3 }; - Lane* const lane{ ToLane(L, UD) }; + static constexpr int kSelf{ 1 }; + static constexpr int kKey{ 2 }; + Lane* const lane{ ToLane(L, kSelf) }; LUA_ASSERT(L, lua_gettop(L) == 2); STACK_GROW(L, 8); // up to 8 positions are needed in case of error propagation // If key is numeric, wait until the thread returns and populate the environment with the return values - if (lua_type(L, KEY) == LUA_TNUMBER) + if (lua_type(L, kKey) == LUA_TNUMBER) { + static constexpr int kUsr{ 3 }; // first, check that we don't already have an environment that holds the requested value { // If key is found in the uservalue, return it - lua_getiuservalue(L, UD, 1); - lua_pushvalue(L, KEY); - lua_rawget(L, USR); + lua_getiuservalue(L, kSelf, 1); + lua_pushvalue(L, kKey); + lua_rawget(L, kUsr); if (!lua_isnil(L, -1)) { - return 1; + return 1; } lua_pop(L, 1); } { // check if we already fetched the values from the thread or not - lua_Integer key = lua_tointeger(L, KEY); lua_pushinteger(L, 0); - lua_rawget(L, USR); + lua_rawget(L, kUsr); bool const fetched{ !lua_isnil(L, -1) }; lua_pop(L, 1); // back to our 2 args + uservalue on the stack if (!fetched) { lua_pushinteger(L, 0); lua_pushboolean(L, 1); - lua_rawset(L, USR); + lua_rawset(L, kUsr); // wait until thread has completed lua_pushcfunction(L, LG_thread_join); - lua_pushvalue(L, UD); + lua_pushvalue(L, kSelf); lua_call(L, 1, LUA_MULTRET); // all return values are on the stack, at slots 4+ switch (lane->m_status) { @@ -1508,7 +1507,7 @@ LUAG_FUNC(thread_index) for (int i = nvalues; i > 0; --i) { // pop the last element of the stack, to store it in the uservalue at its proper index - lua_rawseti(L, USR, i); + lua_rawseti(L, kUsr, i); } } break; @@ -1521,7 +1520,7 @@ LUAG_FUNC(thread_index) // store errstring at key -1 lua_pushnumber(L, -1); lua_pushvalue(L, 5); - lua_rawset(L, USR); + lua_rawset(L, kUsr); break; case Lane::Cancelled: @@ -1529,11 +1528,12 @@ LUAG_FUNC(thread_index) break; } } - lua_settop(L, 3); // UD KEY ENV + lua_settop(L, 3); // self KEY ENV + int const key{ static_cast(lua_tointeger(L, kKey)) }; if (key != -1) { - lua_pushnumber(L, -1); // UD KEY ENV -1 - lua_rawget(L, USR); // UD KEY ENV "error" + lua_pushnumber(L, -1); // self KEY ENV -1 + lua_rawget(L, kUsr); // self KEY ENV "error"|nil if (!lua_isnil(L, -1)) // an error was stored { // Note: Lua 5.1 interpreter is not prepared to show @@ -1547,48 +1547,48 @@ LUAG_FUNC(thread_index) // Level 3 should show the line where 'h[x]' was read // but this only seems to work for string messages // (Lua 5.1.4). No idea, why. --AKa 22-Jan-2009 - lua_getmetatable(L, UD); // UD KEY ENV "error" mt - lua_getfield(L, -1, "cached_error"); // UD KEY ENV "error" mt error() - lua_getfield(L, -2, "cached_tostring"); // UD KEY ENV "error" mt error() tostring() - lua_pushvalue(L, 4); // UD KEY ENV "error" mt error() tostring() "error" - lua_call(L, 1, 1); // tostring( errstring) -- just in case // UD KEY ENV "error" mt error() "error" - lua_pushinteger(L, 3); // UD KEY ENV "error" mt error() "error" 3 - lua_call(L, 2, 0); // error( tostring( errstring), 3) // UD KEY ENV "error" mt + lua_getmetatable(L, kSelf); // self KEY ENV "error" mt + lua_getfield(L, -1, "cached_error"); // self KEY ENV "error" mt error() + lua_getfield(L, -2, "cached_tostring"); // self KEY ENV "error" mt error() tostring() + lua_pushvalue(L, 4); // self KEY ENV "error" mt error() tostring() "error" + lua_call(L, 1, 1); // tostring( errstring) -- just in case // self KEY ENV "error" mt error() "error" + lua_pushinteger(L, 3); // self KEY ENV "error" mt error() "error" 3 + lua_call(L, 2, 0); // error( tostring( errstring), 3) // self KEY ENV "error" mt } else { - lua_pop(L, 1); // back to our 3 arguments on the stack + lua_pop(L, 1); // self KEY ENV } } - lua_rawgeti(L, USR, (int)key); + lua_rawgeti(L, kUsr, key); } return 1; } - if (lua_type(L, KEY) == LUA_TSTRING) + if (lua_type(L, kKey) == LUA_TSTRING) { - char const* const keystr{ lua_tostring(L, KEY) }; + char const* const keystr{ lua_tostring(L, kKey) }; lua_settop(L, 2); // keep only our original arguments on the stack if (strcmp( keystr, "status") == 0) { lane->pushThreadStatus(L); // push the string representing the status return 1; } - // return UD.metatable[key] - lua_getmetatable(L, UD); // UD KEY mt - lua_replace(L, -3); // mt KEY - lua_rawget(L, -2); // mt value + // return self.metatable[key] + lua_getmetatable(L, kSelf); // self KEY mt + lua_replace(L, -3); // mt KEY + lua_rawget(L, -2); // mt value // only "cancel" and "join" are registered as functions, any other string will raise an error - if (lua_iscfunction(L, -1)) + if (!lua_iscfunction(L, -1)) { - return 1; + luaL_error(L, "can't index a lane with '%s'", keystr); // doesn't return } - return luaL_error(L, "can't index a lane with '%s'", keystr); + return 1; } // unknown key - lua_getmetatable(L, UD); + lua_getmetatable(L, kSelf); lua_getfield(L, -1, "cached_error"); lua_pushliteral(L, "Unknown key: "); - lua_pushvalue(L, KEY); + lua_pushvalue(L, kKey); lua_concat(L, 2); lua_call(L, 1, 0); // error( "Unknown key: " .. key) -> doesn't return return 0; @@ -1863,15 +1863,19 @@ LUAG_FUNC(configure) ); // settings M VERSION lua_setfield(L, -2, "version"); // settings M - lua_pushinteger(L, THREAD_PRIO_MAX); // settings M THREAD_PRIO_MAX + lua_pushinteger(L, kThreadPrioMax); // settings M kThreadPrioMax lua_setfield(L, -2, "max_prio"); // settings M - CANCEL_ERROR.pushKey(L); // settings M CANCEL_ERROR + kCancelError.pushKey(L); // settings M kCancelError lua_setfield(L, -2, "cancel_error"); // settings M + /* to activate in a separate commit + kNilSentinel.pushKey(L); // settings M kNilSentinel + lua_setfield(L, -2, "null"); // settings M + */ STACK_CHECK(L, 2); // reference stack contains only the function argument 'settings' // we'll need this every time we transfer some C function from/to this state - LOOKUP_REGKEY.setValue(L, [](lua_State* L) { lua_newtable(L); }); // settings M + kLookupRegKey.setValue(L, [](lua_State* L) { lua_newtable(L); }); // settings M STACK_CHECK(L, 2); // register all native functions found in that module in the transferable functions database @@ -1893,8 +1897,8 @@ LUAG_FUNC(configure) } lua_pop(L, 1); // settings - // set _R[CONFIG_REGKEY] = settings - CONFIG_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); + // set _R[kConfigRegKey] = settings + kConfigRegKey.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); STACK_CHECK(L, 1); DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); // Return the settings table @@ -1986,7 +1990,7 @@ LANES_API int luaopen_lanes_core( lua_State* L) lua_pushvalue(L, 1); // M "lanes.core" lua_pushvalue(L, -2); // M "lanes.core" M lua_pushcclosure(L, LG_configure, 2); // M LG_configure() - CONFIG_REGKEY.pushValue(L); // M LG_configure() settings + kConfigRegKey.pushValue(L); // M LG_configure() settings if (!lua_isnil(L, -1)) // this is not the first require "lanes.core": call configure() immediately { lua_pushvalue(L, -1); // M LG_configure() settings settings diff --git a/src/lanes_private.h b/src/lanes_private.h index c35d618..7ecb9a7 100644 --- a/src/lanes_private.h +++ b/src/lanes_private.h @@ -92,8 +92,8 @@ class Lane void pushThreadStatus(lua_State* L_); }; -// xxh64 of string "LANE_POINTER_REGKEY" generated at https://www.pelock.com/products/hash-calculator -static constexpr RegistryUniqueKey LANE_POINTER_REGKEY{ 0xB3022205633743BCull }; // used as registry key +// xxh64 of string "kLanePointerRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kLanePointerRegKey{ 0x2D8CF03FE9F0A51Aull }; // used as registry key // To allow free-running threads (longer lifespan than the handle's) // 'Lane' are malloc/free'd and the handle only carries a pointer. diff --git a/src/lanesconf.h b/src/lanesconf.h index fb4a601..8c66bd1 100644 --- a/src/lanesconf.h +++ b/src/lanesconf.h @@ -2,6 +2,32 @@ #include "platform.h" +// here is a place that's as good as any other to list a few coding conventions that I will endeavour to follow: +// +// indentation: +// ------------ +// spaces everywhere +// case indented inside switch braces +// accessibility keywords indented inside class braces +// matching braces have the same indentation +// +// identifiers: +// ------------ +// style is camel case. scope of variable is optionally specified with a single lowercase letter. +// constants: prefix k, followed by an uppercase letter +// program-level global variable: in 'global' namespace, prefix g, followed by an uppercase letter +// file-level static variable: in 'global' namespace, prefix s, followed by an uppercase letter +// file-level function (static or not): no prefix, start with an uppercase letter +// class/struct/enum type: no prefix, start with an uppercase letter +// static class member: prefix s, followed by an uppercase letter +// regular class member: no prefix, start with a lowercase letter +// static class method: no prefix, start with an uppercase letter +// regular class method: no prefix, start with a lowercase letter +// function argument: suffix _ +// static function variable: prefix s, followed by an uppercase letter +// function local variable: prefix l, followed by an uppercase letter +// named lambda capture: no prefix, start with a lowercase letter + #if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) #ifdef __cplusplus #define LANES_API extern "C" __declspec(dllexport) diff --git a/src/linda.cpp b/src/linda.cpp index 0a1ca3f..590b487 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -41,8 +41,8 @@ THE SOFTWARE. #include -// xxh64 of string "CANCEL_ERROR" generated at https://www.pelock.com/products/hash-calculator -static constexpr UniqueKey BATCH_SENTINEL{ 0x2DDFEE0968C62AA7ull, "linda.batched" }; +// xxh64 of string "kLindaBatched" generated at https://www.pelock.com/products/hash-calculator +static constexpr UniqueKey kLindaBatched{ 0xB8234DF772646567ull, "linda.batched" }; // ################################################################################################# @@ -64,12 +64,12 @@ static LindaFactory g_LindaFactory; // Any hashing will do that maps pointers to [0..Universe::nb_keepers[ consistently. // Pointers are often aligned by 8 or so - ignore the low order bits // have to cast to unsigned long to avoid compilation warnings about loss of data when converting pointer-to-integer -static constexpr uintptr_t KEEPER_MAGIC_SHIFT{ 3 }; +static constexpr uintptr_t kPointerMagicShift{ 3 }; Linda::Linda(Universe* U_, LindaGroup group_, char const* name_, size_t len_) : DeepPrelude{ g_LindaFactory } , U{ U_ } -, m_keeper_index{ (group_ ? group_ : static_cast(std::bit_cast(this) >> KEEPER_MAGIC_SHIFT)) % U_->keepers->nb_keepers } +, m_keeper_index{ (group_ ? group_ : static_cast(std::bit_cast(this) >> kPointerMagicShift)) % U_->keepers->nb_keepers } { setName(name_, len_); } @@ -157,8 +157,8 @@ static void check_key_types(lua_State* L, int start_, int end_) case LuaType::LIGHTUSERDATA: { - static constexpr std::array, 3> to_check{ BATCH_SENTINEL, CANCEL_ERROR, NIL_SENTINEL }; - for (UniqueKey const& key : to_check) + static constexpr std::array, 3> kKeysToCheck{ kLindaBatched, kCancelError, kNilSentinel }; + for (UniqueKey const& key : kKeysToCheck) { if (key.equals(L, i)) { @@ -217,7 +217,7 @@ int Linda::ProtectedCall(lua_State* L, lua_CFunction f_) * * Returns: 'true' if the value was queued * 'false' for timeout (only happens when the queue size is limited) -* nil, CANCEL_ERROR if cancelled +* nil, kCancelError if cancelled */ LUAG_FUNC(linda_send) { @@ -241,10 +241,10 @@ LUAG_FUNC(linda_send) ++key_i; } - bool const as_nil_sentinel{ NIL_SENTINEL.equals(L, key_i) }; // if not nullptr, send() will silently send a single nil if nothing is provided + bool const as_nil_sentinel{ kNilSentinel.equals(L, key_i) }; // if not nullptr, send() will silently send a single nil if nothing is provided if (as_nil_sentinel) { - // the real key to send data to is after the NIL_SENTINEL marker + // the real key to send data to is after the kNilSentinel marker ++key_i; } @@ -259,7 +259,7 @@ LUAG_FUNC(linda_send) if (as_nil_sentinel) { // send a single nil if nothing is provided - NIL_SENTINEL.pushKey(L); + kNilSentinel.pushKey(L); } else { @@ -273,7 +273,7 @@ LUAG_FUNC(linda_send) CancelRequest cancel{ CancelRequest::None }; KeeperCallResult pushed; { - Lane* const lane{ LANE_POINTER_REGKEY.readLightUserDataValue(L) }; + Lane* const lane{ kLanePointerRegKey.readLightUserDataValue(L) }; Keeper* const K{ linda->whichKeeper() }; KeeperState const KL{ K ? K->L : nullptr }; if (KL == nullptr) @@ -354,7 +354,7 @@ LUAG_FUNC(linda_send) { case CancelRequest::Soft: // if user wants to soft-cancel, the call returns lanes.cancel_error - CANCEL_ERROR.pushKey(L); + kCancelError.pushKey(L); return 1; case CancelRequest::Hard: @@ -407,7 +407,7 @@ LUAG_FUNC(linda_receive) keeper_api_t selected_keeper_receive{ nullptr }; int expected_pushed_min{ 0 }, expected_pushed_max{ 0 }; // are we in batched mode? - BATCH_SENTINEL.pushKey(L); + kLindaBatched.pushKey(L); int const is_batched{ lua501_equal(L, key_i, -1) }; lua_pop(L, 1); if (is_batched) @@ -439,7 +439,7 @@ LUAG_FUNC(linda_receive) expected_pushed_min = expected_pushed_max = 2; } - Lane* const lane{ LANE_POINTER_REGKEY.readLightUserDataValue(L) }; + Lane* const lane{ kLanePointerRegKey.readLightUserDataValue(L) }; Keeper* const K{ linda->whichKeeper() }; KeeperState const KL{ K ? K->L : nullptr }; if (KL == nullptr) @@ -518,8 +518,8 @@ LUAG_FUNC(linda_receive) switch (cancel) { case CancelRequest::Soft: - // if user wants to soft-cancel, the call returns CANCEL_ERROR - CANCEL_ERROR.pushKey(L); + // if user wants to soft-cancel, the call returns kCancelError + kCancelError.pushKey(L); return 1; case CancelRequest::Hard: @@ -582,7 +582,7 @@ LUAG_FUNC(linda_set) else // linda is cancelled { // do nothing and return lanes.cancel_error - CANCEL_ERROR.pushKey(L); + kCancelError.pushKey(L); pushed.emplace(1); } @@ -645,7 +645,7 @@ LUAG_FUNC(linda_get) else // linda is cancelled { // do nothing and return lanes.cancel_error - CANCEL_ERROR.pushKey(L); + kCancelError.pushKey(L); pushed.emplace(1); } // an error can be raised if we attempt to read an unregistered function @@ -689,7 +689,7 @@ LUAG_FUNC(linda_limit) else // linda is cancelled { // do nothing and return lanes.cancel_error - CANCEL_ERROR.pushKey(L); + kCancelError.pushKey(L); pushed.emplace(1); } // propagate pushed boolean if any @@ -956,10 +956,10 @@ void LindaFactory::createMetatable(lua_State* L) const luaL_setfuncs(L, s_LindaMT, 0); // some constants - BATCH_SENTINEL.pushKey(L); + kLindaBatched.pushKey(L); lua_setfield(L, -2, "batched"); - NIL_SENTINEL.pushKey(L); + kNilSentinel.pushKey(L); lua_setfield(L, -2, "null"); STACK_CHECK(L, 1); diff --git a/src/linda.h b/src/linda.h index 559ef24..f3b1844 100644 --- a/src/linda.h +++ b/src/linda.h @@ -50,6 +50,7 @@ class Linda : public DeepPrelude // Deep userdata MUST start with this header ~Linda(); Linda(Universe* U_, LindaGroup group_, char const* name_, size_t len_); Linda() = delete; + // non-copyable, non-movable Linda(Linda const&) = delete; Linda(Linda const&&) = delete; Linda& operator=(Linda const&) = delete; diff --git a/src/state.cpp b/src/state.cpp index 2a69a46..379b63e 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -200,15 +200,15 @@ static void copy_one_time_settings(Universe* U, SourceState L1, DestState L2) DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END)); - CONFIG_REGKEY.pushValue(L1); // config + kConfigRegKey.pushValue(L1); // config // copy settings from from source to destination registry InterCopyContext c{ U, L2, L1, {}, {}, {}, {}, {} }; if (c.inter_move(1) != InterCopyResult::Success) // // config { luaL_error(L1, "failed to copy settings when loading lanes.core"); // doesn't return } - // set L2:_R[CONFIG_REGKEY] = settings - CONFIG_REGKEY.setValue(L2, [](lua_State* L) { lua_insert(L, -2); }); // config + // set L2:_R[kConfigRegKey] = settings + kConfigRegKey.setValue(L2, [](lua_State* L) { lua_insert(L, -2); }); // config STACK_CHECK(L2, 0); STACK_CHECK(L1, 0); } @@ -301,7 +301,7 @@ void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMod STACK_CHECK(L, 0); return; } - CONFIG_REGKEY.pushValue(L); // {} + kConfigRegKey.pushValue(L); // {} STACK_CHECK(L, 1); lua_getfield(L, -1, "on_state_create"); // {} on_state_create() lua_remove(L, -2); // on_state_create() @@ -344,7 +344,7 @@ lua_State* luaG_newstate(Universe* U, SourceState from_, char const* libs_) STACK_CHECK(L, 0); // we'll need this every time we transfer some C function from/to this state - LOOKUP_REGKEY.setValue(L, [](lua_State* L) { lua_newtable(L); }); + kLookupRegKey.setValue(L, [](lua_State* L) { lua_newtable(L); }); STACK_CHECK(L, 0); // neither libs (not even 'base') nor special init func: we are done @@ -427,7 +427,7 @@ lua_State* luaG_newstate(Universe* U, SourceState from_, char const* libs_) #if 1 && USE_DEBUG_SPEW() // dump the lookup database contents - LOOKUP_REGKEY.pushValue(L); // {} + kLookupRegKey.pushValue(L); // {} lua_pushnil(L); // {} nil while (lua_next(L, -2)) // {} k v { diff --git a/src/threading.cpp b/src/threading.cpp index c7928c1..acc79d8 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -350,7 +350,7 @@ static int const gs_prio_remap[] = [[nodiscard]] static int select_prio(int prio /* -3..+3 */) { - if (prio == THREAD_PRIO_DEFAULT) + if (prio == kThreadPrioDefault) prio = 0; // prio range [-3,+3] was checked by the caller return gs_prio_remap[prio + 3]; diff --git a/src/threading.h b/src/threading.h index e13932b..7d24ef0 100644 --- a/src/threading.h +++ b/src/threading.h @@ -15,7 +15,7 @@ #define THREADAPI THREADAPI_PTHREAD #endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) -static constexpr int THREAD_PRIO_DEFAULT{ -999 }; +static constexpr int kThreadPrioDefault{ -999 }; // ################################################################################################# // ################################################################################################# @@ -40,8 +40,8 @@ static constexpr int THREAD_PRIO_DEFAULT{ -999 }; #pragma message( "The value of _WIN32_WINNT: " XSTR(_WIN32_WINNT)) */ -static constexpr int THREAD_PRIO_MIN{ -3 }; -static constexpr int THREAD_PRIO_MAX{ +3 }; +static constexpr int kThreadPrioMin{ -3 }; +static constexpr int kThreadPrioMax{ +3 }; // ################################################################################################# // ################################################################################################# @@ -58,11 +58,11 @@ static constexpr int THREAD_PRIO_MAX{ +3 }; #include #if defined(PLATFORM_LINUX) && !defined(LINUX_SCHED_RR) -static constexpr int THREAD_PRIO_MIN{ 0 }; +static constexpr int kThreadPrioMin{ 0 }; #else -static constexpr int THREAD_PRIO_MIN{ -3 }; +static constexpr int kThreadPrioMin{ -3 }; #endif -static constexpr int THREAD_PRIO_MAX{ +3 }; +static constexpr int kThreadPrioMax{ +3 }; #endif // THREADAPI == THREADAPI_PTHREAD // ################################################################################################# diff --git a/src/tools.cpp b/src/tools.cpp index a25fb2b..aa8f927 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -37,8 +37,8 @@ THE SOFTWARE. DEBUGSPEW_CODE(char const* const DebugSpewIndentScope::debugspew_indent = "----+----!----+----!----+----!----+----!----+----!----+----!----+----!----+"); -// crc64/we of string "LOOKUPCACHE_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey LOOKUPCACHE_REGKEY{ 0x837A68DFC6FCB716ull }; +// xxh64 of string "kLookupCacheRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kLookupCacheRegKey{ 0x9BF75F84E54B691Bull }; // ################################################################################################# @@ -480,7 +480,7 @@ void populate_func_lookup_table(lua_State* L, int i_, char const* name_) DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); STACK_GROW(L, 3); STACK_CHECK_START_REL(L, 0); - LOOKUP_REGKEY.pushValue(L); // {} + kLookupRegKey.pushValue(L); // {} STACK_CHECK(L, 1); LUA_ASSERT(L, lua_istable(L, -1)); if (lua_type(L, in_base) == LUA_TFUNCTION) // for example when a module is a simple function @@ -510,12 +510,12 @@ void populate_func_lookup_table(lua_State* L, int i_, char const* name_) STACK_CHECK(L, 2); } // retrieve the cache, create it if we haven't done it yet - LOOKUPCACHE_REGKEY.pushValue(L); // {} {fqn} {cache}? + kLookupCacheRegKey.pushValue(L); // {} {fqn} {cache}? if (lua_isnil(L, -1)) { lua_pop(L, 1); // {} {fqn} lua_newtable(L); // {} {fqn} {cache} - LOOKUPCACHE_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); + kLookupCacheRegKey.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); STACK_CHECK(L, 3); } // process everything we find in that table, filling in lookup data for all functions and tables we see there @@ -534,8 +534,8 @@ void populate_func_lookup_table(lua_State* L, int i_, char const* name_) /*---=== Inter-state copying ===---*/ -// crc64/we of string "REG_MTID" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; +// xxh64 of string "kMtIdRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kMtIdRegKey{ 0xA8895DCF4EC3FE3Cull }; /* * Get a unique ID for metatable at [i]. @@ -547,12 +547,12 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; STACK_GROW(L, 3); STACK_CHECK_START_REL(L, 0); - push_registry_subtable(L, REG_MTID); // ... _R[REG_MTID] - lua_pushvalue(L, i); // ... _R[REG_MTID] {mt} - lua_rawget(L, -2); // ... _R[REG_MTID] mtk? + push_registry_subtable(L, kMtIdRegKey); // ... _R[kMtIdRegKey] + lua_pushvalue(L, i); // ... _R[kMtIdRegKey] {mt} + lua_rawget(L, -2); // ... _R[kMtIdRegKey] mtk? lua_Integer id{ lua_tointeger(L, -1) }; // 0 for nil - lua_pop(L, 1); // ... _R[REG_MTID] + lua_pop(L, 1); // ... _R[kMtIdRegKey] STACK_CHECK(L, 1); if (id == 0) @@ -560,13 +560,13 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; id = U->next_mt_id.fetch_add(1, std::memory_order_relaxed); // Create two-way references: id_uint <-> table - lua_pushvalue(L, i); // ... _R[REG_MTID] {mt} - lua_pushinteger(L, id); // ... _R[REG_MTID] {mt} id - lua_rawset(L, -3); // ... _R[REG_MTID] + lua_pushvalue(L, i); // ... _R[kMtIdRegKey] {mt} + lua_pushinteger(L, id); // ... _R[kMtIdRegKey] {mt} id + lua_rawset(L, -3); // ... _R[kMtIdRegKey] - lua_pushinteger(L, id); // ... _R[REG_MTID] id - lua_pushvalue(L, i); // ... _R[REG_MTID] id {mt} - lua_rawset(L, -3); // ... _R[REG_MTID] + lua_pushinteger(L, id); // ... _R[kMtIdRegKey] id + lua_pushvalue(L, i); // ... _R[kMtIdRegKey] id {mt} + lua_rawset(L, -3); // ... _R[kMtIdRegKey] } lua_pop(L, 1); // ... STACK_CHECK(L, 0); @@ -628,7 +628,7 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; else { // fetch the name from the source state's lookup table - LOOKUP_REGKEY.pushValue(L); // ... v ... {} + kLookupRegKey.pushValue(L); // ... v ... {} STACK_CHECK( L, 1); LUA_ASSERT(L, lua_istable( L, -1)); lua_pushvalue( L, i); // ... v ... {} v @@ -700,7 +700,7 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; case LookupMode::LaneBody: case LookupMode::FromKeeper: - LOOKUP_REGKEY.pushValue(L2); // {} + kLookupRegKey.pushValue(L2); // {} STACK_CHECK(L2, 1); LUA_ASSERT(L1, lua_istable(L2, -1)); lua_pushlstring(L2, fqn, len); // {} "f.q.n" @@ -1005,7 +1005,7 @@ void InterCopyContext::lookup_native_func() const case LookupMode::LaneBody: case LookupMode::FromKeeper: - LOOKUP_REGKEY.pushValue(L2); // {} + kLookupRegKey.pushValue(L2); // {} STACK_CHECK(L2, 1); LUA_ASSERT(L1, lua_istable(L2, -1)); lua_pushlstring(L2, fqn, len); // {} "f.q.n" @@ -1324,30 +1324,30 @@ void InterCopyContext::copy_cached_func() const STACK_CHECK_START_REL(L2, 0); STACK_GROW(L2, 4); // do we already know this metatable? - push_registry_subtable(L2, REG_MTID); // _R[REG_MTID] - lua_pushinteger(L2, mt_id); // _R[REG_MTID] id - lua_rawget(L2, -2); // _R[REG_MTID] mt|nil + push_registry_subtable(L2, kMtIdRegKey); // _R[kMtIdRegKey] + lua_pushinteger(L2, mt_id); // _R[kMtIdRegKey] id + lua_rawget(L2, -2); // _R[kMtIdRegKey] mt|nil STACK_CHECK(L2, 2); if (lua_isnil(L2, -1)) { // L2 did not know the metatable - lua_pop(L2, 1); // _R[REG_MTID] + lua_pop(L2, 1); // _R[kMtIdRegKey] InterCopyContext const c{ U, L2, L1, L2_cache_i, SourceIndex{ lua_gettop(L1) }, VT::METATABLE, mode, name }; - if (!c.inter_copy_one()) // _R[REG_MTID] mt? + if (!c.inter_copy_one()) // _R[kMtIdRegKey] mt? { luaL_error(L1, "Error copying a metatable"); // doesn't return } - STACK_CHECK(L2, 2); // _R[REG_MTID] mt + STACK_CHECK(L2, 2); // _R[kMtIdRegKey] mt // mt_id -> metatable - lua_pushinteger(L2, mt_id); // _R[REG_MTID] mt id - lua_pushvalue(L2, -2); // _R[REG_MTID] mt id mt - lua_rawset(L2, -4); // _R[REG_MTID] mt + lua_pushinteger(L2, mt_id); // _R[kMtIdRegKey] mt id + lua_pushvalue(L2, -2); // _R[kMtIdRegKey] mt id mt + lua_rawset(L2, -4); // _R[kMtIdRegKey] mt // metatable -> mt_id - lua_pushvalue(L2, -1); // _R[REG_MTID] mt mt - lua_pushinteger(L2, mt_id); // _R[REG_MTID] mt mt id - lua_rawset(L2, -4); // _R[REG_MTID] mt + lua_pushvalue(L2, -1); // _R[kMtIdRegKey] mt mt + lua_pushinteger(L2, mt_id); // _R[kMtIdRegKey] mt mt id + lua_rawset(L2, -4); // _R[kMtIdRegKey] mt STACK_CHECK(L2, 2); } lua_remove(L2, -2); // mt @@ -1432,12 +1432,6 @@ void InterCopyContext::inter_copy_keyvaluepair() const // ################################################################################################# -/* -* The clone cache is a weak valued table listing all clones, indexed by their userdatapointer -* fnv164 of string "CLONABLES_CACHE_KEY" generated at https://www.pelock.com/products/hash-calculator -*/ -static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull }; - [[nodiscard]] bool InterCopyContext::copyclone() const { SourceIndex const L1_i{ lua_absindex(L1, this->L1_i) }; @@ -1848,7 +1842,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull }; */ [[nodiscard]] bool InterCopyContext::inter_copy_one() const { - static constexpr int pod_mask = (1 << LUA_TNIL) | (1 << LUA_TBOOLEAN) | (1 << LUA_TLIGHTUSERDATA) | (1 << LUA_TNUMBER) | (1 << LUA_TSTRING); + static constexpr int kPODmask = (1 << LUA_TNIL) | (1 << LUA_TBOOLEAN) | (1 << LUA_TLIGHTUSERDATA) | (1 << LUA_TNUMBER) | (1 << LUA_TSTRING); STACK_GROW(L2, 1); STACK_CHECK_START_REL(L1, 0); // L1 // L2 STACK_CHECK_START_REL(L2, 0); // L1 // L2 @@ -1860,7 +1854,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull }; DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END, lua_type_names[static_cast(val_type)], vt_names[static_cast(vt)])); // Non-POD can be skipped if its metatable contains { __lanesignore = true } - if (((1 << static_cast(val_type)) & pod_mask) == 0) + if (((1 << static_cast(val_type)) & kPODmask) == 0) { if (lua_getmetatable(L1, L1_i)) // ... mt { diff --git a/src/tools.h b/src/tools.h index 755a0c6..53edbe2 100644 --- a/src/tools.h +++ b/src/tools.h @@ -87,8 +87,8 @@ void initialize_allocator_function(Universe* U, lua_State* L); // ################################################################################################# -// crc64/we of string "CONFIG_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey CONFIG_REGKEY{ 0x31CD24894EAE8624ull }; // registry key to access the configuration +// xxh64 of string "kConfigRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kConfigRegKey{ 0x608379D20A398046ull }; // registry key to access the configuration -// crc64/we of string "LOOKUP_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey LOOKUP_REGKEY{ 0x5051ED67EE7B51A1ull }; // registry key to access the lookup database +// xxh64 of string "kLookupRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kLookupRegKey{ 0xBF1FC5CF3C6DD47Bull }; // registry key to access the lookup database diff --git a/src/universe.cpp b/src/universe.cpp index fb10067..4eca0c9 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -36,10 +36,10 @@ THE SOFTWARE. #include "macros_and_utils.h" #include "uniquekey.h" -// xxh64 of string "UNIVERSE_FULL_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey UNIVERSE_FULL_REGKEY{ 0x99CA130C09EDC074ull }; -// xxh64 of string "UNIVERSE_LIGHT_REGKEY" generated at http://www.nitrxgen.net/hashgen/ -static constexpr RegistryUniqueKey UNIVERSE_LIGHT_REGKEY{ 0x3663C07C742CEB81ull }; +// xxh64 of string "kUniverseFullRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kUniverseFullRegKey{ 0x1C2D76870DD9DD9Full }; +// xxh64 of string "kUniverseLightRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kUniverseLightRegKey{ 0x48BBE9CEAB0BA04Full }; // ################################################################################################# @@ -79,8 +79,8 @@ Universe* universe_create(lua_State* L) Universe* const U{ lua_newuserdatauv(L, 0) }; // universe U->Universe::Universe(); STACK_CHECK_START_REL(L, 1); - UNIVERSE_FULL_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); - UNIVERSE_LIGHT_REGKEY.setValue(L, [U](lua_State* L) { lua_pushlightuserdata(L, U); }); + kUniverseFullRegKey.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); + kUniverseLightRegKey.setValue(L, [U](lua_State* L) { lua_pushlightuserdata(L, U); }); STACK_CHECK(L, 1); return U; } @@ -91,7 +91,7 @@ void universe_store(lua_State* L, Universe* U) { LUA_ASSERT(L, !U || universe_get(L) == nullptr); STACK_CHECK_START_REL(L, 0); - UNIVERSE_LIGHT_REGKEY.setValue(L, [U](lua_State* L) { U ? lua_pushlightuserdata(L, U) : lua_pushnil(L); }); + kUniverseLightRegKey.setValue(L, [U](lua_State* L) { U ? lua_pushlightuserdata(L, U) : lua_pushnil(L); }); STACK_CHECK(L, 0); } @@ -100,7 +100,7 @@ void universe_store(lua_State* L, Universe* U) Universe* universe_get(lua_State* L) { STACK_CHECK_START_REL(L, 0); - Universe* const universe{ UNIVERSE_LIGHT_REGKEY.readLightUserDataValue(L) }; + Universe* const universe{ kUniverseLightRegKey.readLightUserDataValue(L) }; STACK_CHECK(L, 0); return universe; } diff --git a/src/universe.h b/src/universe.h index d0d7864..bdc871d 100644 --- a/src/universe.h +++ b/src/universe.h @@ -178,6 +178,7 @@ class Universe Universe(); ~Universe() = default; + // non-copyable, non-movable Universe(Universe const&) = delete; Universe(Universe&&) = delete; Universe& operator=(Universe const&) = delete; -- cgit v1.2.3-55-g6feb