diff options
Diffstat (limited to 'src/lanes.cpp')
-rw-r--r-- | src/lanes.cpp | 662 |
1 files changed, 331 insertions, 331 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp index ee40ffa..f0ec84c 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
@@ -116,7 +116,7 @@ THE SOFTWARE. | |||
116 | */ | 116 | */ |
117 | static void tracking_add(Lane* lane_) | 117 | static void tracking_add(Lane* lane_) |
118 | { | 118 | { |
119 | std::lock_guard<std::mutex> guard{ lane_->U->trackingMutex }; | 119 | std::lock_guard<std::mutex> _guard{ lane_->U->trackingMutex }; |
120 | assert(lane_->tracking_next == nullptr); | 120 | assert(lane_->tracking_next == nullptr); |
121 | 121 | ||
122 | lane_->tracking_next = lane_->U->trackingFirst; | 122 | lane_->tracking_next = lane_->U->trackingFirst; |
@@ -130,27 +130,27 @@ static void tracking_add(Lane* lane_) | |||
130 | */ | 130 | */ |
131 | [[nodiscard]] static bool tracking_remove(Lane* lane_) | 131 | [[nodiscard]] static bool tracking_remove(Lane* lane_) |
132 | { | 132 | { |
133 | bool found{ false }; | 133 | bool _found{ false }; |
134 | std::lock_guard<std::mutex> guard{ lane_->U->trackingMutex }; | 134 | std::lock_guard<std::mutex> _guard{ lane_->U->trackingMutex }; |
135 | // Make sure (within the MUTEX) that we actually are in the chain | 135 | // Make sure (within the MUTEX) that we actually are in the chain |
136 | // still (at process exit they will remove us from chain and then | 136 | // still (at process exit they will remove us from chain and then |
137 | // cancel/kill). | 137 | // cancel/kill). |
138 | // | 138 | // |
139 | if (lane_->tracking_next != nullptr) { | 139 | if (lane_->tracking_next != nullptr) { |
140 | Lane** ref = (Lane**) &lane_->U->trackingFirst; | 140 | Lane** _ref = (Lane**) &lane_->U->trackingFirst; |
141 | 141 | ||
142 | while (*ref != TRACKING_END) { | 142 | while (*_ref != TRACKING_END) { |
143 | if (*ref == lane_) { | 143 | if (*_ref == lane_) { |
144 | *ref = lane_->tracking_next; | 144 | *_ref = lane_->tracking_next; |
145 | lane_->tracking_next = nullptr; | 145 | lane_->tracking_next = nullptr; |
146 | found = true; | 146 | _found = true; |
147 | break; | 147 | break; |
148 | } | 148 | } |
149 | ref = (Lane**) &((*ref)->tracking_next); | 149 | _ref = (Lane**) &((*_ref)->tracking_next); |
150 | } | 150 | } |
151 | assert(found); | 151 | assert(_found); |
152 | } | 152 | } |
153 | return found; | 153 | return _found; |
154 | } | 154 | } |
155 | 155 | ||
156 | #endif // HAVE_LANE_TRACKING() | 156 | #endif // HAVE_LANE_TRACKING() |
@@ -172,15 +172,15 @@ Lane::Lane(Universe* U_, lua_State* L_) | |||
172 | 172 | ||
173 | bool Lane::waitForCompletion(std::chrono::time_point<std::chrono::steady_clock> until_) | 173 | bool Lane::waitForCompletion(std::chrono::time_point<std::chrono::steady_clock> until_) |
174 | { | 174 | { |
175 | std::unique_lock lock{ doneMutex }; | 175 | std::unique_lock _guard{ doneMutex }; |
176 | // std::stop_token token{ thread.get_stop_token() }; | 176 | // std::stop_token token{ thread.get_stop_token() }; |
177 | // return doneCondVar.wait_until(lock, token, secs_, [this](){ return status >= Lane::Done; }); | 177 | // return doneCondVar.wait_until(lock, token, secs_, [this](){ return status >= Lane::Done; }); |
178 | return doneCondVar.wait_until(lock, until_, [this]() { return status >= Lane::Done; }); | 178 | return doneCondVar.wait_until(_guard, until_, [this]() { return status >= Lane::Done; }); |
179 | } | 179 | } |
180 | 180 | ||
181 | // ################################################################################################# | 181 | // ################################################################################################# |
182 | 182 | ||
183 | static void lane_main(Lane* lane); | 183 | static void lane_main(Lane* lane_); |
184 | void Lane::startThread(int priority_) | 184 | void Lane::startThread(int priority_) |
185 | { | 185 | { |
186 | thread = std::jthread([this]() { lane_main(this); }); | 186 | thread = std::jthread([this]() { lane_main(this); }); |
@@ -267,9 +267,9 @@ LUAG_FUNC(set_finalizer) | |||
267 | // Get the current finalizer table (if any), create one if it doesn't exist | 267 | // Get the current finalizer table (if any), create one if it doesn't exist |
268 | std::ignore = kFinalizerRegKey.getSubTable(L_, 1, 0); // L_: finalizer {finalisers} | 268 | std::ignore = kFinalizerRegKey.getSubTable(L_, 1, 0); // L_: finalizer {finalisers} |
269 | // must cast to int, not lua_Integer, because LuaJIT signature of lua_rawseti is not the same as PUC-Lua. | 269 | // must cast to int, not lua_Integer, because LuaJIT signature of lua_rawseti is not the same as PUC-Lua. |
270 | int const idx{ static_cast<int>(lua_rawlen(L_, -1) + 1) }; | 270 | int const _idx{ static_cast<int>(lua_rawlen(L_, -1) + 1) }; |
271 | lua_pushvalue(L_, 1); // L_: finalizer {finalisers} finalizer | 271 | lua_pushvalue(L_, 1); // L_: finalizer {finalisers} finalizer |
272 | lua_rawseti(L_, -2, idx); // L_: finalizer {finalisers} | 272 | lua_rawseti(L_, -2, _idx); // L_: finalizer {finalisers} |
273 | // no need to adjust the stack, Lua does this for us | 273 | // no need to adjust the stack, Lua does this for us |
274 | return 0; | 274 | return 0; |
275 | } | 275 | } |
@@ -337,28 +337,28 @@ static void push_stack_trace(lua_State* L_, int rc_, int stk_base_) | |||
337 | 337 | ||
338 | STACK_GROW(L_, 5); | 338 | STACK_GROW(L_, 5); |
339 | 339 | ||
340 | int const finalizers_index{ lua_gettop(L_) }; | 340 | int const _finalizers_index{ lua_gettop(L_) }; |
341 | int const err_handler_index{ ERROR_FULL_STACK ? (lua_pushcfunction(L_, lane_error), lua_gettop(L_)) : 0 }; | 341 | int const _err_handler_index{ ERROR_FULL_STACK ? (lua_pushcfunction(L_, lane_error), lua_gettop(L_)) : 0 }; |
342 | 342 | ||
343 | int rc{ LUA_OK }; | 343 | int rc{ LUA_OK }; |
344 | for (int n = static_cast<int>(lua_rawlen(L_, finalizers_index)); n > 0; --n) { | 344 | for (int n = static_cast<int>(lua_rawlen(L_, _finalizers_index)); n > 0; --n) { |
345 | int args = 0; | 345 | int args = 0; |
346 | lua_pushinteger(L_, n); // L_: ... finalizers lane_error n | 346 | lua_pushinteger(L_, n); // L_: ... finalizers lane_error n |
347 | lua_rawget(L_, finalizers_index); // L_: ... finalizers lane_error finalizer | 347 | lua_rawget(L_, _finalizers_index); // L_: ... finalizers lane_error finalizer |
348 | LUA_ASSERT(L_, lua_isfunction(L_, -1)); | 348 | LUA_ASSERT(L_, lua_isfunction(L_, -1)); |
349 | if (lua_rc_ != LUA_OK) { // we have an error message and an optional stack trace at the bottom of the stack | 349 | if (lua_rc_ != LUA_OK) { // we have an error message and an optional stack trace at the bottom of the stack |
350 | LUA_ASSERT(L_, finalizers_index == 2 || finalizers_index == 3); | 350 | LUA_ASSERT(L_, _finalizers_index == 2 || _finalizers_index == 3); |
351 | // char const* err_msg = lua_tostring(L_, 1); | 351 | // char const* err_msg = lua_tostring(L_, 1); |
352 | lua_pushvalue(L_, 1); // L_: ... finalizers lane_error finalizer err_msg | 352 | lua_pushvalue(L_, 1); // L_: ... finalizers lane_error finalizer err_msg |
353 | // 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 | 353 | // 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 |
354 | if (finalizers_index == 3) { | 354 | if (_finalizers_index == 3) { |
355 | lua_pushvalue(L_, 2); // L_: ... finalizers lane_error finalizer err_msg stack_trace | 355 | lua_pushvalue(L_, 2); // L_: ... finalizers lane_error finalizer err_msg stack_trace |
356 | } | 356 | } |
357 | args = finalizers_index - 1; | 357 | args = _finalizers_index - 1; |
358 | } | 358 | } |
359 | 359 | ||
360 | // if no error from the main body, finalizer doesn't receive any argument, else it gets the error message and optional stack trace | 360 | // if no error from the main body, finalizer doesn't receive any argument, else it gets the error message and optional stack trace |
361 | rc = lua_pcall(L_, args, 0, err_handler_index); // L_: ... finalizers lane_error err_msg2? | 361 | rc = lua_pcall(L_, args, 0, _err_handler_index); // L_: ... finalizers lane_error err_msg2? |
362 | if (rc != LUA_OK) { | 362 | if (rc != LUA_OK) { |
363 | push_stack_trace(L_, rc, lua_gettop(L_)); // L_: ... finalizers lane_error err_msg2? trace | 363 | push_stack_trace(L_, rc, lua_gettop(L_)); // L_: ... finalizers lane_error err_msg2? trace |
364 | // If one finalizer fails, don't run the others. Return this | 364 | // If one finalizer fails, don't run the others. Return this |
@@ -371,7 +371,7 @@ static void push_stack_trace(lua_State* L_, int rc_, int stk_base_) | |||
371 | 371 | ||
372 | if (rc != LUA_OK) { | 372 | if (rc != LUA_OK) { |
373 | // ERROR_FULL_STACK accounts for the presence of lane_error on the stack | 373 | // ERROR_FULL_STACK accounts for the presence of lane_error on the stack |
374 | int const nb_err_slots{ lua_gettop(L_) - finalizers_index - ERROR_FULL_STACK }; | 374 | int const nb_err_slots{ lua_gettop(L_) - _finalizers_index - ERROR_FULL_STACK }; |
375 | // a finalizer generated an error, this is what we leave of the stack | 375 | // a finalizer generated an error, this is what we leave of the stack |
376 | for (int n = nb_err_slots; n > 0; --n) { | 376 | for (int n = nb_err_slots; n > 0; --n) { |
377 | lua_replace(L_, n); | 377 | lua_replace(L_, n); |
@@ -379,7 +379,7 @@ static void push_stack_trace(lua_State* L_, int rc_, int stk_base_) | |||
379 | // leave on the stack only the error and optional stack trace produced by the error in the finalizer | 379 | // leave on the stack only the error and optional stack trace produced by the error in the finalizer |
380 | lua_settop(L_, nb_err_slots); // L_: ... lane_error trace | 380 | lua_settop(L_, nb_err_slots); // L_: ... lane_error trace |
381 | } else { // no error from the finalizers, make sure only the original return values from the lane body remain on the stack | 381 | } else { // no error from the finalizers, make sure only the original return values from the lane body remain on the stack |
382 | lua_settop(L_, finalizers_index - 1); | 382 | lua_settop(L_, _finalizers_index - 1); |
383 | } | 383 | } |
384 | 384 | ||
385 | return rc; | 385 | return rc; |
@@ -395,7 +395,7 @@ static void push_stack_trace(lua_State* L_, int rc_, int stk_base_) | |||
395 | */ | 395 | */ |
396 | static void selfdestruct_add(Lane* lane_) | 396 | static void selfdestruct_add(Lane* lane_) |
397 | { | 397 | { |
398 | std::lock_guard<std::mutex> guard{ lane_->U->selfdestructMutex }; | 398 | std::lock_guard<std::mutex> _guard{ lane_->U->selfdestructMutex }; |
399 | assert(lane_->selfdestruct_next == nullptr); | 399 | assert(lane_->selfdestruct_next == nullptr); |
400 | 400 | ||
401 | lane_->selfdestruct_next = lane_->U->selfdestructFirst; | 401 | lane_->selfdestruct_next = lane_->U->selfdestructFirst; |
@@ -407,29 +407,29 @@ static void selfdestruct_add(Lane* lane_) | |||
407 | // A free-running lane has ended; remove it from selfdestruct chain | 407 | // A free-running lane has ended; remove it from selfdestruct chain |
408 | [[nodiscard]] static bool selfdestruct_remove(Lane* lane_) | 408 | [[nodiscard]] static bool selfdestruct_remove(Lane* lane_) |
409 | { | 409 | { |
410 | bool found{ false }; | 410 | bool _found{ false }; |
411 | std::lock_guard<std::mutex> guard{ lane_->U->selfdestructMutex }; | 411 | std::lock_guard<std::mutex> _guard{ lane_->U->selfdestructMutex }; |
412 | // Make sure (within the MUTEX) that we actually are in the chain | 412 | // Make sure (within the MUTEX) that we actually are in the chain |
413 | // still (at process exit they will remove us from chain and then | 413 | // still (at process exit they will remove us from chain and then |
414 | // cancel/kill). | 414 | // cancel/kill). |
415 | // | 415 | // |
416 | if (lane_->selfdestruct_next != nullptr) { | 416 | if (lane_->selfdestruct_next != nullptr) { |
417 | Lane* volatile* ref = static_cast<Lane* volatile*>(&lane_->U->selfdestructFirst); | 417 | Lane* volatile* _ref = static_cast<Lane* volatile*>(&lane_->U->selfdestructFirst); |
418 | 418 | ||
419 | while (*ref != SELFDESTRUCT_END) { | 419 | while (*_ref != SELFDESTRUCT_END) { |
420 | if (*ref == lane_) { | 420 | if (*_ref == lane_) { |
421 | *ref = lane_->selfdestruct_next; | 421 | *_ref = lane_->selfdestruct_next; |
422 | lane_->selfdestruct_next = nullptr; | 422 | lane_->selfdestruct_next = nullptr; |
423 | // the terminal shutdown should wait until the lane is done with its lua_close() | 423 | // the terminal shutdown should wait until the lane is done with its lua_close() |
424 | lane_->U->selfdestructingCount.fetch_add(1, std::memory_order_release); | 424 | lane_->U->selfdestructingCount.fetch_add(1, std::memory_order_release); |
425 | found = true; | 425 | _found = true; |
426 | break; | 426 | break; |
427 | } | 427 | } |
428 | ref = static_cast<Lane* volatile*>(&((*ref)->selfdestruct_next)); | 428 | _ref = static_cast<Lane* volatile*>(&((*_ref)->selfdestruct_next)); |
429 | } | 429 | } |
430 | assert(found); | 430 | assert(_found); |
431 | } | 431 | } |
432 | return found; | 432 | return _found; |
433 | } | 433 | } |
434 | 434 | ||
435 | // ################################################################################################# | 435 | // ################################################################################################# |
@@ -442,11 +442,11 @@ static void selfdestruct_add(Lane* lane_) | |||
442 | // | 442 | // |
443 | LUAG_FUNC(set_singlethreaded) | 443 | LUAG_FUNC(set_singlethreaded) |
444 | { | 444 | { |
445 | [[maybe_unused]] lua_Integer const cores{ luaL_optinteger(L_, 1, 1) }; | 445 | [[maybe_unused]] lua_Integer const _cores{ luaL_optinteger(L_, 1, 1) }; |
446 | 446 | ||
447 | #ifdef PLATFORM_OSX | 447 | #ifdef PLATFORM_OSX |
448 | #ifdef _UTILBINDTHREADTOCPU | 448 | #ifdef _UTILBINDTHREADTOCPU |
449 | if (cores > 1) { | 449 | if (_cores > 1) { |
450 | raise_luaL_error(L_, "Limiting to N>1 cores not possible"); | 450 | raise_luaL_error(L_, "Limiting to N>1 cores not possible"); |
451 | } | 451 | } |
452 | // requires 'chudInitialize()' | 452 | // requires 'chudInitialize()' |
@@ -486,15 +486,15 @@ static constexpr RegistryUniqueKey kExtendedStackTraceRegKey{ 0x38147AD48FB426E2 | |||
486 | LUAG_FUNC(set_error_reporting) | 486 | LUAG_FUNC(set_error_reporting) |
487 | { | 487 | { |
488 | luaL_checktype(L_, 1, LUA_TSTRING); | 488 | luaL_checktype(L_, 1, LUA_TSTRING); |
489 | char const* mode{ lua_tostring(L_, 1) }; | 489 | char const* _mode{ lua_tostring(L_, 1) }; |
490 | lua_pushliteral(L_, "extended"); | 490 | lua_pushliteral(L_, "extended"); |
491 | bool const extended{ strcmp(mode, "extended") == 0 }; | 491 | bool const _extended{ strcmp(_mode, "extended") == 0 }; |
492 | bool const basic{ strcmp(mode, "basic") == 0 }; | 492 | bool const _basic{ strcmp(_mode, "basic") == 0 }; |
493 | if (!extended && !basic) { | 493 | if (!_extended && !_basic) { |
494 | raise_luaL_error(L_, "unsupported error reporting model %s", mode); | 494 | raise_luaL_error(L_, "unsupported error reporting model %s", _mode); |
495 | } | 495 | } |
496 | 496 | ||
497 | kExtendedStackTraceRegKey.setValue(L_, [extended](lua_State* L_) { lua_pushboolean(L_, extended ? 1 : 0); }); | 497 | kExtendedStackTraceRegKey.setValue(L_, [extended = _extended](lua_State* L_) { lua_pushboolean(L_, extended ? 1 : 0); }); |
498 | return 0; | 498 | return 0; |
499 | } | 499 | } |
500 | 500 | ||
@@ -512,7 +512,7 @@ LUAG_FUNC(set_error_reporting) | |||
512 | } | 512 | } |
513 | 513 | ||
514 | STACK_GROW(L_, 3); | 514 | STACK_GROW(L_, 3); |
515 | bool const extended{ kExtendedStackTraceRegKey.readBoolValue(L_) }; | 515 | bool const _extended{ kExtendedStackTraceRegKey.readBoolValue(L_) }; |
516 | STACK_CHECK(L_, 1); | 516 | STACK_CHECK(L_, 1); |
517 | 517 | ||
518 | // Place stack trace at 'registry[kStackTraceRegKey]' for the 'lua_pcall()' | 518 | // Place stack trace at 'registry[kStackTraceRegKey]' for the 'lua_pcall()' |
@@ -531,32 +531,32 @@ LUAG_FUNC(set_error_reporting) | |||
531 | // and we don't get '.currentline' for that. It's okay - just keep level | 531 | // and we don't get '.currentline' for that. It's okay - just keep level |
532 | // and table index growing separate. --AKa 22-Jan-2009 | 532 | // and table index growing separate. --AKa 22-Jan-2009 |
533 | // | 533 | // |
534 | lua_Debug ar; | 534 | lua_Debug _ar; |
535 | for (int n = 1; lua_getstack(L_, n, &ar); ++n) { | 535 | for (int _n = 1; lua_getstack(L_, _n, &_ar); ++_n) { |
536 | lua_getinfo(L_, extended ? "Sln" : "Sl", &ar); | 536 | lua_getinfo(L_, _extended ? "Sln" : "Sl", &_ar); |
537 | if (extended) { | 537 | if (_extended) { |
538 | lua_newtable(L_); // L_: some_error {} {} | 538 | lua_newtable(L_); // L_: some_error {} {} |
539 | 539 | ||
540 | lua_pushstring(L_, ar.source); // L_: some_error {} {} source | 540 | lua_pushstring(L_, _ar.source); // L_: some_error {} {} source |
541 | lua_setfield(L_, -2, "source"); // L_: some_error {} {} | 541 | lua_setfield(L_, -2, "source"); // L_: some_error {} {} |
542 | 542 | ||
543 | lua_pushinteger(L_, ar.currentline); // L_: some_error {} {} currentline | 543 | lua_pushinteger(L_, _ar.currentline); // L_: some_error {} {} currentline |
544 | lua_setfield(L_, -2, "currentline"); // L_: some_error {} {} | 544 | lua_setfield(L_, -2, "currentline"); // L_: some_error {} {} |
545 | 545 | ||
546 | lua_pushstring(L_, ar.name); // L_: some_error {} {} name | 546 | lua_pushstring(L_, _ar.name); // L_: some_error {} {} name |
547 | lua_setfield(L_, -2, "name"); // L_: some_error {} {} | 547 | lua_setfield(L_, -2, "name"); // L_: some_error {} {} |
548 | 548 | ||
549 | lua_pushstring(L_, ar.namewhat); // L_: some_error {} {} namewhat | 549 | lua_pushstring(L_, _ar.namewhat); // L_: some_error {} {} namewhat |
550 | lua_setfield(L_, -2, "namewhat"); // L_: some_error {} {} | 550 | lua_setfield(L_, -2, "namewhat"); // L_: some_error {} {} |
551 | 551 | ||
552 | lua_pushstring(L_, ar.what); // L_: some_error {} {} what | 552 | lua_pushstring(L_, _ar.what); // L_: some_error {} {} what |
553 | lua_setfield(L_, -2, "what"); // L_: some_error {} {} | 553 | lua_setfield(L_, -2, "what"); // L_: some_error {} {} |
554 | } else if (ar.currentline > 0) { | 554 | } else if (_ar.currentline > 0) { |
555 | lua_pushfstring(L_, "%s:%d", ar.short_src, ar.currentline); // L_: some_error {} "blah:blah" | 555 | lua_pushfstring(L_, "%s:%d", _ar.short_src, _ar.currentline); // L_: some_error {} "blah:blah" |
556 | } else { | 556 | } else { |
557 | lua_pushfstring(L_, "%s:?", ar.short_src); // L_: some_error {} "blah" | 557 | lua_pushfstring(L_, "%s:?", _ar.short_src); // L_: some_error {} "blah" |
558 | } | 558 | } |
559 | lua_rawseti(L_, -2, (lua_Integer) n); // L_: some_error {} | 559 | lua_rawseti(L_, -2, static_cast<lua_Integer>(_n)); // L_: some_error {} |
560 | } | 560 | } |
561 | 561 | ||
562 | // store the stack trace table in the registry | 562 | // store the stack trace table in the registry |
@@ -572,12 +572,12 @@ LUAG_FUNC(set_error_reporting) | |||
572 | void Lane::changeDebugName(int nameIdx_) | 572 | void Lane::changeDebugName(int nameIdx_) |
573 | { | 573 | { |
574 | // xxh64 of string "debugName" generated at https://www.pelock.com/products/hash-calculator | 574 | // xxh64 of string "debugName" generated at https://www.pelock.com/products/hash-calculator |
575 | static constexpr RegistryUniqueKey hidden_regkey{ 0xA194E2645C57F6DDull }; | 575 | static constexpr RegistryUniqueKey kRegKey{ 0xA194E2645C57F6DDull }; |
576 | nameIdx_ = lua_absindex(L, nameIdx_); | 576 | nameIdx_ = lua_absindex(L, nameIdx_); |
577 | luaL_checktype(L, nameIdx_, LUA_TSTRING); // L: ... "name" ... | 577 | luaL_checktype(L, nameIdx_, LUA_TSTRING); // L: ... "name" ... |
578 | STACK_CHECK_START_REL(L, 0); | 578 | STACK_CHECK_START_REL(L, 0); |
579 | // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... | 579 | // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... |
580 | hidden_regkey.setValue(L, [nameIdx = nameIdx_](lua_State* L_) { lua_pushvalue(L_, nameIdx); });// L: ... "name" ... | 580 | kRegKey.setValue(L, [nameIdx = nameIdx_](lua_State* L_) { lua_pushvalue(L_, nameIdx); }); // L: ... "name" ... |
581 | // keep a direct pointer on the string | 581 | // keep a direct pointer on the string |
582 | debugName = lua_tostring(L, nameIdx_); | 582 | debugName = lua_tostring(L, nameIdx_); |
583 | // to see VM name in Decoda debugger Virtual Machine window | 583 | // to see VM name in Decoda debugger Virtual Machine window |
@@ -594,11 +594,11 @@ void Lane::changeDebugName(int nameIdx_) | |||
594 | LUAG_FUNC(set_debug_threadname) | 594 | LUAG_FUNC(set_debug_threadname) |
595 | { | 595 | { |
596 | // C s_lane structure is a light userdata upvalue | 596 | // C s_lane structure is a light userdata upvalue |
597 | Lane* const lane{ lua_tolightuserdata<Lane>(L_, lua_upvalueindex(1)) }; | 597 | Lane* const _lane{ lua_tolightuserdata<Lane>(L_, lua_upvalueindex(1)) }; |
598 | LUA_ASSERT(L_, L_ == lane->L); // this function is exported in a lane's state, therefore it is callable only from inside the Lane's state | 598 | LUA_ASSERT(L_, L_ == _lane->L); // this function is exported in a lane's state, therefore it is callable only from inside the Lane's state |
599 | lua_settop(L_, 1); | 599 | lua_settop(L_, 1); |
600 | STACK_CHECK_START_REL(L_, 0); | 600 | STACK_CHECK_START_REL(L_, 0); |
601 | lane->changeDebugName(-1); | 601 | _lane->changeDebugName(-1); |
602 | STACK_CHECK(L_, 0); | 602 | STACK_CHECK(L_, 0); |
603 | return 0; | 603 | return 0; |
604 | } | 604 | } |
@@ -607,9 +607,9 @@ LUAG_FUNC(set_debug_threadname) | |||
607 | 607 | ||
608 | LUAG_FUNC(get_debug_threadname) | 608 | LUAG_FUNC(get_debug_threadname) |
609 | { | 609 | { |
610 | Lane* const lane{ ToLane(L_, 1) }; | 610 | Lane* const _lane{ ToLane(L_, 1) }; |
611 | luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments"); | 611 | luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments"); |
612 | lua_pushstring(L_, lane->debugName); | 612 | lua_pushstring(L_, _lane->debugName); |
613 | return 1; | 613 | return 1; |
614 | } | 614 | } |
615 | 615 | ||
@@ -617,14 +617,14 @@ LUAG_FUNC(get_debug_threadname) | |||
617 | 617 | ||
618 | LUAG_FUNC(set_thread_priority) | 618 | LUAG_FUNC(set_thread_priority) |
619 | { | 619 | { |
620 | lua_Integer const prio{ luaL_checkinteger(L_, 1) }; | 620 | lua_Integer const _prio{ luaL_checkinteger(L_, 1) }; |
621 | // public Lanes API accepts a generic range -3/+3 | 621 | // public Lanes API accepts a generic range -3/+3 |
622 | // that will be remapped into the platform-specific scheduler priority scheme | 622 | // that will be remapped into the platform-specific scheduler priority scheme |
623 | // On some platforms, -3 is equivalent to -2 and +3 to +2 | 623 | // On some platforms, -3 is equivalent to -2 and +3 to +2 |
624 | if (prio < kThreadPrioMin || prio > kThreadPrioMax) { | 624 | if (_prio < kThreadPrioMin || _prio > kThreadPrioMax) { |
625 | raise_luaL_error(L_, "priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, prio); | 625 | raise_luaL_error(L_, "priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, _prio); |
626 | } | 626 | } |
627 | THREAD_SET_PRIORITY(static_cast<int>(prio), universe_get(L_)->sudo); | 627 | THREAD_SET_PRIORITY(static_cast<int>(_prio), universe_get(L_)->sudo); |
628 | return 0; | 628 | return 0; |
629 | } | 629 | } |
630 | 630 | ||
@@ -632,11 +632,11 @@ LUAG_FUNC(set_thread_priority) | |||
632 | 632 | ||
633 | LUAG_FUNC(set_thread_affinity) | 633 | LUAG_FUNC(set_thread_affinity) |
634 | { | 634 | { |
635 | lua_Integer const affinity{ luaL_checkinteger(L_, 1) }; | 635 | lua_Integer const _affinity{ luaL_checkinteger(L_, 1) }; |
636 | if (affinity <= 0) { | 636 | if (_affinity <= 0) { |
637 | raise_luaL_error(L_, "invalid affinity (%d)", affinity); | 637 | raise_luaL_error(L_, "invalid affinity (%d)", _affinity); |
638 | } | 638 | } |
639 | THREAD_SET_AFFINITY(static_cast<unsigned int>(affinity)); | 639 | THREAD_SET_AFFINITY(static_cast<unsigned int>(_affinity)); |
640 | return 0; | 640 | return 0; |
641 | } | 641 | } |
642 | 642 | ||
@@ -662,9 +662,9 @@ static struct errcode_name s_errcodes[] = { | |||
662 | }; | 662 | }; |
663 | static char const* get_errcode_name(int _code) | 663 | static char const* get_errcode_name(int _code) |
664 | { | 664 | { |
665 | for (int i{ 0 }; i < 7; ++i) { | 665 | for (errcode_name const& _entry : s_errcodes) { |
666 | if (s_errcodes[i].code == _code) { | 666 | if (_entry.code == _code) { |
667 | return s_errcodes[i].name; | 667 | return _entry.name; |
668 | } | 668 | } |
669 | } | 669 | } |
670 | return "<nullptr>"; | 670 | return "<nullptr>"; |
@@ -675,61 +675,61 @@ static char const* get_errcode_name(int _code) | |||
675 | 675 | ||
676 | static void lane_main(Lane* lane_) | 676 | static void lane_main(Lane* lane_) |
677 | { | 677 | { |
678 | lua_State* const L{ lane_->L }; | 678 | lua_State* const _L{ lane_->L }; |
679 | // wait until the launching thread has finished preparing L | 679 | // wait until the launching thread has finished preparing L |
680 | lane_->ready.wait(); | 680 | lane_->ready.wait(); |
681 | int rc{ LUA_ERRRUN }; | 681 | int _rc{ LUA_ERRRUN }; |
682 | if (lane_->status == Lane::Pending) { // nothing wrong happened during preparation, we can work | 682 | if (lane_->status == Lane::Pending) { // nothing wrong happened during preparation, we can work |
683 | // At this point, the lane function and arguments are on the stack | 683 | // At this point, the lane function and arguments are on the stack |
684 | int const nargs{ lua_gettop(L) - 1 }; | 684 | int const nargs{ lua_gettop(_L) - 1 }; |
685 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); | 685 | DEBUGSPEW_CODE(Universe* U = universe_get(_L)); |
686 | lane_->status = Lane::Running; // Pending -> Running | 686 | lane_->status = Lane::Running; // Pending -> Running |
687 | 687 | ||
688 | // Tie "set_finalizer()" to the state | 688 | // Tie "set_finalizer()" to the state |
689 | lua_pushcfunction(L, LG_set_finalizer); | 689 | lua_pushcfunction(_L, LG_set_finalizer); |
690 | populate_func_lookup_table(L, -1, "set_finalizer"); | 690 | populate_func_lookup_table(_L, -1, "set_finalizer"); |
691 | lua_setglobal(L, "set_finalizer"); | 691 | lua_setglobal(_L, "set_finalizer"); |
692 | 692 | ||
693 | // Tie "set_debug_threadname()" to the state | 693 | // Tie "set_debug_threadname()" to the state |
694 | // But don't register it in the lookup database because of the Lane pointer upvalue | 694 | // But don't register it in the lookup database because of the Lane pointer upvalue |
695 | lua_pushlightuserdata(L, lane_); | 695 | lua_pushlightuserdata(_L, lane_); |
696 | lua_pushcclosure(L, LG_set_debug_threadname, 1); | 696 | lua_pushcclosure(_L, LG_set_debug_threadname, 1); |
697 | lua_setglobal(L, "set_debug_threadname"); | 697 | lua_setglobal(_L, "set_debug_threadname"); |
698 | 698 | ||
699 | // Tie "cancel_test()" to the state | 699 | // Tie "cancel_test()" to the state |
700 | lua_pushcfunction(L, LG_cancel_test); | 700 | lua_pushcfunction(_L, LG_cancel_test); |
701 | populate_func_lookup_table(L, -1, "cancel_test"); | 701 | populate_func_lookup_table(_L, -1, "cancel_test"); |
702 | lua_setglobal(L, "cancel_test"); | 702 | lua_setglobal(_L, "cancel_test"); |
703 | 703 | ||
704 | // this could be done in lane_new before the lane body function is pushed on the stack to avoid unnecessary stack slot shifting around | 704 | // this could be done in lane_new before the lane body function is pushed on the stack to avoid unnecessary stack slot shifting around |
705 | #if ERROR_FULL_STACK | 705 | #if ERROR_FULL_STACK |
706 | // Tie "set_error_reporting()" to the state | 706 | // Tie "set_error_reporting()" to the state |
707 | lua_pushcfunction(L, LG_set_error_reporting); | 707 | lua_pushcfunction(_L, LG_set_error_reporting); |
708 | populate_func_lookup_table(L, -1, "set_error_reporting"); | 708 | populate_func_lookup_table(_L, -1, "set_error_reporting"); |
709 | lua_setglobal(L, "set_error_reporting"); | 709 | lua_setglobal(_L, "set_error_reporting"); |
710 | 710 | ||
711 | STACK_GROW(L, 1); | 711 | STACK_GROW(_L, 1); |
712 | lua_pushcfunction(L, lane_error); // L: func args handler | 712 | lua_pushcfunction(_L, lane_error); // L: func args handler |
713 | lua_insert(L, 1); // L: handler func args | 713 | lua_insert(_L, 1); // L: handler func args |
714 | #endif // L: ERROR_FULL_STACK | 714 | #endif // L: ERROR_FULL_STACK |
715 | 715 | ||
716 | rc = lua_pcall(L, nargs, LUA_MULTRET, ERROR_FULL_STACK); // L: retvals|err | 716 | _rc = lua_pcall(_L, nargs, LUA_MULTRET, ERROR_FULL_STACK); // L: retvals|err |
717 | 717 | ||
718 | #if ERROR_FULL_STACK | 718 | #if ERROR_FULL_STACK |
719 | lua_remove(L, 1); // L: retvals|error | 719 | lua_remove(_L, 1); // L: retvals|error |
720 | #endif // ERROR_FULL_STACK | 720 | #endif // ERROR_FULL_STACK |
721 | 721 | ||
722 | // in case of error and if it exists, fetch stack trace from registry and push it | 722 | // in case of error and if it exists, fetch stack trace from registry and push it |
723 | push_stack_trace(L, rc, 1); // L: retvals|error [trace] | 723 | push_stack_trace(_L, _rc, 1); // L: retvals|error [trace] |
724 | 724 | ||
725 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "Lane %p body: %s (%s)\n" INDENT_END(U), L, get_errcode_name(rc), kCancelError.equals(L, 1) ? "cancelled" : lua_typename(L, lua_type(L, 1)))); | 725 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "Lane %p body: %s (%s)\n" INDENT_END(U), _L, get_errcode_name(_rc), kCancelError.equals(_L, 1) ? "cancelled" : lua_typename(_L, lua_type(_L, 1)))); |
726 | // Call finalizers, if the script has set them up. | 726 | // Call finalizers, if the script has set them up. |
727 | // | 727 | // |
728 | int rc2{ run_finalizers(L, rc) }; | 728 | int _rc2{ run_finalizers(_L, _rc) }; |
729 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "Lane %p finalizer: %s\n" INDENT_END(U), L, get_errcode_name(rc2))); | 729 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "Lane %p finalizer: %s\n" INDENT_END(U), _L, get_errcode_name(_rc2))); |
730 | if (rc2 != LUA_OK) { // Error within a finalizer! | 730 | if (_rc2 != LUA_OK) { // Error within a finalizer! |
731 | // the finalizer generated an error, and left its own error message [and stack trace] on the stack | 731 | // the finalizer generated an error, and left its own error message [and stack trace] on the stack |
732 | rc = rc2; // we're overruling the earlier script error or normal return | 732 | _rc = _rc2; // we're overruling the earlier script error or normal return |
733 | } | 733 | } |
734 | lane_->waiting_on = nullptr; // just in case | 734 | lane_->waiting_on = nullptr; // just in case |
735 | if (selfdestruct_remove(lane_)) { // check and remove (under lock!) | 735 | if (selfdestruct_remove(lane_)) { // check and remove (under lock!) |
@@ -750,12 +750,12 @@ static void lane_main(Lane* lane_) | |||
750 | if (lane_) { | 750 | if (lane_) { |
751 | // leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them | 751 | // leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them |
752 | 752 | ||
753 | Lane::Status const st = (rc == LUA_OK) ? Lane::Done : kCancelError.equals(L, 1) ? Lane::Cancelled : Lane::Error; | 753 | Lane::Status const _st = (_rc == LUA_OK) ? Lane::Done : kCancelError.equals(_L, 1) ? Lane::Cancelled : Lane::Error; |
754 | 754 | ||
755 | { | 755 | { |
756 | // 'doneMutex' protects the -> Done|Error|Cancelled state change | 756 | // 'doneMutex' protects the -> Done|Error|Cancelled state change |
757 | std::lock_guard lock{ lane_->doneMutex }; | 757 | std::lock_guard lock{ lane_->doneMutex }; |
758 | lane_->status = st; | 758 | lane_->status = _st; |
759 | lane_->doneCondVar.notify_one(); // wake up master (while 'lane_->doneMutex' is on) | 759 | lane_->doneCondVar.notify_one(); // wake up master (while 'lane_->doneMutex' is on) |
760 | } | 760 | } |
761 | } | 761 | } |
@@ -769,17 +769,17 @@ static void lane_main(Lane* lane_) | |||
769 | // upvalue[1]: _G.require | 769 | // upvalue[1]: _G.require |
770 | LUAG_FUNC(require) | 770 | LUAG_FUNC(require) |
771 | { | 771 | { |
772 | char const* name = lua_tostring(L_, 1); // L_: "name" ... | 772 | char const* _name{ lua_tostring(L_, 1) }; // L_: "name" ... |
773 | int const nargs{ lua_gettop(L_) }; | 773 | int const _nargs{ lua_gettop(L_) }; |
774 | DEBUGSPEW_CODE(Universe* U = universe_get(L_)); | 774 | DEBUGSPEW_CODE(Universe * _U{ universe_get(L_) }); |
775 | STACK_CHECK_START_REL(L_, 0); | 775 | STACK_CHECK_START_REL(L_, 0); |
776 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END(U), name)); | 776 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END(_U), _name)); |
777 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 777 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); |
778 | lua_pushvalue(L_, lua_upvalueindex(1)); // L_: "name" ... require | 778 | lua_pushvalue(L_, lua_upvalueindex(1)); // L_: "name" ... require |
779 | lua_insert(L_, 1); // L_: require "name" ... | 779 | lua_insert(L_, 1); // L_: require "name" ... |
780 | lua_call(L_, nargs, 1); // L_: module | 780 | lua_call(L_, _nargs, 1); // L_: module |
781 | populate_func_lookup_table(L_, -1, name); | 781 | populate_func_lookup_table(L_, -1, _name); |
782 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s END\n" INDENT_END(U), name)); | 782 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s END\n" INDENT_END(_U), _name)); |
783 | STACK_CHECK(L_, 0); | 783 | STACK_CHECK(L_, 0); |
784 | return 1; | 784 | return 1; |
785 | } | 785 | } |
@@ -791,17 +791,17 @@ LUAG_FUNC(require) | |||
791 | // lanes.register( "modname", module) | 791 | // lanes.register( "modname", module) |
792 | LUAG_FUNC(register) | 792 | LUAG_FUNC(register) |
793 | { | 793 | { |
794 | char const* name = luaL_checkstring(L_, 1); | 794 | char const* _name{ luaL_checkstring(L_, 1) }; |
795 | LuaType const mod_type{ lua_type_as_enum(L_, 2) }; | 795 | LuaType const _mod_type{ lua_type_as_enum(L_, 2) }; |
796 | // ignore extra parameters, just in case | 796 | // ignore extra parameters, just in case |
797 | lua_settop(L_, 2); | 797 | lua_settop(L_, 2); |
798 | luaL_argcheck(L_, (mod_type == LuaType::TABLE) || (mod_type == LuaType::FUNCTION), 2, "unexpected module type"); | 798 | luaL_argcheck(L_, (_mod_type == LuaType::TABLE) || (_mod_type == LuaType::FUNCTION), 2, "unexpected module type"); |
799 | DEBUGSPEW_CODE(Universe* U = universe_get(L_)); | 799 | DEBUGSPEW_CODE(Universe* U = universe_get(L_)); |
800 | STACK_CHECK_START_REL(L_, 0); // "name" mod_table | 800 | STACK_CHECK_START_REL(L_, 0); // "name" mod_table |
801 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END(U), name)); | 801 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END(U), _name)); |
802 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 802 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
803 | populate_func_lookup_table(L_, -1, name); | 803 | populate_func_lookup_table(L_, -1, _name); |
804 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s END\n" INDENT_END(U), name)); | 804 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s END\n" INDENT_END(U), _name)); |
805 | STACK_CHECK(L_, 0); | 805 | STACK_CHECK(L_, 0); |
806 | return 0; | 806 | return 0; |
807 | } | 807 | } |
@@ -827,37 +827,37 @@ static constexpr UniqueKey kLaneGC{ 0x5D6122141727F960ull }; | |||
827 | LUAG_FUNC(lane_new) | 827 | LUAG_FUNC(lane_new) |
828 | { | 828 | { |
829 | // first 8 args: func libs priority globals package required gc_cb name | 829 | // first 8 args: func libs priority globals package required gc_cb name |
830 | char const* const libs_str{ lua_tostring(L_, 2) }; | 830 | char const* const _libs_str{ lua_tostring(L_, 2) }; |
831 | bool const have_priority{ !lua_isnoneornil(L_, 3) }; | 831 | bool const _have_priority{ !lua_isnoneornil(L_, 3) }; |
832 | int const priority{ have_priority ? static_cast<int>(lua_tointeger(L_, 3)) : kThreadPrioDefault }; | 832 | int const _priority{ _have_priority ? static_cast<int>(lua_tointeger(L_, 3)) : kThreadPrioDefault }; |
833 | int const globals_idx{ lua_isnoneornil(L_, 4) ? 0 : 4 }; | 833 | int const _globals_idx{ lua_isnoneornil(L_, 4) ? 0 : 4 }; |
834 | int const package_idx{ lua_isnoneornil(L_, 5) ? 0 : 5 }; | 834 | int const _package_idx{ lua_isnoneornil(L_, 5) ? 0 : 5 }; |
835 | int const required_idx{ lua_isnoneornil(L_, 6) ? 0 : 6 }; | 835 | int const _required_idx{ lua_isnoneornil(L_, 6) ? 0 : 6 }; |
836 | int const gc_cb_idx{ lua_isnoneornil(L_, 7) ? 0 : 7 }; | 836 | int const _gc_cb_idx{ lua_isnoneornil(L_, 7) ? 0 : 7 }; |
837 | int const name_idx{ lua_isnoneornil(L_, 8) ? 0 : 8 }; | 837 | int const _name_idx{ lua_isnoneornil(L_, 8) ? 0 : 8 }; |
838 | 838 | ||
839 | static constexpr int kFixedArgsIdx{ 8 }; | 839 | static constexpr int kFixedArgsIdx{ 8 }; |
840 | int const nargs{ lua_gettop(L_) - kFixedArgsIdx }; | 840 | int const _nargs{ lua_gettop(L_) - kFixedArgsIdx }; |
841 | Universe* const U{ universe_get(L_) }; | 841 | Universe* const _U{ universe_get(L_) }; |
842 | LUA_ASSERT(L_, nargs >= 0); | 842 | LUA_ASSERT(L_, _nargs >= 0); |
843 | 843 | ||
844 | // public Lanes API accepts a generic range -3/+3 | 844 | // public Lanes API accepts a generic range -3/+3 |
845 | // that will be remapped into the platform-specific scheduler priority scheme | 845 | // that will be remapped into the platform-specific scheduler priority scheme |
846 | // On some platforms, -3 is equivalent to -2 and +3 to +2 | 846 | // On some platforms, -3 is equivalent to -2 and +3 to +2 |
847 | if (have_priority && (priority < kThreadPrioMin || priority > kThreadPrioMax)) { | 847 | if (_have_priority && (_priority < kThreadPrioMin || _priority > kThreadPrioMax)) { |
848 | raise_luaL_error(L_, "Priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, priority); | 848 | raise_luaL_error(L_, "Priority out of range: %d..+%d (%d)", kThreadPrioMin, kThreadPrioMax, _priority); |
849 | } | 849 | } |
850 | 850 | ||
851 | /* --- Create and prepare the sub state --- */ | 851 | /* --- Create and prepare the sub state --- */ |
852 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END(U))); | 852 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END(_U))); |
853 | 853 | ||
854 | // populate with selected libraries at the same time. | 854 | // populate with selected libraries at the same time. |
855 | lua_State* const L2{ luaG_newstate(U, SourceState{ L_ }, libs_str) }; // L_: [8 args] ... L2: | 855 | lua_State* const _L2{ luaG_newstate(_U, SourceState{ L_ }, _libs_str) }; // L_: [8 args] ... L2: |
856 | STACK_CHECK_START_REL(L2, 0); | 856 | STACK_CHECK_START_REL(_L2, 0); |
857 | 857 | ||
858 | // 'lane' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread) | 858 | // 'lane' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread) |
859 | Lane* const lane{ new (U) Lane{ U, L2 } }; | 859 | Lane* const _lane{ new (_U) Lane{ _U, _L2 } }; |
860 | if (lane == nullptr) { | 860 | if (_lane == nullptr) { |
861 | raise_luaL_error(L_, "could not create lane: out of memory"); | 861 | raise_luaL_error(L_, "could not create lane: out of memory"); |
862 | } | 862 | } |
863 | 863 | ||
@@ -959,135 +959,135 @@ LUAG_FUNC(lane_new) | |||
959 | m_lane->ready.count_down(); | 959 | m_lane->ready.count_down(); |
960 | m_lane = nullptr; | 960 | m_lane = nullptr; |
961 | } | 961 | } |
962 | } onExit{ L_, lane, gc_cb_idx, name_idx DEBUGSPEW_COMMA_PARAM(U) }; | 962 | } onExit{ L_, _lane, _gc_cb_idx, _name_idx DEBUGSPEW_COMMA_PARAM(_U) }; |
963 | // launch the thread early, it will sync with a std::latch to parallelize OS thread warmup and L2 preparation | 963 | // launch the thread early, it will sync with a std::latch to parallelize OS thread warmup and L2 preparation |
964 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: launching thread\n" INDENT_END(U))); | 964 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: launching thread\n" INDENT_END(_U))); |
965 | lane->startThread(priority); | 965 | _lane->startThread(_priority); |
966 | 966 | ||
967 | STACK_GROW(L2, nargs + 3); | 967 | STACK_GROW(_L2, _nargs + 3); |
968 | STACK_GROW(L_, 3); | 968 | STACK_GROW(L_, 3); |
969 | STACK_CHECK_START_REL(L_, 0); | 969 | STACK_CHECK_START_REL(L_, 0); |
970 | 970 | ||
971 | // package | 971 | // package |
972 | if (package_idx != 0) { | 972 | if (_package_idx != 0) { |
973 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: update 'package'\n" INDENT_END(U))); | 973 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: update 'package'\n" INDENT_END(_U))); |
974 | // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack | 974 | // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack |
975 | InterCopyContext c{ U, DestState{ L2 }, SourceState{ L_ }, {}, SourceIndex{ package_idx }, {}, {}, {} }; | 975 | InterCopyContext c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, SourceIndex{ _package_idx }, {}, {}, {} }; |
976 | [[maybe_unused]] InterCopyResult const ret{ c.inter_copy_package() }; | 976 | [[maybe_unused]] InterCopyResult const ret{ c.inter_copy_package() }; |
977 | LUA_ASSERT(L_, ret == InterCopyResult::Success); // either all went well, or we should not even get here | 977 | LUA_ASSERT(L_, ret == InterCopyResult::Success); // either all went well, or we should not even get here |
978 | } | 978 | } |
979 | 979 | ||
980 | // modules to require in the target lane *before* the function is transfered! | 980 | // modules to require in the target lane *before* the function is transfered! |
981 | if (required_idx != 0) { | 981 | if (_required_idx != 0) { |
982 | int nbRequired = 1; | 982 | int _nbRequired{ 1 }; |
983 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: require 'required' list\n" INDENT_END(U))); | 983 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: require 'required' list\n" INDENT_END(_U))); |
984 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 984 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ _U }); |
985 | // should not happen, was checked in lanes.lua before calling lane_new() | 985 | // should not happen, was checked in lanes.lua before calling lane_new() |
986 | if (lua_type(L_, required_idx) != LUA_TTABLE) { | 986 | if (lua_type(L_, _required_idx) != LUA_TTABLE) { |
987 | raise_luaL_error(L_, "expected required module list as a table, got %s", luaL_typename(L_, required_idx)); | 987 | raise_luaL_error(L_, "expected required module list as a table, got %s", luaL_typename(L_, _required_idx)); |
988 | } | 988 | } |
989 | 989 | ||
990 | lua_pushnil(L_); // L_: [8 args] args... nil L2: | 990 | lua_pushnil(L_); // L_: [8 args] args... nil L2: |
991 | while (lua_next(L_, required_idx) != 0) { // L_: [8 args] args... n "modname" L2: | 991 | while (lua_next(L_, _required_idx) != 0) { // L_: [8 args] args... n "modname" L2: |
992 | if (lua_type(L_, -1) != LUA_TSTRING || lua_type(L_, -2) != LUA_TNUMBER || lua_tonumber(L_, -2) != nbRequired) { | 992 | if (lua_type(L_, -1) != LUA_TSTRING || lua_type(L_, -2) != LUA_TNUMBER || lua_tonumber(L_, -2) != _nbRequired) { |
993 | raise_luaL_error(L_, "required module list should be a list of strings"); | 993 | raise_luaL_error(L_, "required module list should be a list of strings"); |
994 | } else { | 994 | } else { |
995 | // require the module in the target state, and populate the lookup table there too | 995 | // require the module in the target state, and populate the lookup table there too |
996 | size_t len; | 996 | size_t len; |
997 | char const* name = lua_tolstring(L_, -1, &len); | 997 | char const* name = lua_tolstring(L_, -1, &len); |
998 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: require '%s'\n" INDENT_END(U), name)); | 998 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: require '%s'\n" INDENT_END(_U), name)); |
999 | 999 | ||
1000 | // require the module in the target lane | 1000 | // require the module in the target lane |
1001 | lua_getglobal(L2, "require"); // L_: [8 args] args... n "modname" L2: require()? | 1001 | lua_getglobal(_L2, "require"); // L_: [8 args] args... n "modname" L2: require()? |
1002 | if (lua_isnil(L2, -1)) { | 1002 | if (lua_isnil(_L2, -1)) { |
1003 | lua_pop(L2, 1); // L_: [8 args] args... n "modname" L2: | 1003 | lua_pop(_L2, 1); // L_: [8 args] args... n "modname" L2: |
1004 | raise_luaL_error(L_, "cannot pre-require modules without loading 'package' library first"); | 1004 | raise_luaL_error(L_, "cannot pre-require modules without loading 'package' library first"); |
1005 | } else { | 1005 | } else { |
1006 | lua_pushlstring(L2, name, len); // L_: [8 args] args... n "modname" L2: require() name | 1006 | lua_pushlstring(_L2, name, len); // L_: [8 args] args... n "modname" L2: require() name |
1007 | if (lua_pcall(L2, 1, 1, 0) != LUA_OK) { // L_: [8 args] args... n "modname" L2: ret/errcode | 1007 | if (lua_pcall(_L2, 1, 1, 0) != LUA_OK) { // L_: [8 args] args... n "modname" L2: ret/errcode |
1008 | // propagate error to main state if any | 1008 | // propagate error to main state if any |
1009 | InterCopyContext c{ U, DestState{ L_ }, SourceState{ L2 }, {}, {}, {}, {}, {} }; | 1009 | InterCopyContext _c{ _U, DestState{ L_ }, SourceState{ _L2 }, {}, {}, {}, {}, {} }; |
1010 | std::ignore = c.inter_move(1); // L_: [8 args] args... n "modname" error L2: | 1010 | std::ignore = _c.inter_move(1); // L_: [8 args] args... n "modname" error L2: |
1011 | raise_lua_error(L_); | 1011 | raise_lua_error(L_); |
1012 | } | 1012 | } |
1013 | // here the module was successfully required // L_: [8 args] args... n "modname" L2: ret | 1013 | // here the module was successfully required // L_: [8 args] args... n "modname" L2: ret |
1014 | // after requiring the module, register the functions it exported in our name<->function database | 1014 | // after requiring the module, register the functions it exported in our name<->function database |
1015 | populate_func_lookup_table(L2, -1, name); | 1015 | populate_func_lookup_table(_L2, -1, name); |
1016 | lua_pop(L2, 1); // L_: [8 args] args... n "modname" L2: | 1016 | lua_pop(_L2, 1); // L_: [8 args] args... n "modname" L2: |
1017 | } | 1017 | } |
1018 | } | 1018 | } |
1019 | lua_pop(L_, 1); // L_: func libs priority globals package required gc_cb [... args ...] n | 1019 | lua_pop(L_, 1); // L_: func libs priority globals package required gc_cb [... args ...] n |
1020 | ++nbRequired; | 1020 | ++_nbRequired; |
1021 | } // L_: [8 args] args... | 1021 | } // L_: [8 args] args... |
1022 | } | 1022 | } |
1023 | STACK_CHECK(L_, 0); | 1023 | STACK_CHECK(L_, 0); |
1024 | STACK_CHECK(L2, 0); // L_: [8 args] args... L2: | 1024 | STACK_CHECK(_L2, 0); // L_: [8 args] args... L2: |
1025 | 1025 | ||
1026 | // Appending the specified globals to the global environment | 1026 | // Appending the specified globals to the global environment |
1027 | // *after* stdlibs have been loaded and modules required, in case we transfer references to native functions they exposed... | 1027 | // *after* stdlibs have been loaded and modules required, in case we transfer references to native functions they exposed... |
1028 | // | 1028 | // |
1029 | if (globals_idx != 0) { | 1029 | if (_globals_idx != 0) { |
1030 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer globals\n" INDENT_END(U))); | 1030 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer globals\n" INDENT_END(_U))); |
1031 | if (!lua_istable(L_, globals_idx)) { | 1031 | if (!lua_istable(L_, _globals_idx)) { |
1032 | raise_luaL_error(L_, "Expected table, got %s", luaL_typename(L_, globals_idx)); | 1032 | raise_luaL_error(L_, "Expected table, got %s", luaL_typename(L_, _globals_idx)); |
1033 | } | 1033 | } |
1034 | 1034 | ||
1035 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1035 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); |
1036 | lua_pushnil(L_); // L_: [8 args] args... nil L2: | 1036 | lua_pushnil(L_); // L_: [8 args] args... nil L2: |
1037 | // Lua 5.2 wants us to push the globals table on the stack | 1037 | // Lua 5.2 wants us to push the globals table on the stack |
1038 | InterCopyContext c{ U, DestState{ L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; | 1038 | InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; |
1039 | lua_pushglobaltable(L2); // L_: [8 args] args... nil L2: _G | 1039 | lua_pushglobaltable(_L2); // L_: [8 args] args... nil L2: _G |
1040 | while (lua_next(L_, globals_idx)) { // L_: [8 args] args... k v L2: _G | 1040 | while (lua_next(L_, _globals_idx)) { // L_: [8 args] args... k v L2: _G |
1041 | std::ignore = c.inter_copy(2); // L_: [8 args] args... k v L2: _G k v | 1041 | std::ignore = _c.inter_copy(2); // L_: [8 args] args... k v L2: _G k v |
1042 | // assign it in L2's globals table | 1042 | // assign it in L2's globals table |
1043 | lua_rawset(L2, -3); // L_: [8 args] args... k v L2: _G | 1043 | lua_rawset(_L2, -3); // L_: [8 args] args... k v L2: _G |
1044 | lua_pop(L_, 1); // L_: [8 args] args... k | 1044 | lua_pop(L_, 1); // L_: [8 args] args... k |
1045 | } // L_: [8 args] args... | 1045 | } // L_: [8 args] args... |
1046 | lua_pop(L2, 1); // L_: [8 args] args... L2: | 1046 | lua_pop(_L2, 1); // L_: [8 args] args... L2: |
1047 | } | 1047 | } |
1048 | STACK_CHECK(L_, 0); | 1048 | STACK_CHECK(L_, 0); |
1049 | STACK_CHECK(L2, 0); | 1049 | STACK_CHECK(_L2, 0); |
1050 | 1050 | ||
1051 | // Lane main function | 1051 | // Lane main function |
1052 | LuaType const func_type{ lua_type_as_enum(L_, 1) }; | 1052 | LuaType const _func_type{ lua_type_as_enum(L_, 1) }; |
1053 | if (func_type == LuaType::FUNCTION) { | 1053 | if (_func_type == LuaType::FUNCTION) { |
1054 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END(U))); | 1054 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END(_U))); |
1055 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1055 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); |
1056 | lua_pushvalue(L_, 1); // L_: [8 args] args... func L2: | 1056 | lua_pushvalue(L_, 1); // L_: [8 args] args... func L2: |
1057 | InterCopyContext c{ U, DestState{ L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; | 1057 | InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; |
1058 | InterCopyResult const res{ c.inter_move(1) }; // L_: [8 args] args... L2: func | 1058 | InterCopyResult const _res{ _c.inter_move(1) }; // L_: [8 args] args... L2: func |
1059 | if (res != InterCopyResult::Success) { | 1059 | if (_res != InterCopyResult::Success) { |
1060 | raise_luaL_error(L_, "tried to copy unsupported types"); | 1060 | raise_luaL_error(L_, "tried to copy unsupported types"); |
1061 | } | 1061 | } |
1062 | } else if (func_type == LuaType::STRING) { | 1062 | } else if (_func_type == LuaType::STRING) { |
1063 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: compile lane body\n" INDENT_END(U))); | 1063 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: compile lane body\n" INDENT_END(_U))); |
1064 | // compile the string | 1064 | // compile the string |
1065 | if (luaL_loadstring(L2, lua_tostring(L_, 1)) != 0) { // L_: [8 args] args... L2: func | 1065 | if (luaL_loadstring(_L2, lua_tostring(L_, 1)) != 0) { // L_: [8 args] args... L2: func |
1066 | raise_luaL_error(L_, "error when parsing lane function code"); | 1066 | raise_luaL_error(L_, "error when parsing lane function code"); |
1067 | } | 1067 | } |
1068 | } else { | 1068 | } else { |
1069 | raise_luaL_error(L_, "Expected function, got %s", lua_typename(L_, func_type)); | 1069 | raise_luaL_error(L_, "Expected function, got %s", lua_typename(L_, _func_type)); |
1070 | } | 1070 | } |
1071 | STACK_CHECK(L_, 0); | 1071 | STACK_CHECK(L_, 0); |
1072 | STACK_CHECK(L2, 1); | 1072 | STACK_CHECK(_L2, 1); |
1073 | LUA_ASSERT(L_, lua_isfunction(L2, 1)); | 1073 | LUA_ASSERT(L_, lua_isfunction(_L2, 1)); |
1074 | 1074 | ||
1075 | // revive arguments | 1075 | // revive arguments |
1076 | if (nargs > 0) { | 1076 | if (_nargs > 0) { |
1077 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END(U))); | 1077 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END(_U))); |
1078 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1078 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); |
1079 | InterCopyContext c{ U, DestState{ L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; | 1079 | InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; |
1080 | InterCopyResult const res{ c.inter_move(nargs) }; // L_: [8 args] L2: func args... | 1080 | InterCopyResult const res{ _c.inter_move(_nargs) }; // L_: [8 args] L2: func args... |
1081 | if (res != InterCopyResult::Success) { | 1081 | if (res != InterCopyResult::Success) { |
1082 | raise_luaL_error(L_, "tried to copy unsupported types"); | 1082 | raise_luaL_error(L_, "tried to copy unsupported types"); |
1083 | } | 1083 | } |
1084 | } | 1084 | } |
1085 | STACK_CHECK(L_, -nargs); | 1085 | STACK_CHECK(L_, -_nargs); |
1086 | LUA_ASSERT(L_, lua_gettop(L_) == kFixedArgsIdx); | 1086 | LUA_ASSERT(L_, lua_gettop(L_) == kFixedArgsIdx); |
1087 | 1087 | ||
1088 | // Store 'lane' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive). | 1088 | // Store 'lane' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive). |
1089 | kLanePointerRegKey.setValue(L2, [lane](lua_State* L_) { lua_pushlightuserdata(L_, lane); }); // L_: [8 args] L2: func args... | 1089 | kLanePointerRegKey.setValue(_L2, [lane = _lane](lua_State* L_) { lua_pushlightuserdata(L_, lane); });// L_: [8 args] L2: func args... |
1090 | STACK_CHECK(L2, 1 + nargs); | 1090 | STACK_CHECK(_L2, 1 + _nargs); |
1091 | 1091 | ||
1092 | STACK_CHECK_RESET_REL(L_, 0); | 1092 | STACK_CHECK_RESET_REL(L_, 0); |
1093 | // all went well, the lane's thread can start working | 1093 | // all went well, the lane's thread can start working |
@@ -1112,8 +1112,8 @@ LUAG_FUNC(lane_new) | |||
1112 | // | 1112 | // |
1113 | [[nodiscard]] static int lane_gc(lua_State* L_) | 1113 | [[nodiscard]] static int lane_gc(lua_State* L_) |
1114 | { | 1114 | { |
1115 | bool have_gc_cb{ false }; | 1115 | bool _have_gc_cb{ false }; |
1116 | Lane* const lane{ ToLane(L_, 1) }; // L_: ud | 1116 | Lane* const _lane{ ToLane(L_, 1) }; // L_: ud |
1117 | 1117 | ||
1118 | // if there a gc callback? | 1118 | // if there a gc callback? |
1119 | lua_getiuservalue(L_, 1, 1); // L_: ud uservalue | 1119 | lua_getiuservalue(L_, 1, 1); // L_: ud uservalue |
@@ -1121,35 +1121,35 @@ LUAG_FUNC(lane_new) | |||
1121 | lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil | 1121 | lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil |
1122 | if (!lua_isnil(L_, -1)) { | 1122 | if (!lua_isnil(L_, -1)) { |
1123 | lua_remove(L_, -2); // L_: ud gc_cb|nil | 1123 | lua_remove(L_, -2); // L_: ud gc_cb|nil |
1124 | lua_pushstring(L_, lane->debugName); // L_: ud gc_cb name | 1124 | lua_pushstring(L_, _lane->debugName); // L_: ud gc_cb name |
1125 | have_gc_cb = true; | 1125 | _have_gc_cb = true; |
1126 | } else { | 1126 | } else { |
1127 | lua_pop(L_, 2); // L_: ud | 1127 | lua_pop(L_, 2); // L_: ud |
1128 | } | 1128 | } |
1129 | 1129 | ||
1130 | // We can read 'lane->status' without locks, but not wait for it | 1130 | // We can read 'lane->status' without locks, but not wait for it |
1131 | if (lane->status < Lane::Done) { | 1131 | if (_lane->status < Lane::Done) { |
1132 | // still running: will have to be cleaned up later | 1132 | // still running: will have to be cleaned up later |
1133 | selfdestruct_add(lane); | 1133 | selfdestruct_add(_lane); |
1134 | assert(lane->selfdestruct_next); | 1134 | assert(_lane->selfdestruct_next); |
1135 | if (have_gc_cb) { | 1135 | if (_have_gc_cb) { |
1136 | lua_pushliteral(L_, "selfdestruct"); // L_: ud gc_cb name status | 1136 | lua_pushliteral(L_, "selfdestruct"); // L_: ud gc_cb name status |
1137 | lua_call(L_, 2, 0); // L_: ud | 1137 | lua_call(L_, 2, 0); // L_: ud |
1138 | } | 1138 | } |
1139 | return 0; | 1139 | return 0; |
1140 | } else if (lane->L) { | 1140 | } else if (_lane->L) { |
1141 | // no longer accessing the Lua VM: we can close right now | 1141 | // no longer accessing the Lua VM: we can close right now |
1142 | lua_close(lane->L); | 1142 | lua_close(_lane->L); |
1143 | lane->L = nullptr; | 1143 | _lane->L = nullptr; |
1144 | // just in case, but s will be freed soon so... | 1144 | // just in case, but s will be freed soon so... |
1145 | lane->debugName = "<gc>"; | 1145 | _lane->debugName = "<gc>"; |
1146 | } | 1146 | } |
1147 | 1147 | ||
1148 | // Clean up after a (finished) thread | 1148 | // Clean up after a (finished) thread |
1149 | delete lane; | 1149 | delete _lane; |
1150 | 1150 | ||
1151 | // do this after lane cleanup in case the callback triggers an error | 1151 | // do this after lane cleanup in case the callback triggers an error |
1152 | if (have_gc_cb) { | 1152 | if (_have_gc_cb) { |
1153 | lua_pushliteral(L_, "closed"); // L_: ud gc_cb name status | 1153 | lua_pushliteral(L_, "closed"); // L_: ud gc_cb name status |
1154 | lua_call(L_, 2, 0); // L_: ud | 1154 | lua_call(L_, 2, 0); // L_: ud |
1155 | } | 1155 | } |
@@ -1170,7 +1170,7 @@ LUAG_FUNC(lane_new) | |||
1170 | // | 1170 | // |
1171 | [[nodiscard]] static char const* thread_status_string(Lane::Status status_) | 1171 | [[nodiscard]] static char const* thread_status_string(Lane::Status status_) |
1172 | { | 1172 | { |
1173 | char const* const str{ | 1173 | char const* const _str{ |
1174 | (status_ == Lane::Pending) ? "pending" : | 1174 | (status_ == Lane::Pending) ? "pending" : |
1175 | (status_ == Lane::Running) ? "running" : // like in 'co.status()' | 1175 | (status_ == Lane::Running) ? "running" : // like in 'co.status()' |
1176 | (status_ == Lane::Waiting) ? "waiting" : | 1176 | (status_ == Lane::Waiting) ? "waiting" : |
@@ -1179,17 +1179,17 @@ LUAG_FUNC(lane_new) | |||
1179 | (status_ == Lane::Cancelled) ? "cancelled" : | 1179 | (status_ == Lane::Cancelled) ? "cancelled" : |
1180 | nullptr | 1180 | nullptr |
1181 | }; | 1181 | }; |
1182 | return str; | 1182 | return _str; |
1183 | } | 1183 | } |
1184 | 1184 | ||
1185 | // ################################################################################################# | 1185 | // ################################################################################################# |
1186 | 1186 | ||
1187 | void Lane::pushThreadStatus(lua_State* L_) | 1187 | void Lane::pushThreadStatus(lua_State* L_) |
1188 | { | 1188 | { |
1189 | char const* const str{ thread_status_string(status) }; | 1189 | char const* const _str{ thread_status_string(status) }; |
1190 | LUA_ASSERT(L_, str); | 1190 | LUA_ASSERT(L_, _str); |
1191 | 1191 | ||
1192 | lua_pushstring(L_, str); | 1192 | lua_pushstring(L_, _str); |
1193 | } | 1193 | } |
1194 | 1194 | ||
1195 | // ################################################################################################# | 1195 | // ################################################################################################# |
@@ -1204,14 +1204,14 @@ void Lane::pushThreadStatus(lua_State* L_) | |||
1204 | // | 1204 | // |
1205 | LUAG_FUNC(thread_join) | 1205 | LUAG_FUNC(thread_join) |
1206 | { | 1206 | { |
1207 | Lane* const lane{ ToLane(L_, 1) }; | 1207 | Lane* const _lane{ ToLane(L_, 1) }; |
1208 | lua_State* const L2{ lane->L }; | 1208 | lua_State* const _L2{ _lane->L }; |
1209 | 1209 | ||
1210 | std::chrono::time_point<std::chrono::steady_clock> until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; | 1210 | std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; |
1211 | if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion | 1211 | if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion |
1212 | lua_Duration const duration{ lua_tonumber(L_, 2) }; | 1212 | lua_Duration const duration{ lua_tonumber(L_, 2) }; |
1213 | if (duration.count() >= 0.0) { | 1213 | if (duration.count() >= 0.0) { |
1214 | until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration); | 1214 | _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration); |
1215 | } else { | 1215 | } else { |
1216 | raise_luaL_argerror(L_, 2, "duration cannot be < 0"); | 1216 | raise_luaL_argerror(L_, 2, "duration cannot be < 0"); |
1217 | } | 1217 | } |
@@ -1220,9 +1220,9 @@ LUAG_FUNC(thread_join) | |||
1220 | raise_luaL_argerror(L_, 2, "incorrect duration type"); | 1220 | raise_luaL_argerror(L_, 2, "incorrect duration type"); |
1221 | } | 1221 | } |
1222 | 1222 | ||
1223 | bool const done{ !lane->thread.joinable() || lane->waitForCompletion(until) }; | 1223 | bool const done{ !_lane->thread.joinable() || _lane->waitForCompletion(_until) }; |
1224 | lua_settop(L_, 1); // L_: lane | 1224 | lua_settop(L_, 1); // L_: lane |
1225 | if (!done || !L2) { | 1225 | if (!done || !_L2) { |
1226 | lua_pushnil(L_); // L_: lane nil | 1226 | lua_pushnil(L_); // L_: lane nil |
1227 | lua_pushliteral(L_, "timeout"); // L_: lane nil "timeout" | 1227 | lua_pushliteral(L_, "timeout"); // L_: lane nil "timeout" |
1228 | return 2; | 1228 | return 2; |
@@ -1231,51 +1231,51 @@ LUAG_FUNC(thread_join) | |||
1231 | STACK_CHECK_START_REL(L_, 0); // L_: lane | 1231 | STACK_CHECK_START_REL(L_, 0); // L_: lane |
1232 | // Thread is Done/Error/Cancelled; all ours now | 1232 | // Thread is Done/Error/Cancelled; all ours now |
1233 | 1233 | ||
1234 | int ret{ 0 }; | 1234 | int _ret{ 0 }; |
1235 | // debugName is a pointer to string possibly interned in the lane's state, that no longer exists when the state is closed | 1235 | // debugName is a pointer to string possibly interned in the lane's state, that no longer exists when the state is closed |
1236 | // so store it in the userdata uservalue at a key that can't possibly collide | 1236 | // so store it in the userdata uservalue at a key that can't possibly collide |
1237 | lane->securizeDebugName(L_); | 1237 | _lane->securizeDebugName(L_); |
1238 | switch (lane->status) { | 1238 | switch (_lane->status) { |
1239 | case Lane::Done: | 1239 | case Lane::Done: |
1240 | { | 1240 | { |
1241 | int const n{ lua_gettop(L2) }; // whole L2 stack | 1241 | int const _n{ lua_gettop(_L2) }; // whole L2 stack |
1242 | if ( | 1242 | if ( |
1243 | (n > 0) && | 1243 | (_n > 0) && |
1244 | (InterCopyContext{ lane->U, DestState{ L_ }, SourceState{ L2 }, {}, {}, {}, {}, {} }.inter_move(n) != InterCopyResult::Success) | 1244 | (InterCopyContext{ _lane->U, DestState{ L_ }, SourceState{ _L2 }, {}, {}, {}, {}, {} }.inter_move(_n) != InterCopyResult::Success) |
1245 | ) { // L_: lane results L2: | 1245 | ) { // L_: lane results L2: |
1246 | raise_luaL_error(L_, "tried to copy unsupported types"); | 1246 | raise_luaL_error(L_, "tried to copy unsupported types"); |
1247 | } | 1247 | } |
1248 | ret = n; | 1248 | _ret = _n; |
1249 | } | 1249 | } |
1250 | break; | 1250 | break; |
1251 | 1251 | ||
1252 | case Lane::Error: | 1252 | case Lane::Error: |
1253 | { | 1253 | { |
1254 | int const n{ lua_gettop(L2) }; // L_: lane L2: "err" [trace] | 1254 | int const _n{ lua_gettop(_L2) }; // L_: lane L2: "err" [trace] |
1255 | STACK_GROW(L_, 3); | 1255 | STACK_GROW(L_, 3); |
1256 | lua_pushnil(L_); // L_: lane nil | 1256 | lua_pushnil(L_); // L_: lane nil |
1257 | // even when ERROR_FULL_STACK, if the error is not LUA_ERRRUN, the handler wasn't called, and we only have 1 error message on the stack ... | 1257 | // even when ERROR_FULL_STACK, if the error is not LUA_ERRRUN, the handler wasn't called, and we only have 1 error message on the stack ... |
1258 | InterCopyContext c{ lane->U, DestState{ L_ }, SourceState{ L2 }, {}, {}, {}, {}, {} }; | 1258 | InterCopyContext _c{ _lane->U, DestState{ L_ }, SourceState{ _L2 }, {}, {}, {}, {}, {} }; |
1259 | if (c.inter_move(n) != InterCopyResult::Success) { // L_: lane nil "err" [trace] L2: | 1259 | if (_c.inter_move(_n) != InterCopyResult::Success) { // L_: lane nil "err" [trace] L2: |
1260 | raise_luaL_error(L_, "tried to copy unsupported types: %s", lua_tostring(L_, -n)); | 1260 | raise_luaL_error(L_, "tried to copy unsupported types: %s", lua_tostring(L_, -_n)); |
1261 | } | 1261 | } |
1262 | ret = 1 + n; | 1262 | _ret = 1 + _n; |
1263 | } | 1263 | } |
1264 | break; | 1264 | break; |
1265 | 1265 | ||
1266 | case Lane::Cancelled: | 1266 | case Lane::Cancelled: |
1267 | ret = 0; | 1267 | _ret = 0; |
1268 | break; | 1268 | break; |
1269 | 1269 | ||
1270 | default: | 1270 | default: |
1271 | DEBUGSPEW_CODE(fprintf(stderr, "Status: %d\n", lane->status)); | 1271 | DEBUGSPEW_CODE(fprintf(stderr, "Status: %d\n", _lane->status)); |
1272 | LUA_ASSERT(L_, false); | 1272 | LUA_ASSERT(L_, false); |
1273 | ret = 0; | 1273 | _ret = 0; |
1274 | } | 1274 | } |
1275 | lua_close(L2); | 1275 | lua_close(_L2); |
1276 | lane->L = nullptr; | 1276 | _lane->L = nullptr; |
1277 | STACK_CHECK(L_, ret); | 1277 | STACK_CHECK(L_, _ret); |
1278 | return ret; | 1278 | return _ret; |
1279 | } | 1279 | } |
1280 | 1280 | ||
1281 | // ################################################################################################# | 1281 | // ################################################################################################# |
@@ -1291,7 +1291,7 @@ LUAG_FUNC(thread_index) | |||
1291 | { | 1291 | { |
1292 | static constexpr int kSelf{ 1 }; | 1292 | static constexpr int kSelf{ 1 }; |
1293 | static constexpr int kKey{ 2 }; | 1293 | static constexpr int kKey{ 2 }; |
1294 | Lane* const lane{ ToLane(L_, kSelf) }; | 1294 | Lane* const _lane{ ToLane(L_, kSelf) }; |
1295 | LUA_ASSERT(L_, lua_gettop(L_) == 2); | 1295 | LUA_ASSERT(L_, lua_gettop(L_) == 2); |
1296 | 1296 | ||
1297 | STACK_GROW(L_, 8); // up to 8 positions are needed in case of error propagation | 1297 | STACK_GROW(L_, 8); // up to 8 positions are needed in case of error propagation |
@@ -1314,9 +1314,9 @@ LUAG_FUNC(thread_index) | |||
1314 | // check if we already fetched the values from the thread or not | 1314 | // check if we already fetched the values from the thread or not |
1315 | lua_pushinteger(L_, 0); | 1315 | lua_pushinteger(L_, 0); |
1316 | lua_rawget(L_, kUsr); | 1316 | lua_rawget(L_, kUsr); |
1317 | bool const fetched{ !lua_isnil(L_, -1) }; | 1317 | bool const _fetched{ !lua_isnil(L_, -1) }; |
1318 | lua_pop(L_, 1); // back to our 2 args + uservalue on the stack | 1318 | lua_pop(L_, 1); // back to our 2 args + uservalue on the stack |
1319 | if (!fetched) { | 1319 | if (!_fetched) { |
1320 | lua_pushinteger(L_, 0); | 1320 | lua_pushinteger(L_, 0); |
1321 | lua_pushboolean(L_, 1); | 1321 | lua_pushboolean(L_, 1); |
1322 | lua_rawset(L_, kUsr); | 1322 | lua_rawset(L_, kUsr); |
@@ -1324,22 +1324,22 @@ LUAG_FUNC(thread_index) | |||
1324 | lua_pushcfunction(L_, LG_thread_join); | 1324 | lua_pushcfunction(L_, LG_thread_join); |
1325 | lua_pushvalue(L_, kSelf); | 1325 | lua_pushvalue(L_, kSelf); |
1326 | lua_call(L_, 1, LUA_MULTRET); // all return values are on the stack, at slots 4+ | 1326 | lua_call(L_, 1, LUA_MULTRET); // all return values are on the stack, at slots 4+ |
1327 | switch (lane->status) { | 1327 | switch (_lane->status) { |
1328 | default: | 1328 | default: |
1329 | // this is an internal error, we probably never get here | 1329 | // this is an internal error, we probably never get here |
1330 | lua_settop(L_, 0); | 1330 | lua_settop(L_, 0); |
1331 | lua_pushliteral(L_, "Unexpected status: "); | 1331 | lua_pushliteral(L_, "Unexpected status: "); |
1332 | lua_pushstring(L_, thread_status_string(lane->status)); | 1332 | lua_pushstring(L_, thread_status_string(_lane->status)); |
1333 | lua_concat(L_, 2); | 1333 | lua_concat(L_, 2); |
1334 | raise_lua_error(L_); | 1334 | raise_lua_error(L_); |
1335 | [[fallthrough]]; // fall through if we are killed, as we got nil, "killed" on the stack | 1335 | [[fallthrough]]; // fall through if we are killed, as we got nil, "killed" on the stack |
1336 | 1336 | ||
1337 | case Lane::Done: // got regular return values | 1337 | case Lane::Done: // got regular return values |
1338 | { | 1338 | { |
1339 | int const nvalues{ lua_gettop(L_) - 3 }; | 1339 | int const _nvalues{ lua_gettop(L_) - 3 }; |
1340 | for (int i = nvalues; i > 0; --i) { | 1340 | for (int _i = _nvalues; _i > 0; --_i) { |
1341 | // pop the last element of the stack, to store it in the uservalue at its proper index | 1341 | // pop the last element of the stack, to store it in the uservalue at its proper index |
1342 | lua_rawseti(L_, kUsr, i); | 1342 | lua_rawseti(L_, kUsr, _i); |
1343 | } | 1343 | } |
1344 | } | 1344 | } |
1345 | break; | 1345 | break; |
@@ -1361,8 +1361,8 @@ LUAG_FUNC(thread_index) | |||
1361 | } | 1361 | } |
1362 | } | 1362 | } |
1363 | lua_settop(L_, 3); // L_: self KEY ENV | 1363 | lua_settop(L_, 3); // L_: self KEY ENV |
1364 | int const key{ static_cast<int>(lua_tointeger(L_, kKey)) }; | 1364 | int const _key{ static_cast<int>(lua_tointeger(L_, kKey)) }; |
1365 | if (key != -1) { | 1365 | if (_key != -1) { |
1366 | lua_pushnumber(L_, -1); // L_: self KEY ENV -1 | 1366 | lua_pushnumber(L_, -1); // L_: self KEY ENV -1 |
1367 | lua_rawget(L_, kUsr); // L_: self KEY ENV "error"|nil | 1367 | lua_rawget(L_, kUsr); // L_: self KEY ENV "error"|nil |
1368 | if (!lua_isnil(L_, -1)) { // L_: an error was stored | 1368 | if (!lua_isnil(L_, -1)) { // L_: an error was stored |
@@ -1388,15 +1388,15 @@ LUAG_FUNC(thread_index) | |||
1388 | lua_pop(L_, 1); // L_: self KEY ENV | 1388 | lua_pop(L_, 1); // L_: self KEY ENV |
1389 | } | 1389 | } |
1390 | } | 1390 | } |
1391 | lua_rawgeti(L_, kUsr, key); | 1391 | lua_rawgeti(L_, kUsr, _key); |
1392 | } | 1392 | } |
1393 | return 1; | 1393 | return 1; |
1394 | } | 1394 | } |
1395 | if (lua_type(L_, kKey) == LUA_TSTRING) { | 1395 | if (lua_type(L_, kKey) == LUA_TSTRING) { |
1396 | char const* const keystr{ lua_tostring(L_, kKey) }; | 1396 | char const* const _keystr{ lua_tostring(L_, kKey) }; |
1397 | lua_settop(L_, 2); // keep only our original arguments on the stack | 1397 | lua_settop(L_, 2); // keep only our original arguments on the stack |
1398 | if (strcmp(keystr, "status") == 0) { | 1398 | if (strcmp(_keystr, "status") == 0) { |
1399 | lane->pushThreadStatus(L_); // push the string representing the status | 1399 | _lane->pushThreadStatus(L_); // push the string representing the status |
1400 | return 1; | 1400 | return 1; |
1401 | } | 1401 | } |
1402 | // return self.metatable[key] | 1402 | // return self.metatable[key] |
@@ -1405,7 +1405,7 @@ LUAG_FUNC(thread_index) | |||
1405 | lua_rawget(L_, -2); // L_: mt value | 1405 | lua_rawget(L_, -2); // L_: mt value |
1406 | // only "cancel" and "join" are registered as functions, any other string will raise an error | 1406 | // only "cancel" and "join" are registered as functions, any other string will raise an error |
1407 | if (!lua_iscfunction(L_, -1)) { | 1407 | if (!lua_iscfunction(L_, -1)) { |
1408 | raise_luaL_error(L_, "can't index a lane with '%s'", keystr); | 1408 | raise_luaL_error(L_, "can't index a lane with '%s'", _keystr); |
1409 | } | 1409 | } |
1410 | return 1; | 1410 | return 1; |
1411 | } | 1411 | } |
@@ -1428,27 +1428,27 @@ LUAG_FUNC(thread_index) | |||
1428 | // Return a list of all known lanes | 1428 | // Return a list of all known lanes |
1429 | LUAG_FUNC(threads) | 1429 | LUAG_FUNC(threads) |
1430 | { | 1430 | { |
1431 | int const top{ lua_gettop(L_) }; | 1431 | int const _top{ lua_gettop(L_) }; |
1432 | Universe* const U{ universe_get(L_) }; | 1432 | Universe* const _U{ universe_get(L_) }; |
1433 | 1433 | ||
1434 | // List _all_ still running threads | 1434 | // List _all_ still running threads |
1435 | std::lock_guard<std::mutex> guard{ U->trackingMutex }; | 1435 | std::lock_guard<std::mutex> _guard{ _U->trackingMutex }; |
1436 | if (U->trackingFirst && U->trackingFirst != TRACKING_END) { | 1436 | if (_U->trackingFirst && _U->trackingFirst != TRACKING_END) { |
1437 | Lane* lane{ U->trackingFirst }; | 1437 | Lane* _lane{ _U->trackingFirst }; |
1438 | int index{ 0 }; | 1438 | int _index{ 0 }; |
1439 | lua_newtable(L_); // L_: {} | 1439 | lua_newtable(L_); // L_: {} |
1440 | while (lane != TRACKING_END) { | 1440 | while (_lane != TRACKING_END) { |
1441 | // insert a { name='<name>', status='<status>' } tuple, so that several lanes with the same name can't clobber each other | 1441 | // insert a { name='<name>', status='<status>' } tuple, so that several lanes with the same name can't clobber each other |
1442 | lua_createtable(L_, 0, 2); // L_: {} {} | 1442 | lua_createtable(L_, 0, 2); // L_: {} {} |
1443 | lua_pushstring(L_, lane->debugName); // L_: {} {} "name" | 1443 | lua_pushstring(L_, _lane->debugName); // L_: {} {} "name" |
1444 | lua_setfield(L_, -2, "name"); // L_: {} {} | 1444 | lua_setfield(L_, -2, "name"); // L_: {} {} |
1445 | lane->pushThreadStatus(L_); // L_: {} {} "status" | 1445 | _lane->pushThreadStatus(L_); // L_: {} {} "status" |
1446 | lua_setfield(L_, -2, "status"); // L_: {} {} | 1446 | lua_setfield(L_, -2, "status"); // L_: {} {} |
1447 | lua_rawseti(L_, -2, ++index); // L_: {} | 1447 | lua_rawseti(L_, -2, ++_index); // L_: {} |
1448 | lane = lane->tracking_next; | 1448 | _lane = _lane->tracking_next; |
1449 | } | 1449 | } |
1450 | } | 1450 | } |
1451 | return lua_gettop(L_) - top; // L_: 0 or 1 | 1451 | return lua_gettop(L_) - _top; // L_: 0 or 1 |
1452 | } | 1452 | } |
1453 | #endif // HAVE_LANE_TRACKING() | 1453 | #endif // HAVE_LANE_TRACKING() |
1454 | 1454 | ||
@@ -1464,8 +1464,8 @@ LUAG_FUNC(threads) | |||
1464 | */ | 1464 | */ |
1465 | LUAG_FUNC(now_secs) | 1465 | LUAG_FUNC(now_secs) |
1466 | { | 1466 | { |
1467 | auto const now{ std::chrono::system_clock::now() }; | 1467 | auto const _now{ std::chrono::system_clock::now() }; |
1468 | lua_Duration duration{ now.time_since_epoch() }; | 1468 | lua_Duration duration{ _now.time_since_epoch() }; |
1469 | 1469 | ||
1470 | lua_pushnumber(L_, duration.count()); | 1470 | lua_pushnumber(L_, duration.count()); |
1471 | return 1; | 1471 | return 1; |
@@ -1487,38 +1487,38 @@ LUAG_FUNC(wakeup_conv) | |||
1487 | // .isdst (daylight saving on/off) | 1487 | // .isdst (daylight saving on/off) |
1488 | 1488 | ||
1489 | STACK_CHECK_START_REL(L_, 0); | 1489 | STACK_CHECK_START_REL(L_, 0); |
1490 | auto readInteger = [L = L_](char const* name_) { | 1490 | auto _readInteger = [L = L_](char const* name_) { |
1491 | lua_getfield(L, 1, name_); | 1491 | lua_getfield(L, 1, name_); |
1492 | lua_Integer const val{ lua_tointeger(L, -1) }; | 1492 | lua_Integer const val{ lua_tointeger(L, -1) }; |
1493 | lua_pop(L, 1); | 1493 | lua_pop(L, 1); |
1494 | return static_cast<int>(val); | 1494 | return static_cast<int>(val); |
1495 | }; | 1495 | }; |
1496 | int const year{ readInteger("year") }; | 1496 | int const _year{ _readInteger("year") }; |
1497 | int const month{ readInteger("month") }; | 1497 | int const _month{ _readInteger("month") }; |
1498 | int const day{ readInteger("day") }; | 1498 | int const _day{ _readInteger("day") }; |
1499 | int const hour{ readInteger("hour") }; | 1499 | int const _hour{ _readInteger("hour") }; |
1500 | int const min{ readInteger("min") }; | 1500 | int const _min{ _readInteger("min") }; |
1501 | int const sec{ readInteger("sec") }; | 1501 | int const _sec{ _readInteger("sec") }; |
1502 | STACK_CHECK(L_, 0); | 1502 | STACK_CHECK(L_, 0); |
1503 | 1503 | ||
1504 | // If Lua table has '.isdst' we trust that. If it does not, we'll let | 1504 | // If Lua table has '.isdst' we trust that. If it does not, we'll let |
1505 | // 'mktime' decide on whether the time is within DST or not (value -1). | 1505 | // 'mktime' decide on whether the time is within DST or not (value -1). |
1506 | // | 1506 | // |
1507 | lua_getfield(L_, 1, "isdst"); | 1507 | lua_getfield(L_, 1, "isdst"); |
1508 | int const isdst{ lua_isboolean(L_, -1) ? lua_toboolean(L_, -1) : -1 }; | 1508 | int const _isdst{ lua_isboolean(L_, -1) ? lua_toboolean(L_, -1) : -1 }; |
1509 | lua_pop(L_, 1); | 1509 | lua_pop(L_, 1); |
1510 | STACK_CHECK(L_, 0); | 1510 | STACK_CHECK(L_, 0); |
1511 | 1511 | ||
1512 | std::tm t{}; | 1512 | std::tm _t{}; |
1513 | t.tm_year = year - 1900; | 1513 | _t.tm_year = _year - 1900; |
1514 | t.tm_mon = month - 1; // 0..11 | 1514 | _t.tm_mon = _month - 1; // 0..11 |
1515 | t.tm_mday = day; // 1..31 | 1515 | _t.tm_mday = _day; // 1..31 |
1516 | t.tm_hour = hour; // 0..23 | 1516 | _t.tm_hour = _hour; // 0..23 |
1517 | t.tm_min = min; // 0..59 | 1517 | _t.tm_min = _min; // 0..59 |
1518 | t.tm_sec = sec; // 0..60 | 1518 | _t.tm_sec = _sec; // 0..60 |
1519 | t.tm_isdst = isdst; // 0/1/negative | 1519 | _t.tm_isdst = _isdst; // 0/1/negative |
1520 | 1520 | ||
1521 | lua_pushnumber(L_, static_cast<lua_Number>(std::mktime(&t))); // resolution: 1 second | 1521 | lua_pushnumber(L_, static_cast<lua_Number>(std::mktime(&_t))); // resolution: 1 second |
1522 | return 1; | 1522 | return 1; |
1523 | } | 1523 | } |
1524 | 1524 | ||
@@ -1527,7 +1527,7 @@ LUAG_FUNC(wakeup_conv) | |||
1527 | // ################################################################################################# | 1527 | // ################################################################################################# |
1528 | 1528 | ||
1529 | // same as PUC-Lua l_alloc | 1529 | // same as PUC-Lua l_alloc |
1530 | extern "C" [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud, [[maybe_unused]] void* ptr_, [[maybe_unused]] size_t osize_, size_t nsize_) | 1530 | extern "C" [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud_, [[maybe_unused]] void* ptr_, [[maybe_unused]] size_t osize_, size_t nsize_) |
1531 | { | 1531 | { |
1532 | if (nsize_ == 0) { | 1532 | if (nsize_ == 0) { |
1533 | free(ptr_); | 1533 | free(ptr_); |
@@ -1541,9 +1541,9 @@ extern "C" [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud, | |||
1541 | 1541 | ||
1542 | [[nodiscard]] static int luaG_provide_protected_allocator(lua_State* L_) | 1542 | [[nodiscard]] static int luaG_provide_protected_allocator(lua_State* L_) |
1543 | { | 1543 | { |
1544 | Universe* const U{ universe_get(L_) }; | 1544 | Universe* const _U{ universe_get(L_) }; |
1545 | // push a new full userdata on the stack, giving access to the universe's protected allocator | 1545 | // push a new full userdata on the stack, giving access to the universe's protected allocator |
1546 | [[maybe_unused]] AllocatorDefinition* const def{ new (L_) AllocatorDefinition{ U->protectedAllocator.makeDefinition() } }; | 1546 | [[maybe_unused]] AllocatorDefinition* const def{ new (L_) AllocatorDefinition{ _U->protectedAllocator.makeDefinition() } }; |
1547 | return 1; | 1547 | return 1; |
1548 | } | 1548 | } |
1549 | 1549 | ||
@@ -1585,8 +1585,8 @@ static void initialize_allocator_function(Universe* U_, lua_State* L_) | |||
1585 | 1585 | ||
1586 | lua_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" | 1586 | lua_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" |
1587 | { | 1587 | { |
1588 | char const* allocator = lua_tostring(L_, -1); | 1588 | char const* const _allocator{ lua_tostring(L_, -1) }; |
1589 | if (strcmp(allocator, "libc") == 0) { | 1589 | if (strcmp(_allocator, "libc") == 0) { |
1590 | U_->internalAllocator = AllocatorDefinition{ libc_lua_Alloc, nullptr }; | 1590 | U_->internalAllocator = AllocatorDefinition{ libc_lua_Alloc, nullptr }; |
1591 | } else if (U_->provideAllocator == luaG_provide_protected_allocator) { | 1591 | } else if (U_->provideAllocator == luaG_provide_protected_allocator) { |
1592 | // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case. | 1592 | // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case. |
@@ -1639,20 +1639,20 @@ LUAG_FUNC(configure) | |||
1639 | }); | 1639 | }); |
1640 | } | 1640 | } |
1641 | 1641 | ||
1642 | Universe* U = universe_get(L_); | 1642 | Universe* _U{ universe_get(L_) }; |
1643 | bool const from_master_state{ U == nullptr }; | 1643 | bool const _from_master_state{ _U == nullptr }; |
1644 | char const* name = luaL_checkstring(L_, lua_upvalueindex(1)); | 1644 | char const* const _name{ luaL_checkstring(L_, lua_upvalueindex(1)) }; |
1645 | LUA_ASSERT(L_, lua_type(L_, 1) == LUA_TTABLE); | 1645 | LUA_ASSERT(L_, lua_type(L_, 1) == LUA_TTABLE); |
1646 | 1646 | ||
1647 | STACK_GROW(L_, 4); | 1647 | STACK_GROW(L_, 4); |
1648 | STACK_CHECK_START_ABS(L_, 1); // L_: settings | 1648 | STACK_CHECK_START_ABS(L_, 1); // L_: settings |
1649 | 1649 | ||
1650 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END(U), L_)); | 1650 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END(_U), L_)); |
1651 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); | 1651 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ _U }); |
1652 | 1652 | ||
1653 | if (U == nullptr) { | 1653 | if (_U == nullptr) { |
1654 | U = universe_create(L_); // L_: settings universe | 1654 | _U = universe_create(L_); // L_: settings universe |
1655 | DEBUGSPEW_CODE(DebugSpewIndentScope scope2{ U }); | 1655 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ _U }); |
1656 | lua_createtable(L_, 0, 1); // L_: settings universe {mt} | 1656 | lua_createtable(L_, 0, 1); // L_: settings universe {mt} |
1657 | lua_getfield(L_, 1, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout | 1657 | lua_getfield(L_, 1, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout |
1658 | lua_getfield(L_, 1, "shutdown_mode"); // L_: settings universe {mt} shutdown_timeout shutdown_mode | 1658 | lua_getfield(L_, 1, "shutdown_mode"); // L_: settings universe {mt} shutdown_timeout shutdown_mode |
@@ -1661,21 +1661,21 @@ LUAG_FUNC(configure) | |||
1661 | lua_setmetatable(L_, -2); // L_: settings universe | 1661 | lua_setmetatable(L_, -2); // L_: settings universe |
1662 | lua_pop(L_, 1); // L_: settings | 1662 | lua_pop(L_, 1); // L_: settings |
1663 | lua_getfield(L_, 1, "verbose_errors"); // L_: settings verbose_errors | 1663 | lua_getfield(L_, 1, "verbose_errors"); // L_: settings verbose_errors |
1664 | U->verboseErrors = lua_toboolean(L_, -1) ? true : false; | 1664 | _U->verboseErrors = lua_toboolean(L_, -1) ? true : false; |
1665 | lua_pop(L_, 1); // L_: settings | 1665 | lua_pop(L_, 1); // L_: settings |
1666 | lua_getfield(L_, 1, "demote_full_userdata"); // L_: settings demote_full_userdata | 1666 | lua_getfield(L_, 1, "demote_full_userdata"); // L_: settings demote_full_userdata |
1667 | U->demoteFullUserdata = lua_toboolean(L_, -1) ? true : false; | 1667 | _U->demoteFullUserdata = lua_toboolean(L_, -1) ? true : false; |
1668 | lua_pop(L_, 1); // L_: settings | 1668 | lua_pop(L_, 1); // L_: settings |
1669 | #if HAVE_LANE_TRACKING() | 1669 | #if HAVE_LANE_TRACKING() |
1670 | lua_getfield(L_, 1, "track_lanes"); // L_: settings track_lanes | 1670 | lua_getfield(L_, 1, "track_lanes"); // L_: settings track_lanes |
1671 | U->trackingFirst = lua_toboolean(L_, -1) ? TRACKING_END : nullptr; | 1671 | _U->trackingFirst = lua_toboolean(L_, -1) ? TRACKING_END : nullptr; |
1672 | lua_pop(L_, 1); // L_: settings | 1672 | lua_pop(L_, 1); // L_: settings |
1673 | #endif // HAVE_LANE_TRACKING() | 1673 | #endif // HAVE_LANE_TRACKING() |
1674 | // Linked chains handling | 1674 | // Linked chains handling |
1675 | U->selfdestructFirst = SELFDESTRUCT_END; | 1675 | _U->selfdestructFirst = SELFDESTRUCT_END; |
1676 | initialize_allocator_function(U, L_); | 1676 | initialize_allocator_function(_U, L_); |
1677 | initializeOnStateCreate(U, L_); | 1677 | initializeOnStateCreate(_U, L_); |
1678 | init_keepers(U, L_); | 1678 | init_keepers(_U, L_); |
1679 | STACK_CHECK(L_, 1); | 1679 | STACK_CHECK(L_, 1); |
1680 | 1680 | ||
1681 | // Initialize 'timerLinda'; a common Linda object shared by all states | 1681 | // Initialize 'timerLinda'; a common Linda object shared by all states |
@@ -1685,15 +1685,15 @@ LUAG_FUNC(configure) | |||
1685 | STACK_CHECK(L_, 2); | 1685 | STACK_CHECK(L_, 2); |
1686 | 1686 | ||
1687 | // Proxy userdata contents is only a 'DeepPrelude*' pointer | 1687 | // Proxy userdata contents is only a 'DeepPrelude*' pointer |
1688 | U->timerLinda = *lua_tofulluserdata<DeepPrelude*>(L_, -1); | 1688 | _U->timerLinda = *lua_tofulluserdata<DeepPrelude*>(L_, -1); |
1689 | // increment refcount so that this linda remains alive as long as the universe exists. | 1689 | // increment refcount so that this linda remains alive as long as the universe exists. |
1690 | U->timerLinda->refcount.fetch_add(1, std::memory_order_relaxed); | 1690 | _U->timerLinda->refcount.fetch_add(1, std::memory_order_relaxed); |
1691 | lua_pop(L_, 1); // L_: settings | 1691 | lua_pop(L_, 1); // L_: settings |
1692 | } | 1692 | } |
1693 | STACK_CHECK(L_, 1); | 1693 | STACK_CHECK(L_, 1); |
1694 | 1694 | ||
1695 | // Serialize calls to 'require' from now on, also in the primary state | 1695 | // Serialize calls to 'require' from now on, also in the primary state |
1696 | serialize_require(DEBUGSPEW_PARAM_COMMA(U) L_); | 1696 | serialize_require(DEBUGSPEW_PARAM_COMMA(_U) L_); |
1697 | 1697 | ||
1698 | // Retrieve main module interface table | 1698 | // Retrieve main module interface table |
1699 | lua_pushvalue(L_, lua_upvalueindex(2)); // L_: settings M | 1699 | lua_pushvalue(L_, lua_upvalueindex(2)); // L_: settings M |
@@ -1704,7 +1704,7 @@ LUAG_FUNC(configure) | |||
1704 | luaG_registerlibfuncs(L_, global::sLanesFunctions); | 1704 | luaG_registerlibfuncs(L_, global::sLanesFunctions); |
1705 | #if HAVE_LANE_TRACKING() | 1705 | #if HAVE_LANE_TRACKING() |
1706 | // register core.threads() only if settings say it should be available | 1706 | // register core.threads() only if settings say it should be available |
1707 | if (U->trackingFirst != nullptr) { | 1707 | if (_U->trackingFirst != nullptr) { |
1708 | lua_pushcfunction(L_, LG_threads); // L_: settings M LG_threads() | 1708 | lua_pushcfunction(L_, LG_threads); // L_: settings M LG_threads() |
1709 | lua_setfield(L_, -2, "threads"); // L_: settings M | 1709 | lua_setfield(L_, -2, "threads"); // L_: settings M |
1710 | } | 1710 | } |
@@ -1712,11 +1712,11 @@ LUAG_FUNC(configure) | |||
1712 | STACK_CHECK(L_, 2); | 1712 | STACK_CHECK(L_, 2); |
1713 | 1713 | ||
1714 | { | 1714 | { |
1715 | char const* errmsg{ | 1715 | char const* _errmsg{ |
1716 | DeepFactory::PushDeepProxy(DestState{ L_ }, U->timerLinda, 0, LookupMode::LaneBody) | 1716 | DeepFactory::PushDeepProxy(DestState{ L_ }, _U->timerLinda, 0, LookupMode::LaneBody) |
1717 | }; // L_: settings M timerLinda | 1717 | }; // L_: settings M timerLinda |
1718 | if (errmsg != nullptr) { | 1718 | if (_errmsg != nullptr) { |
1719 | raise_luaL_error(L_, errmsg); | 1719 | raise_luaL_error(L_, _errmsg); |
1720 | } | 1720 | } |
1721 | lua_setfield(L_, -2, "timer_gateway"); // L_: settings M | 1721 | lua_setfield(L_, -2, "timer_gateway"); // L_: settings M |
1722 | } | 1722 | } |
@@ -1780,12 +1780,12 @@ LUAG_FUNC(configure) | |||
1780 | // register all native functions found in that module in the transferable functions database | 1780 | // register all native functions found in that module in the transferable functions database |
1781 | // we process it before _G because we don't want to find the module when scanning _G (this would generate longer names) | 1781 | // we process it before _G because we don't want to find the module when scanning _G (this would generate longer names) |
1782 | // for example in package.loaded["lanes.core"].* | 1782 | // for example in package.loaded["lanes.core"].* |
1783 | populate_func_lookup_table(L_, -1, name); | 1783 | populate_func_lookup_table(L_, -1, _name); |
1784 | STACK_CHECK(L_, 2); | 1784 | STACK_CHECK(L_, 2); |
1785 | 1785 | ||
1786 | // record all existing C/JIT-fast functions | 1786 | // record all existing C/JIT-fast functions |
1787 | // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack | 1787 | // Lua 5.2 no longer has LUA_GLOBALSINDEX: we must push globals table on the stack |
1788 | if (from_master_state) { | 1788 | if (_from_master_state) { |
1789 | // don't do this when called during the initialization of a new lane, | 1789 | // don't do this when called during the initialization of a new lane, |
1790 | // because we will do it after on_state_create() is called, | 1790 | // because we will do it after on_state_create() is called, |
1791 | // and we don't want to skip _G because of caching in case globals are created then | 1791 | // and we don't want to skip _G because of caching in case globals are created then |
@@ -1798,7 +1798,7 @@ LUAG_FUNC(configure) | |||
1798 | // set _R[kConfigRegKey] = settings | 1798 | // set _R[kConfigRegKey] = settings |
1799 | kConfigRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); | 1799 | kConfigRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); |
1800 | STACK_CHECK(L_, 1); | 1800 | STACK_CHECK(L_, 1); |
1801 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END(U), L_)); | 1801 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END(_U), L_)); |
1802 | // Return the settings table | 1802 | // Return the settings table |
1803 | return 1; | 1803 | return 1; |
1804 | } | 1804 | } |
@@ -1809,9 +1809,9 @@ LUAG_FUNC(configure) | |||
1809 | #include <signal.h> | 1809 | #include <signal.h> |
1810 | #include <conio.h> | 1810 | #include <conio.h> |
1811 | 1811 | ||
1812 | void signal_handler(int signal) | 1812 | void signal_handler(int signal_) |
1813 | { | 1813 | { |
1814 | if (signal == SIGABRT) { | 1814 | if (signal_ == SIGABRT) { |
1815 | _cprintf("caught abnormal termination!"); | 1815 | _cprintf("caught abnormal termination!"); |
1816 | abort(); | 1816 | abort(); |
1817 | } | 1817 | } |
@@ -1830,18 +1830,18 @@ static void EnableCrashingOnCrashes(void) | |||
1830 | typedef BOOL(WINAPI * tSetPolicy)(DWORD dwFlags); | 1830 | typedef BOOL(WINAPI * tSetPolicy)(DWORD dwFlags); |
1831 | const DWORD EXCEPTION_SWALLOWING = 0x1; | 1831 | const DWORD EXCEPTION_SWALLOWING = 0x1; |
1832 | 1832 | ||
1833 | HMODULE kernel32 = LoadLibraryA("kernel32.dll"); | 1833 | HMODULE _kernel32 = LoadLibraryA("kernel32.dll"); |
1834 | if (kernel32) { | 1834 | if (_kernel32) { |
1835 | tGetPolicy pGetPolicy = (tGetPolicy) GetProcAddress(kernel32, "GetProcessUserModeExceptionPolicy"); | 1835 | tGetPolicy pGetPolicy = (tGetPolicy) GetProcAddress(_kernel32, "GetProcessUserModeExceptionPolicy"); |
1836 | tSetPolicy pSetPolicy = (tSetPolicy) GetProcAddress(kernel32, "SetProcessUserModeExceptionPolicy"); | 1836 | tSetPolicy pSetPolicy = (tSetPolicy) GetProcAddress(_kernel32, "SetProcessUserModeExceptionPolicy"); |
1837 | if (pGetPolicy && pSetPolicy) { | 1837 | if (pGetPolicy && pSetPolicy) { |
1838 | DWORD dwFlags; | 1838 | DWORD _dwFlags; |
1839 | if (pGetPolicy(&dwFlags)) { | 1839 | if (pGetPolicy(&_dwFlags)) { |
1840 | // Turn off the filter | 1840 | // Turn off the filter |
1841 | pSetPolicy(dwFlags & ~EXCEPTION_SWALLOWING); | 1841 | pSetPolicy(_dwFlags & ~EXCEPTION_SWALLOWING); |
1842 | } | 1842 | } |
1843 | } | 1843 | } |
1844 | FreeLibrary(kernel32); | 1844 | FreeLibrary(_kernel32); |
1845 | } | 1845 | } |
1846 | // typedef void (* SignalHandlerPointer)( int); | 1846 | // typedef void (* SignalHandlerPointer)( int); |
1847 | /*SignalHandlerPointer previousHandler =*/signal(SIGABRT, signal_handler); | 1847 | /*SignalHandlerPointer previousHandler =*/signal(SIGABRT, signal_handler); |
@@ -1902,8 +1902,8 @@ LANES_API int luaopen_lanes_core(lua_State* L_) | |||
1902 | 1902 | ||
1903 | [[nodiscard]] static int default_luaopen_lanes(lua_State* L_) | 1903 | [[nodiscard]] static int default_luaopen_lanes(lua_State* L_) |
1904 | { | 1904 | { |
1905 | int const rc{ luaL_loadfile(L_, "lanes.lua") || lua_pcall(L_, 0, 1, 0) }; | 1905 | int const _rc{ luaL_loadfile(L_, "lanes.lua") || lua_pcall(L_, 0, 1, 0) }; |
1906 | if (rc != LUA_OK) { | 1906 | if (_rc != LUA_OK) { |
1907 | raise_luaL_error(L_, "failed to initialize embedded Lanes"); | 1907 | raise_luaL_error(L_, "failed to initialize embedded Lanes"); |
1908 | } | 1908 | } |
1909 | return 1; | 1909 | return 1; |