From 23f25f3a9d327153e4c16ca86d937ec334803a10 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 9 Apr 2024 10:12:30 +0200 Subject: C++ migration: still more threading code cleanup. 'sudo' global moved in the Universe --- src/deep.h | 2 +- src/keeper.h | 2 +- src/lanes.cpp | 32 +++----------------------------- src/state.h | 2 +- src/threading.cpp | 22 ++++++++-------------- src/threading.h | 24 +++++------------------- src/tools.h | 2 +- src/universe.cpp | 29 +++++++++++++++++++++++++++++ src/universe.h | 20 ++++++++++++++++++-- 9 files changed, 67 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/deep.h b/src/deep.h index 1799cf0..c09af23 100644 --- a/src/deep.h +++ b/src/deep.h @@ -19,7 +19,7 @@ extern "C" { #include // forwards -struct Universe; +class Universe; enum class LookupMode { diff --git a/src/keeper.h b/src/keeper.h index 931c1d5..ba5a57b 100644 --- a/src/keeper.h +++ b/src/keeper.h @@ -15,7 +15,7 @@ extern "C" { // forwards enum class LookupMode; -struct Universe; +class Universe; struct Keeper { diff --git a/src/lanes.cpp b/src/lanes.cpp index f5d5130..28f95f6 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -136,7 +136,7 @@ void Lane::startThread(int priority_) m_thread = std::jthread([this]() { lane_main(this); }); if (priority_ != THREAD_PRIO_DEFAULT) { - JTHREAD_SET_PRIORITY(m_thread, priority_); + JTHREAD_SET_PRIORITY(m_thread, priority_, U->m_sudo); } } @@ -784,7 +784,7 @@ LUAG_FUNC(get_debug_threadname) LUAG_FUNC(set_thread_priority) { - int const prio{ (int) luaL_checkinteger(L, 1) }; + lua_Integer const prio{ luaL_checkinteger(L, 1) }; // 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 @@ -792,7 +792,7 @@ LUAG_FUNC(set_thread_priority) { return luaL_error(L, "priority out of range: %d..+%d (%d)", THREAD_PRIO_MIN, THREAD_PRIO_MAX, prio); } - THREAD_SET_PRIORITY(prio); + THREAD_SET_PRIORITY(static_cast(prio), universe_get(L)->m_sudo); return 0; } @@ -1746,32 +1746,6 @@ static void init_once_LOCKED( void) #if (defined PLATFORM_OSX) && (defined _UTILBINDTHREADTOCPU) chudInitialize(); #endif - - //--- - // Linux needs SCHED_RR to change thread priorities, and that is only - // allowed for sudo'ers. SCHED_OTHER (default) has no priorities. - // SCHED_OTHER threads are always lower priority than SCHED_RR. - // - // ^-- those apply to 2.6 kernel. IF **wishful thinking** these - // constraints will change in the future, non-sudo priorities can - // be enabled also for Linux. - // -#ifdef PLATFORM_LINUX - sudo = (geteuid() == 0); // we are root? - - // If lower priorities (-2..-1) are wanted, we need to lift the main - // thread to SCHED_RR and 50 (medium) level. Otherwise, we're always below - // the launched threads (even -2). - // -#ifdef LINUX_SCHED_RR - if (sudo) - { - struct sched_param sp; - sp.sched_priority = _PRIO_0; - PT_CALL(pthread_setschedparam(pthread_self(), SCHED_RR, &sp)); - } -#endif // LINUX_SCHED_RR -#endif // PLATFORM_LINUX } // ################################################################################################# diff --git a/src/state.h b/src/state.h index 0e35e89..0e069da 100644 --- a/src/state.h +++ b/src/state.h @@ -3,7 +3,7 @@ #include "macros_and_utils.h" // forwards -struct Universe; +class Universe; void serialize_require(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L); diff --git a/src/threading.cpp b/src/threading.cpp index aab2fa7..4d210d6 100644 --- a/src/threading.cpp +++ b/src/threading.cpp @@ -65,12 +65,6 @@ THE SOFTWARE. # include #endif -/* Linux needs to check, whether it's been run as root -*/ -#ifdef PLATFORM_LINUX - volatile bool sudo; -#endif - #ifdef PLATFORM_OSX # include "threading_osx.h" #endif @@ -134,10 +128,10 @@ static int const gs_prio_remap[] = // ############################################################################################### -void THREAD_SET_PRIORITY(int prio) +void THREAD_SET_PRIORITY(int prio_, [[maybe_unused]] bool sudo_) { // prio range [-3,+3] was checked by the caller - if (!SetThreadPriority(GetCurrentThread(), gs_prio_remap[prio + 3])) + if (!SetThreadPriority(GetCurrentThread(), gs_prio_remap[prio_ + 3])) { FAIL("THREAD_SET_PRIORITY", GetLastError()); } @@ -145,7 +139,7 @@ void THREAD_SET_PRIORITY(int prio) // ############################################################################################### -void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_) +void JTHREAD_SET_PRIORITY(std::jthread& 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])) @@ -368,25 +362,25 @@ static int select_prio(int prio /* -3..+3 */) return gs_prio_remap[prio + 3]; } -void THREAD_SET_PRIORITY(int prio) +void THREAD_SET_PRIORITY(int prio_, [[maybe_unused]] bool sudo_) { #ifdef PLATFORM_LINUX - if (!sudo) // only root-privileged process can change priorities + if (!sudo_) // only root-privileged process can change priorities return; #endif // PLATFORM_LINUX struct sched_param sp; // prio range [-3,+3] was checked by the caller - sp.sched_priority = gs_prio_remap[prio + 3]; + sp.sched_priority = gs_prio_remap[prio_ + 3]; PT_CALL(pthread_setschedparam(pthread_self(), _PRIO_MODE, &sp)); } // ################################################################################################# -void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_) +void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, [[maybe_unused]] bool sudo_) { #ifdef PLATFORM_LINUX - if (!sudo) // only root-privileged process can change priorities + if (!sudo_) // only root-privileged process can change priorities return; #endif // PLATFORM_LINUX diff --git a/src/threading.h b/src/threading.h index c443c82..f38b2de 100644 --- a/src/threading.h +++ b/src/threading.h @@ -58,33 +58,19 @@ static constexpr int THREAD_PRIO_MAX{ +3 }; #endif // PLATFORM_WIN32 #include -// Yield is non-portable: -// -// OS X 10.4.8/9 has pthread_yield_np() -// Linux 2.4 has pthread_yield() if _GNU_SOURCE is #defined -// FreeBSD 6.2 has pthread_yield() -// ... -// - -#if defined(PLATFORM_LINUX) -extern volatile bool sudo; -# ifdef LINUX_SCHED_RR -# define THREAD_PRIO_MIN (sudo ? -3 : 0) -# else +#if defined(PLATFORM_LINUX) && !defined(LINUX_SCHED_RR) static constexpr int THREAD_PRIO_MIN{ 0 }; -#endif -# define THREAD_PRIO_MAX (sudo ? +3 : 0) #else static constexpr int THREAD_PRIO_MIN{ -3 }; -static constexpr int THREAD_PRIO_MAX{ +3 }; #endif +static constexpr int THREAD_PRIO_MAX{ +3 }; -#endif // THREADAPI == THREADAPI_WINDOWS +#endif // THREADAPI == THREADAPI_PTHREAD // ################################################################################################## // ################################################################################################## void THREAD_SETNAME(char const* _name); -void THREAD_SET_PRIORITY(int prio); +void THREAD_SET_PRIORITY(int prio_, bool sudo_); void THREAD_SET_AFFINITY(unsigned int aff); -void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_); +void JTHREAD_SET_PRIORITY(std::jthread& thread_, int prio_, bool sudo_); diff --git a/src/tools.h b/src/tools.h index c1a8534..7d9aaab 100644 --- a/src/tools.h +++ b/src/tools.h @@ -6,7 +6,7 @@ #include "macros_and_utils.h" // forwards -struct Universe; +class Universe; // ################################################################################################ diff --git a/src/universe.cpp b/src/universe.cpp index 66da147..290e547 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -43,6 +43,35 @@ static constexpr UniqueKey UNIVERSE_LIGHT_REGKEY{ 0x3663C07C742CEB81ull }; // ################################################################################################ +Universe::Universe() +{ + //--- + // Linux needs SCHED_RR to change thread priorities, and that is only + // allowed for sudo'ers. SCHED_OTHER (default) has no priorities. + // SCHED_OTHER threads are always lower priority than SCHED_RR. + // + // ^-- those apply to 2.6 kernel. IF **wishful thinking** these + // constraints will change in the future, non-sudo priorities can + // be enabled also for Linux. + // +#ifdef PLATFORM_LINUX + // If lower priorities (-2..-1) are wanted, we need to lift the main + // thread to SCHED_RR and 50 (medium) level. Otherwise, we're always below + // the launched threads (even -2). + // +#ifdef LINUX_SCHED_RR + if (m_sudo) + { + struct sched_param sp; + sp.sched_priority = _PRIO_0; + PT_CALL(pthread_setschedparam(pthread_self(), SCHED_RR, &sp)); + } +#endif // LINUX_SCHED_RR +#endif // PLATFORM_LINUX +} + +// ################################################################################################ + // only called from the master state Universe* universe_create(lua_State* L) { diff --git a/src/universe.h b/src/universe.h index 38885cb..f4211af 100644 --- a/src/universe.h +++ b/src/universe.h @@ -119,9 +119,17 @@ class ProtectedAllocator : public AllocatorDefinition // everything regarding the Lanes universe is stored in that global structure // held as a full userdata in the master Lua state that required it for the first time -// don't forget to initialize all members in LG_configure() -struct Universe +class Universe { + public: + +#ifdef PLATFORM_LINUX + // Linux needs to check, whether it's been run as root + bool const m_sudo{ geteuid() == 0 }; +#else + bool const m_sudo{ false }; +#endif // PLATFORM_LINUX + // for verbose errors bool verboseErrors{ false }; @@ -155,6 +163,7 @@ struct Universe // require() serialization std::recursive_mutex require_cs; + // metatable unique identifiers std::atomic next_mt_id{ 1 }; #if USE_DEBUG_SPEW() @@ -165,6 +174,13 @@ struct Universe // 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 std::atomic selfdestructing_count{ 0 }; + + Universe(); + ~Universe() = default; + Universe(Universe const&) = delete; + Universe(Universe&&) = delete; + Universe& operator=(Universe const&) = delete; + Universe& operator=(Universe&&) = delete; }; // ################################################################################################ -- cgit v1.2.3-55-g6feb