From 792128255b6c6add22f97ea60734181cb915f2ae Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Fri, 14 Jun 2024 12:25:23 +0200 Subject: More fixes to make clang happy --- src/_pch.h | 1 + src/compat.h | 229 +++++++++++++++++------------------------------ src/intercopycontext.cpp | 3 +- src/keeper.h | 2 +- src/lane.cpp | 14 ++- src/lane.h | 2 +- src/threading.cpp | 36 +++++--- src/threading.h | 2 +- src/tools.cpp | 4 +- 9 files changed, 122 insertions(+), 171 deletions(-) (limited to 'src') diff --git a/src/_pch.h b/src/_pch.h index 6dc0cb5..29a8108 100644 --- a/src/_pch.h +++ b/src/_pch.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include diff --git a/src/compat.h b/src/compat.h index 3b0ebf4..bcea225 100644 --- a/src/compat.h +++ b/src/compat.h @@ -128,188 +128,119 @@ inline LuaType luaG_type(lua_State* const L_, int const idx_) return static_cast(lua_type(L_, idx_)); } -// ------------------------------------------------------------------------------------------------- +// ################################################################################################# +// ################################################################################################# +// All the compatibility wrappers we expose start with luaG_ +// ################################################################################################# +// ################################################################################################# -// Default matches Lua 5.4 as of now -template -struct Wrap +// use this in place of lua_absindex to save a function call +inline int luaG_absindex(lua_State* L_, int idx_) { - static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, int const strip_) - { - return ::lua_dump(L_, writer_, data_, strip_); - } + return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); +} - static inline LuaType lua_getfield(lua_State* L_, int idx_, std::string_view const& k_) - { - // starting with Lua 5.3, lua_getfield returns the type of the pushed value - return static_cast(::lua_getfield(L_, idx_, k_.data())); - } +// ################################################################################################# - template - static inline void (luaL_newlib)(lua_State* const L_, luaL_Reg const (&funcs_)[N]) - { - lua_createtable(L_, 0, N - 1); - ::luaL_setfuncs(L_, funcs_, 0); - } +template +concept RequiresOldLuaDump = requires(LUA_DUMP f_) { { f_(nullptr, nullptr, nullptr) } -> std::same_as; }; - static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int nup_) - { - ::luaL_setfuncs(L_, funcs_, nup_); - } +template +static inline int WrapLuaDump(LUA_DUMP f_, lua_State* const L_, lua_Writer const writer_, void* const data_, [[maybe_unused]] int const strip_) +{ + return f_(L_, writer_, data_); +} - static void luaL_setmetatable(lua_State* const L_, std::string_view const& tname_) - { - ::luaL_setmetatable(L_, tname_.data()); - } -}; +// ------------------------------------------------------------------------------------------------- -// ################################################################################################# +template +concept RequiresNewLuaDump = requires(LUA_DUMP f_) { { f_(nullptr, nullptr, nullptr, 0) } -> std::same_as; }; -template -struct Wrap> +template +static inline int WrapLuaDump(LUA_DUMP f_, lua_State* const L_, lua_Writer const writer_, void* const data_, int const strip_) { - static inline int lua_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) - { - return ::lua_dump(L_, writer_, data_, strip_); - } + return f_(L_, writer_, data_, strip_); +} - static inline LuaType lua_getfield(lua_State* L_, int idx_, std::string_view const& k_) - { - // starting with Lua 5.3, lua_getfield returns the type of the pushed value - return static_cast(::lua_getfield(L_, idx_, k_.data())); - } +// ------------------------------------------------------------------------------------------------- - template - static inline void (luaL_newlib)(lua_State* const L_, luaL_Reg const (&funcs_)[N]) - { - lua_createtable(L_, 0, N - 1); - ::luaL_setfuncs(L_, funcs_, 0); - } +static inline int luaG_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, int const strip_) +{ + return WrapLuaDump(lua_dump, L_, writer_, data_, strip_); +} - static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int const nup_) - { - ::luaL_setfuncs(L_, funcs_, nup_); - } +// ################################################################################################# - static void luaL_setmetatable(lua_State* const L_, std::string_view const& tname_) - { - ::luaL_setmetatable(L_, tname_.data()); - } -}; +int luaG_getalluservalues(lua_State* L_, int idx_); // ################################################################################################# -template -struct Wrap> +template +concept RequiresOldLuaGetfield = requires(LUA_GETFIELD f_) { - static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, [[maybe_unused]] int const strip_) { - return ::lua_dump(L_, writer_, data_); - } - - static inline LuaType lua_getfield(lua_State* L_, int idx_, std::string_view const& k_) - { - // before Lua 5.3, lua_getfield returns nothing - ::lua_getfield(L_, idx_, k_.data()); - return luaG_type(L_, -1); - } - - template - static inline void (luaL_newlib)(lua_State* const L_, luaL_Reg const (&funcs_)[N]) - { - ::lua_createtable(L_, 0, N - 1); - ::luaL_setfuncs(L_, funcs_, 0); - } - - static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int const nup_) - { - ::luaL_setfuncs(L_, funcs_, nup_); - } - - static void luaL_setmetatable(lua_State* const L_, std::string_view const& tname_) - { - ::luaL_setmetatable(L_, tname_.data()); - } + f_(nullptr, 0, nullptr) + } -> std::same_as; }; -// ################################################################################################# - -template -struct Wrap> +template +static inline int WrapLuaGetField(LUA_GETFIELD f_, lua_State* const L_, int const idx_, char const* const name_) { - static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, [[maybe_unused]] int const strip_) - { - return ::lua_dump(L_, writer_, data_); - } - - static inline LuaType lua_getfield(lua_State* L_, int idx_, std::string_view const& k_) - { - // before Lua 5.3, lua_getfield returns nothing - ::lua_getfield(L_, idx_, k_.data()); - return luaG_type(L_, -1); - } - - template - static inline void (luaL_newlib)(lua_State* const L_, luaL_Reg const (&funcs_)[N]) - { - lua_createtable(L_, 0, N - 1); - ::luaL_register(L_, nullptr, funcs_); - } + f_(L_, idx_, name_); + return lua_type(L_, -1); +} - static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], [[maybe_unused]] int const nup_) - { - ::luaL_register(L_, nullptr, funcs_); - } +// ------------------------------------------------------------------------------------------------- - static void luaL_setmetatable(lua_State* const L_, std::string_view const& tname_) +template +concept RequiresNewLuaGetfield = requires(LUA_GETFIELD f_) +{ { - luaL_getmetatable(L_, tname_.data()); - lua_setmetatable(L_, -2); - } + f_(nullptr, 0, nullptr) + } -> std::same_as; }; -// ################################################################################################# -// All the compatibility wrappers we expose start with luaG_ - -// ------------------------------------------------------------------------------------------------- - -// use this in place of lua_absindex to save a function call -inline int luaG_absindex(lua_State* L_, int idx_) +template +static inline int WrapLuaGetField(LUA_GETFIELD f_, lua_State* const L_, int const idx_, char const* const name_) { - return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); + return f_(L_, idx_, name_); } // ------------------------------------------------------------------------------------------------- -inline int luaG_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) +static inline LuaType luaG_getfield(lua_State* const L_, int const idx_, std::string_view const& name_) { - return Wrap::lua_dump(L_, writer_, data_, strip_); + return static_cast(WrapLuaGetField(lua_getfield, L_, idx_, name_.data())); } -// ------------------------------------------------------------------------------------------------- +// ################################################################################################# -int luaG_getalluservalues(lua_State* L_, int idx_); +LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); -// ------------------------------------------------------------------------------------------------- +// ################################################################################################# -[[nodiscard]] inline LuaType luaG_getfield(lua_State* L_, int idx_, std::string_view const& k_) +inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const* funcs_) { - return Wrap::lua_getfield(L_, idx_, k_); + // fake externs to make clang happy... + extern void luaL_register(lua_State*, char const*, luaL_Reg const*); // Lua 5.1 + extern void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int nup_); // Lua 5.2+ + if constexpr (LUA_VERSION_NUM == 501) { + luaL_register(L_, nullptr, funcs_); + } else { + luaL_setfuncs(L_, funcs_, 0); + } } -// ------------------------------------------------------------------------------------------------- - -LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); - -// ------------------------------------------------------------------------------------------------- +// ################################################################################################# -template -inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N]) +template +static inline void luaG_newlib(lua_State* const L_, luaL_Reg const (&funcs_)[N]) { - (Wrap::luaL_newlib)(L_, funcs_); + lua_createtable(L_, 0, N - 1); + luaG_registerlibfuncs(L_, funcs_); } -// ------------------------------------------------------------------------------------------------- +// ################################################################################################# template [[nodiscard]] T* luaG_newuserdatauv(lua_State* L_, int nuvalue_) @@ -317,7 +248,7 @@ template return static_cast(lua_newuserdatauv(L_, sizeof(T), nuvalue_)); } -// ------------------------------------------------------------------------------------------------- +// ################################################################################################# inline void luaG_pushglobaltable(lua_State* const L_) { @@ -328,21 +259,21 @@ inline void luaG_pushglobaltable(lua_State* const L_) #endif // LUA_GLOBALSINDEX } -// ------------------------------------------------------------------------------------------------- - -inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) -{ - Wrap::luaL_setfuncs(L_, funcs_, 0); -} - -// ------------------------------------------------------------------------------------------------- +// ################################################################################################# inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname_) { - return Wrap::luaL_setmetatable(L_, tname_); + // fake externs to make clang happy... + extern void luaL_setmetatable(lua_State* const L_, char const* const tname_); // Lua 5.2+ + if constexpr (LUA_VERSION_NUM == 501) { + luaL_setmetatable(L_, tname_.data()); + } else { + luaL_getmetatable(L_, tname_.data()); + lua_setmetatable(L_, -2); + } } -// ------------------------------------------------------------------------------------------------- +// ################################################################################################# // a small helper to extract a full userdata pointer from the stack in a safe way template diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 190e15e..1ccef80 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -183,7 +183,7 @@ void InterCopyContext::copyFunction() const STACK_GROW(L1, 2); STACK_CHECK_START_REL(L1, 0); - // 'lua_dump()' needs the function at top of stack + // 'luaG_dump()' needs the function at top of stack // if already on top of the stack, no need to push again bool const _needToPush{ L1_i != lua_gettop(L1) }; if (_needToPush) { @@ -1136,7 +1136,6 @@ namespace { */ [[nodiscard]] InterCopyResult InterCopyContext::interCopyOne() const { - 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); STACK_CHECK_START_REL(L2, 0); diff --git a/src/keeper.h b/src/keeper.h index 1ad84d7..1a194e8 100644 --- a/src/keeper.h +++ b/src/keeper.h @@ -44,7 +44,7 @@ struct Keepers // can't use std::vector because Keeper contains a mutex, so we need a raw memory buffer struct KV { - std::unique_ptr keepers{}; + std::unique_ptr keepers; size_t nbKeepers{}; }; std::variant keeper_array; diff --git a/src/lane.cpp b/src/lane.cpp index c2cbbac..8f31973 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -677,7 +677,7 @@ static void lane_main(Lane* const lane_) #ifndef __PROSPERO__ lane_->ready.wait(); #else // __PROSPERO__ - while (!lane_->ready._My_flag) { + while (!lane_->ready.test()) { std::this_thread::yield(); } #endif // __PROSPERO__ @@ -719,7 +719,8 @@ static void lane_main(Lane* const lane_) lane_->U->selfdestructingCount.fetch_sub(1, std::memory_order_release); lane_->U->selfdestructMutex.unlock(); - // we destroy our jthread member from inside the thread body, so we have to detach so that we don't try to join, as this doesn't seem a good idea + // we destroy ourselves, therefore our thread member too, from inside the thread body + // detach so that we don't try to join, as this doesn't seem a good idea lane_->thread.detach(); delete lane_; return; @@ -828,6 +829,11 @@ Lane::Lane(Universe* U_, lua_State* L_, ErrorTraceLevel errorTraceLevel_) Lane::~Lane() { + // not necessary when using a jthread + if (thread.joinable()) { + thread.join(); + } + // no longer tracked std::ignore = U->tracker.tracking_remove(this); } @@ -1032,9 +1038,9 @@ void Lane::securizeDebugName(lua_State* L_) void Lane::startThread(int priority_) { - thread = std::jthread([this]() { lane_main(this); }); + thread = std::thread([this]() { lane_main(this); }); if (priority_ != kThreadPrioDefault) { - JTHREAD_SET_PRIORITY(thread, priority_, U->sudo); + THREAD_SET_PRIORITY(thread, priority_, U->sudo); } } diff --git a/src/lane.h b/src/lane.h index 836b1b3..3c3cced 100644 --- a/src/lane.h +++ b/src/lane.h @@ -68,7 +68,7 @@ class Lane using enum ErrorTraceLevel; // the thread - std::jthread thread; + std::thread thread; // use jthread if we ever need a stop_source #ifndef __PROSPERO__ // a latch to wait for the lua_State to be ready std::latch ready{ 1 }; diff --git a/src/threading.cpp b/src/threading.cpp index ebac0da..af2142b 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -131,11 +131,11 @@ void THREAD_SET_PRIORITY(int prio_, [[maybe_unused]] bool sudo_) // ################################################################################################# -void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, [[maybe_unused]] bool sudo_) +void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, [[maybe_unused]] bool sudo_) { // prio range [-3,+3] was checked by the caller if (!SetThreadPriority(thread_.native_handle(), gs_prio_remap[prio_ + 3])) { - FAIL("JTHREAD_SET_PRIORITY", GetLastError()); + FAIL("THREAD_SET_PRIORITY", GetLastError()); } } @@ -349,14 +349,6 @@ static int const gs_prio_remap[] = { #endif // _PRIO_0 }; -[[nodiscard]] static int select_prio(int prio /* -3..+3 */) -{ - if (prio == kThreadPrioDefault) - prio = 0; - // prio range [-3,+3] was checked by the caller - return gs_prio_remap[prio + 3]; -} - void THREAD_SET_PRIORITY(int prio_, [[maybe_unused]] bool sudo_) { #ifdef PLATFORM_LINUX @@ -372,7 +364,7 @@ void THREAD_SET_PRIORITY(int prio_, [[maybe_unused]] bool sudo_) // ################################################################################################# -void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, [[maybe_unused]] bool sudo_) +void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, [[maybe_unused]] bool sudo_) { #ifdef PLATFORM_LINUX if (!sudo_) // only root-privileged process can change priorities @@ -387,6 +379,15 @@ void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, [[maybe_unused]] boo // ################################################################################################# +#ifdef __PROSPERO__ + +void THREAD_SET_AFFINITY(unsigned int aff_) +{ + scePthreadSetaffinity(scePthreadSelf(), aff_); +} + +#else // __PROSPERO__ + void THREAD_SET_AFFINITY(unsigned int aff_) { int bit = 0; @@ -416,8 +417,19 @@ void THREAD_SET_AFFINITY(unsigned int aff_) #endif } +#endif // __PROSPERO__ + // ################################################################################################# +#ifdef __PROSPERO__ + +void THREAD_SETNAME(std::string_view const& name_) +{ + scePthreadRename(scePthreadSelf(), name_.data()); +} + +#else // __PROSPERO__ + void THREAD_SETNAME(std::string_view const& name_) { // exact API to set the thread name is platform-dependant @@ -442,6 +454,8 @@ void THREAD_SETNAME(std::string_view const& name_) #endif } +#endif // __PROSPERO__ + #endif // THREADAPI == THREADAPI_PTHREAD // ################################################################################################# // ################################################################################################# diff --git a/src/threading.h b/src/threading.h index f0d1592..044b5a4 100644 --- a/src/threading.h +++ b/src/threading.h @@ -70,4 +70,4 @@ void THREAD_SETNAME(std::string_view const& name_); void THREAD_SET_PRIORITY(int prio_, bool sudo_); void THREAD_SET_AFFINITY(unsigned int aff_); -void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, bool sudo_); +void THREAD_SET_PRIORITY(std::thread& thread_, int prio_, bool sudo_); diff --git a/src/tools.cpp b/src/tools.cpp index 49872f7..3a331f5 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -61,7 +61,7 @@ static constexpr int kWriterReturnCode{ 666 }; * +-----------------+-------------------+------------+----------+ * | lua_tocfunction | nullptr | | nullptr | * +-----------------+-------------------+------------+----------+ - * | lua_dump | kWriterReturnCode | 1 | 1 | + * | luaG_dump | kWriterReturnCode | 1 | 1 | * +-----------------+-------------------+------------+----------+ */ @@ -77,7 +77,7 @@ static constexpr int kWriterReturnCode{ 666 }; _mustpush = 1; } // the provided writer fails with code kWriterReturnCode - // therefore, anytime we get kWriterReturnCode, this means that lua_dump() attempted a dump + // therefore, anytime we get kWriterReturnCode, this means that luaG_dump() attempted a dump // all other cases mean this is either a C or LuaJIT-fast function int const _dumpres{ luaG_dump(L_, dummy_writer, nullptr, 0) }; lua_pop(L_, _mustpush); -- cgit v1.2.3-55-g6feb