diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2025-07-04 13:50:53 +0200 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2025-07-04 13:50:53 +0200 |
| commit | 042055968ab0c48faec607889814e38c50c09efa (patch) | |
| tree | 4ec067a03ffcb720d8bf38968d5f84ccec8230d0 /src/universe.cpp | |
| parent | 963afcbb3d9dac47b84552ed11e53a0641ad924d (diff) | |
| download | lanes-042055968ab0c48faec607889814e38c50c09efa.tar.gz lanes-042055968ab0c48faec607889814e38c50c09efa.tar.bz2 lanes-042055968ab0c48faec607889814e38c50c09efa.zip | |
Changed lua wrapper prefixes from luaG_ to luaW_ (w as in wrapper!)
Diffstat (limited to 'src/universe.cpp')
| -rw-r--r-- | src/universe.cpp | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/src/universe.cpp b/src/universe.cpp index 3255c7f..044c841 100644 --- a/src/universe.cpp +++ b/src/universe.cpp | |||
| @@ -111,18 +111,18 @@ void Universe::callOnStateCreate(lua_State* const L_, lua_State* const from_, Lo | |||
| 111 | } | 111 | } |
| 112 | kConfigRegKey.pushValue(L_); // L_: config | 112 | kConfigRegKey.pushValue(L_); // L_: config |
| 113 | STACK_CHECK(L_, 1); | 113 | STACK_CHECK(L_, 1); |
| 114 | LuaType const _funcType{ luaG_getfield(L_, kIdxTop, kOnStateCreate) }; // L_: config on_state_create() | 114 | LuaType const _funcType{ luaW_getfield(L_, kIdxTop, kOnStateCreate) }; // L_: config on_state_create() |
| 115 | if (_funcType != LuaType::FUNCTION) { | 115 | if (_funcType != LuaType::FUNCTION) { |
| 116 | raise_luaL_error(L_, "INTERNAL ERROR: %s is a %s, not a function", kOnStateCreate.data(), luaG_typename(L_, _funcType).data()); | 116 | raise_luaL_error(L_, "INTERNAL ERROR: %s is a %s, not a function", kOnStateCreate.data(), luaW_typename(L_, _funcType).data()); |
| 117 | } | 117 | } |
| 118 | lua_remove(L_, -2); // L_: on_state_create() | 118 | lua_remove(L_, -2); // L_: on_state_create() |
| 119 | } | 119 | } |
| 120 | STACK_CHECK(L_, 1); | 120 | STACK_CHECK(L_, 1); |
| 121 | // capture error and raise it in caller state | 121 | // capture error and raise it in caller state |
| 122 | std::string_view const _stateType{ mode_ == LookupMode::LaneBody ? "lane" : "keeper" }; | 122 | std::string_view const _stateType{ mode_ == LookupMode::LaneBody ? "lane" : "keeper" }; |
| 123 | luaG_pushstring(L_, _stateType); // L_: on_state_create() "<type>" | 123 | luaW_pushstring(L_, _stateType); // L_: on_state_create() "<type>" |
| 124 | if (lua_pcall(L_, 1, 0, 0) != LUA_OK) { | 124 | if (lua_pcall(L_, 1, 0, 0) != LUA_OK) { |
| 125 | raise_luaL_error(from_, "%s failed in %s: \"%s\"", kOnStateCreate.data(), _stateType.data(), lua_isstring(L_, -1) ? luaG_tostring(L_, kIdxTop).data() : luaG_typename(L_, kIdxTop).data()); | 125 | raise_luaL_error(from_, "%s failed in %s: \"%s\"", kOnStateCreate.data(), _stateType.data(), lua_isstring(L_, -1) ? luaW_tostring(L_, kIdxTop).data() : luaW_typename(L_, kIdxTop).data()); |
| 126 | } | 126 | } |
| 127 | STACK_CHECK(L_, 0); | 127 | STACK_CHECK(L_, 0); |
| 128 | } | 128 | } |
| @@ -137,14 +137,14 @@ Universe* Universe::Create(lua_State* const L_) | |||
| 137 | static constexpr StackIndex kIdxSettings{ 1 }; | 137 | static constexpr StackIndex kIdxSettings{ 1 }; |
| 138 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); | 138 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); |
| 139 | STACK_CHECK_START_REL(L_, 0); // L_: settings | 139 | STACK_CHECK_START_REL(L_, 0); // L_: settings |
| 140 | std::ignore = luaG_getfield(L_, kIdxSettings, "nb_user_keepers"); // L_: settings nb_user_keepers | 140 | std::ignore = luaW_getfield(L_, kIdxSettings, "nb_user_keepers"); // L_: settings nb_user_keepers |
| 141 | int const _nbUserKeepers{ static_cast<int>(lua_tointeger(L_, -1)) + 1}; | 141 | int const _nbUserKeepers{ static_cast<int>(lua_tointeger(L_, -1)) + 1}; |
| 142 | lua_pop(L_, 1); // L_: settings | 142 | lua_pop(L_, 1); // L_: settings |
| 143 | if (_nbUserKeepers < 1) { | 143 | if (_nbUserKeepers < 1) { |
| 144 | raise_luaL_error(L_, "Bad number of additional keepers (%d)", _nbUserKeepers); | 144 | raise_luaL_error(L_, "Bad number of additional keepers (%d)", _nbUserKeepers); |
| 145 | } | 145 | } |
| 146 | STACK_CHECK(L_, 0); | 146 | STACK_CHECK(L_, 0); |
| 147 | std::ignore = luaG_getfield(L_, kIdxSettings, "keepers_gc_threshold"); // L_: settings keepers_gc_threshold | 147 | std::ignore = luaW_getfield(L_, kIdxSettings, "keepers_gc_threshold"); // L_: settings keepers_gc_threshold |
| 148 | int const _keepers_gc_threshold{ static_cast<int>(lua_tointeger(L_, -1)) }; | 148 | int const _keepers_gc_threshold{ static_cast<int>(lua_tointeger(L_, -1)) }; |
| 149 | lua_pop(L_, 1); // L_: settings | 149 | lua_pop(L_, 1); // L_: settings |
| 150 | STACK_CHECK(L_, 0); | 150 | STACK_CHECK(L_, 0); |
| @@ -157,30 +157,30 @@ Universe* Universe::Create(lua_State* const L_) | |||
| 157 | 157 | ||
| 158 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); | 158 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); |
| 159 | lua_createtable(L_, 0, 1); // L_: settings universe {mt} | 159 | lua_createtable(L_, 0, 1); // L_: settings universe {mt} |
| 160 | std::ignore = luaG_getfield(L_, kIdxSettings, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout | 160 | std::ignore = luaW_getfield(L_, kIdxSettings, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout |
| 161 | lua_pushcclosure(L_, UniverseGC, 1); // L_: settings universe {mt} UniverseGC | 161 | lua_pushcclosure(L_, UniverseGC, 1); // L_: settings universe {mt} UniverseGC |
| 162 | lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt} | 162 | lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt} |
| 163 | lua_setmetatable(L_, -2); // L_: settings universe | 163 | lua_setmetatable(L_, -2); // L_: settings universe |
| 164 | lua_pop(L_, 1); // L_: settings | 164 | lua_pop(L_, 1); // L_: settings |
| 165 | 165 | ||
| 166 | std::ignore = luaG_getfield(L_, kIdxSettings, "linda_wake_period"); // L_: settings linda_wake_period | 166 | std::ignore = luaW_getfield(L_, kIdxSettings, "linda_wake_period"); // L_: settings linda_wake_period |
| 167 | if (luaG_type(L_, kIdxTop) == LuaType::NUMBER) { | 167 | if (luaW_type(L_, kIdxTop) == LuaType::NUMBER) { |
| 168 | _U->lindaWakePeriod = lua_Duration{ lua_tonumber(L_, kIdxTop) }; | 168 | _U->lindaWakePeriod = lua_Duration{ lua_tonumber(L_, kIdxTop) }; |
| 169 | } else { | 169 | } else { |
| 170 | LUA_ASSERT(L_, luaG_tostring(L_, kIdxTop) == "never"); | 170 | LUA_ASSERT(L_, luaW_tostring(L_, kIdxTop) == "never"); |
| 171 | } | 171 | } |
| 172 | lua_pop(L_, 1); // L_: settings | 172 | lua_pop(L_, 1); // L_: settings |
| 173 | 173 | ||
| 174 | std::ignore = luaG_getfield(L_, kIdxSettings, "strip_functions"); // L_: settings strip_functions | 174 | std::ignore = luaW_getfield(L_, kIdxSettings, "strip_functions"); // L_: settings strip_functions |
| 175 | _U->stripFunctions = lua_toboolean(L_, -1) ? true : false; | 175 | _U->stripFunctions = lua_toboolean(L_, -1) ? true : false; |
| 176 | lua_pop(L_, 1); // L_: settings | 176 | lua_pop(L_, 1); // L_: settings |
| 177 | 177 | ||
| 178 | std::ignore = luaG_getfield(L_, kIdxSettings, "verbose_errors"); // L_: settings verbose_errors | 178 | std::ignore = luaW_getfield(L_, kIdxSettings, "verbose_errors"); // L_: settings verbose_errors |
| 179 | _U->verboseErrors = lua_toboolean(L_, -1) ? true : false; | 179 | _U->verboseErrors = lua_toboolean(L_, -1) ? true : false; |
| 180 | lua_pop(L_, 1); // L_: settings | 180 | lua_pop(L_, 1); // L_: settings |
| 181 | 181 | ||
| 182 | // tracking | 182 | // tracking |
| 183 | std::ignore = luaG_getfield(L_, kIdxSettings, "track_lanes"); // L_: settings track_lanes | 183 | std::ignore = luaW_getfield(L_, kIdxSettings, "track_lanes"); // L_: settings track_lanes |
| 184 | if (lua_toboolean(L_, -1)) { | 184 | if (lua_toboolean(L_, -1)) { |
| 185 | _U->tracker.activate(); | 185 | _U->tracker.activate(); |
| 186 | } | 186 | } |
| @@ -217,7 +217,7 @@ static void* libc_lua_Alloc([[maybe_unused]] void* const ud_, [[maybe_unused]] v | |||
| 217 | // ################################################################################################# | 217 | // ################################################################################################# |
| 218 | 218 | ||
| 219 | [[nodiscard]] | 219 | [[nodiscard]] |
| 220 | static int luaG_provide_protected_allocator(lua_State* const L_) | 220 | static int luaW_provide_protected_allocator(lua_State* const L_) |
| 221 | { | 221 | { |
| 222 | Universe* const _U{ Universe::Get(L_) }; | 222 | Universe* const _U{ Universe::Get(L_) }; |
| 223 | // push a new full userdata on the stack, giving access to the universe's protected allocator | 223 | // push a new full userdata on the stack, giving access to the universe's protected allocator |
| @@ -245,17 +245,17 @@ void Universe::initializeAllocatorFunction(lua_State* const L_) | |||
| 245 | // start by just grabbing whatever allocator was provided to the master state | 245 | // start by just grabbing whatever allocator was provided to the master state |
| 246 | protectedAllocator.initFrom(L_); | 246 | protectedAllocator.initFrom(L_); |
| 247 | STACK_CHECK_START_REL(L_, 1); // L_: settings | 247 | STACK_CHECK_START_REL(L_, 1); // L_: settings |
| 248 | switch (luaG_getfield(L_, kIdxTop, "allocator")) { // L_: settings allocator|nil|"protected" | 248 | switch (luaW_getfield(L_, kIdxTop, "allocator")) { // L_: settings allocator|nil|"protected" |
| 249 | case LuaType::NIL: | 249 | case LuaType::NIL: |
| 250 | // nothing else to do | 250 | // nothing else to do |
| 251 | break; | 251 | break; |
| 252 | 252 | ||
| 253 | case LuaType::STRING: | 253 | case LuaType::STRING: |
| 254 | LUA_ASSERT(L_, luaG_tostring(L_, kIdxTop) == "protected"); | 254 | LUA_ASSERT(L_, luaW_tostring(L_, kIdxTop) == "protected"); |
| 255 | // set the original allocator to call from inside protection by the mutex | 255 | // set the original allocator to call from inside protection by the mutex |
| 256 | protectedAllocator.installIn(L_); | 256 | protectedAllocator.installIn(L_); |
| 257 | // before a state is created, this function will be called to obtain the allocator | 257 | // before a state is created, this function will be called to obtain the allocator |
| 258 | provideAllocator = luaG_provide_protected_allocator; | 258 | provideAllocator = luaW_provide_protected_allocator; |
| 259 | break; | 259 | break; |
| 260 | 260 | ||
| 261 | case LuaType::FUNCTION: | 261 | case LuaType::FUNCTION: |
| @@ -276,14 +276,14 @@ void Universe::initializeAllocatorFunction(lua_State* const L_) | |||
| 276 | break; | 276 | break; |
| 277 | 277 | ||
| 278 | default: // should be filtered out in lanes.lua | 278 | default: // should be filtered out in lanes.lua |
| 279 | raise_luaL_error(L_, "Bad config.allocator type %s", luaG_typename(L_, kIdxTop).data()); | 279 | raise_luaL_error(L_, "Bad config.allocator type %s", luaW_typename(L_, kIdxTop).data()); |
| 280 | } | 280 | } |
| 281 | lua_pop(L_, 1); // L_: settings | 281 | lua_pop(L_, 1); // L_: settings |
| 282 | STACK_CHECK(L_, 1); | 282 | STACK_CHECK(L_, 1); |
| 283 | 283 | ||
| 284 | std::ignore = luaG_getfield(L_, kIdxTop, "internal_allocator"); // L_: settings "libc"|"allocator" | 284 | std::ignore = luaW_getfield(L_, kIdxTop, "internal_allocator"); // L_: settings "libc"|"allocator" |
| 285 | LUA_ASSERT(L_, lua_isstring(L_, kIdxTop)); // should be the case due to lanes.lua parameter validation | 285 | LUA_ASSERT(L_, lua_isstring(L_, kIdxTop)); // should be the case due to lanes.lua parameter validation |
| 286 | std::string_view const _allocator{ luaG_tostring(L_, kIdxTop) }; | 286 | std::string_view const _allocator{ luaW_tostring(L_, kIdxTop) }; |
| 287 | // use whatever the provider provides. This performs validation of what provideAllocator is giving | 287 | // use whatever the provider provides. This performs validation of what provideAllocator is giving |
| 288 | // we do this even if _allocator == "libc", to have the validation part | 288 | // we do this even if _allocator == "libc", to have the validation part |
| 289 | internalAllocator = resolveAndValidateAllocator(L_, "internal"); | 289 | internalAllocator = resolveAndValidateAllocator(L_, "internal"); |
| @@ -305,7 +305,7 @@ int Universe::InitializeFinalizer(lua_State* const L_) | |||
| 305 | 305 | ||
| 306 | // make sure we are only called from the Master Lua State! | 306 | // make sure we are only called from the Master Lua State! |
| 307 | kUniverseFullRegKey.pushValue(L_); // L_: f U | 307 | kUniverseFullRegKey.pushValue(L_); // L_: f U |
| 308 | if (luaG_type(L_, kIdxTop) != LuaType::USERDATA) { | 308 | if (luaW_type(L_, kIdxTop) != LuaType::USERDATA) { |
| 309 | raise_luaL_error(L_, "lanes.%s called from inside a lane", kFinally); | 309 | raise_luaL_error(L_, "lanes.%s called from inside a lane", kFinally); |
| 310 | } | 310 | } |
| 311 | lua_pop(L_, 1); // L_: f | 311 | lua_pop(L_, 1); // L_: f |
| @@ -321,8 +321,8 @@ int Universe::InitializeFinalizer(lua_State* const L_) | |||
| 321 | void Universe::initializeOnStateCreate(lua_State* const L_) | 321 | void Universe::initializeOnStateCreate(lua_State* const L_) |
| 322 | { | 322 | { |
| 323 | STACK_CHECK_START_REL(L_, 0); // L_: settings | 323 | STACK_CHECK_START_REL(L_, 0); // L_: settings |
| 324 | if (luaG_getfield(L_, kIdxTop, kOnStateCreate) != LuaType::NIL) { // L_: settings on_state_create|nil | 324 | if (luaW_getfield(L_, kIdxTop, kOnStateCreate) != LuaType::NIL) { // L_: settings on_state_create|nil |
| 325 | LUA_ASSERT(L_, luaG_type(L_, kIdxTop) == LuaType::FUNCTION); // ensured by lanes.lua parameter validation | 325 | LUA_ASSERT(L_, luaW_type(L_, kIdxTop) == LuaType::FUNCTION); // ensured by lanes.lua parameter validation |
| 326 | // store C function pointer in an internal variable | 326 | // store C function pointer in an internal variable |
| 327 | lua_CFunction const _func{ lua_tocfunction(L_, -1) }; // L_: settings on_state_create | 327 | lua_CFunction const _func{ lua_tocfunction(L_, -1) }; // L_: settings on_state_create |
| 328 | if (_func) { | 328 | if (_func) { |
| @@ -335,7 +335,7 @@ void Universe::initializeOnStateCreate(lua_State* const L_) | |||
| 335 | // remove this C function from the config table so that it doesn't cause problems | 335 | // remove this C function from the config table so that it doesn't cause problems |
| 336 | // when we transfer the config table in newly created Lua states | 336 | // when we transfer the config table in newly created Lua states |
| 337 | lua_pushnil(L_); // L_: settings on_state_create nil | 337 | lua_pushnil(L_); // L_: settings on_state_create nil |
| 338 | luaG_setfield(L_, StackIndex{ -3 }, kOnStateCreate); // L_: settings on_state_create | 338 | luaW_setfield(L_, StackIndex{ -3 }, kOnStateCreate); // L_: settings on_state_create |
| 339 | } else { | 339 | } else { |
| 340 | // the function is still in the config table. we indicate this with the uintptr_t alternative (actual value is irrelevant) | 340 | // the function is still in the config table. we indicate this with the uintptr_t alternative (actual value is irrelevant) |
| 341 | onStateCreateFunc.emplace<uintptr_t>(std::bit_cast<uintptr_t>(kOnStateCreate.data())); | 341 | onStateCreateFunc.emplace<uintptr_t>(std::bit_cast<uintptr_t>(kOnStateCreate.data())); |
| @@ -358,7 +358,7 @@ lanes::AllocatorDefinition Universe::resolveAndValidateAllocator(lua_State* cons | |||
| 358 | 358 | ||
| 359 | STACK_CHECK_START_REL(L_, 0); // here, we have a function we can call to obtain an allocator | 359 | STACK_CHECK_START_REL(L_, 0); // here, we have a function we can call to obtain an allocator |
| 360 | lua_pushcclosure(L_, provideAllocator, 0); // L_: provideAllocator() | 360 | lua_pushcclosure(L_, provideAllocator, 0); // L_: provideAllocator() |
| 361 | luaG_pushstring(L_, hint_); // L_: provideAllocator() "<hint>" | 361 | luaW_pushstring(L_, hint_); // L_: provideAllocator() "<hint>" |
| 362 | lua_call(L_, 1, 1); // L_: result | 362 | lua_call(L_, 1, 1); // L_: result |
| 363 | // make sure we have a valid AllocatorDefinition on the stack (an error is raised instead if it is not the case) | 363 | // make sure we have a valid AllocatorDefinition on the stack (an error is raised instead if it is not the case) |
| 364 | _ret = lanes::AllocatorDefinition::Validated(L_, kIdxTop); | 364 | _ret = lanes::AllocatorDefinition::Validated(L_, kIdxTop); |
| @@ -433,7 +433,7 @@ int Universe::UniverseGC(lua_State* const L_) | |||
| 433 | { | 433 | { |
| 434 | lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; | 434 | lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; |
| 435 | STACK_CHECK_START_ABS(L_, 1); | 435 | STACK_CHECK_START_ABS(L_, 1); |
| 436 | Universe* const _U{ luaG_tofulluserdata<Universe>(L_, StackIndex{ 1 }) }; // L_: U | 436 | Universe* const _U{ luaW_tofulluserdata<Universe>(L_, StackIndex{ 1 }) }; // L_: U |
| 437 | 437 | ||
| 438 | // attempt to terminate all lanes with increasingly stronger cancel methods | 438 | // attempt to terminate all lanes with increasingly stronger cancel methods |
| 439 | bool const _allLanesTerminated{ | 439 | bool const _allLanesTerminated{ |
| @@ -456,7 +456,7 @@ int Universe::UniverseGC(lua_State* const L_) | |||
| 456 | if (lua_isnil(L_, kIdxTop)) { | 456 | if (lua_isnil(L_, kIdxTop)) { |
| 457 | lua_pop(L_, 1); // L_: U | 457 | lua_pop(L_, 1); // L_: U |
| 458 | // no finalizer, or it returned no value: push some default message on the stack, in case it is necessary | 458 | // no finalizer, or it returned no value: push some default message on the stack, in case it is necessary |
| 459 | luaG_pushstring(L_, "uncooperative lanes detected at shutdown"); // L_: U "msg" | 459 | luaW_pushstring(L_, "uncooperative lanes detected at shutdown"); // L_: U "msg" |
| 460 | } | 460 | } |
| 461 | STACK_CHECK(L_, 2); | 461 | STACK_CHECK(L_, 2); |
| 462 | 462 | ||
| @@ -466,7 +466,7 @@ int Universe::UniverseGC(lua_State* const L_) | |||
| 466 | bool const _detectedUncooperativeLanes{ _U->selfdestructFirst != SELFDESTRUCT_END }; | 466 | bool const _detectedUncooperativeLanes{ _U->selfdestructFirst != SELFDESTRUCT_END }; |
| 467 | if (_detectedUncooperativeLanes) { | 467 | if (_detectedUncooperativeLanes) { |
| 468 | _U->flagDanglingLanes(); | 468 | _U->flagDanglingLanes(); |
| 469 | if (luaG_tostring(L_, kIdxTop) == "freeze") { | 469 | if (luaW_tostring(L_, kIdxTop) == "freeze") { |
| 470 | std::this_thread::sleep_until(std::chrono::time_point<std::chrono::steady_clock>::max()); | 470 | std::this_thread::sleep_until(std::chrono::time_point<std::chrono::steady_clock>::max()); |
| 471 | } else { | 471 | } else { |
| 472 | // take the value returned by the finalizer (or our default message) and throw it as an error | 472 | // take the value returned by the finalizer (or our default message) and throw it as an error |
