diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-07-30 15:45:32 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-07-30 15:45:32 +0200 |
commit | 6d9c547732506f0a8b006754bb9709699b05e6af (patch) | |
tree | 3e76196c2011db4d80cde528613f3533430309d8 /src/lane.cpp | |
parent | bb11006f635a69dd9244e09e4359ed02eff8fe84 (diff) | |
download | lanes-6d9c547732506f0a8b006754bb9709699b05e6af.tar.gz lanes-6d9c547732506f0a8b006754bb9709699b05e6af.tar.bz2 lanes-6d9c547732506f0a8b006754bb9709699b05e6af.zip |
Code boyscouting
Diffstat (limited to 'src/lane.cpp')
-rw-r--r-- | src/lane.cpp | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/src/lane.cpp b/src/lane.cpp index e38c4bb..baba0fa 100644 --- a/src/lane.cpp +++ b/src/lane.cpp | |||
@@ -480,7 +480,7 @@ int Lane::LuaErrorHandler(lua_State* L_) | |||
480 | // ########################################## Finalizer ############################################ | 480 | // ########################################## Finalizer ############################################ |
481 | // ################################################################################################# | 481 | // ################################################################################################# |
482 | 482 | ||
483 | static [[nodiscard]] int push_stack_trace(lua_State* const L_, Lane::ErrorTraceLevel const errorTraceLevel_, LuaError const rc_, [[maybe_unused]] int const stk_base_) | 483 | [[nodiscard]] static int PushStackTrace(lua_State* const L_, Lane::ErrorTraceLevel const errorTraceLevel_, LuaError const rc_, [[maybe_unused]] int const stk_base_) |
484 | { | 484 | { |
485 | // Lua 5.1 error handler is limited to one return value; it stored the stack trace in the registry | 485 | // Lua 5.1 error handler is limited to one return value; it stored the stack trace in the registry |
486 | int const _top{ lua_gettop(L_) }; | 486 | int const _top{ lua_gettop(L_) }; |
@@ -532,7 +532,7 @@ static [[nodiscard]] int push_stack_trace(lua_State* const L_, Lane::ErrorTraceL | |||
532 | // TBD: should we add stack trace on failing finalizer, wouldn't be hard.. | 532 | // TBD: should we add stack trace on failing finalizer, wouldn't be hard.. |
533 | // | 533 | // |
534 | 534 | ||
535 | [[nodiscard]] static LuaError run_finalizers(Lane* const lane_, Lane::ErrorTraceLevel errorTraceLevel_, LuaError lua_rc_) | 535 | [[nodiscard]] static LuaError run_finalizers(Lane* const lane_, Lane::ErrorTraceLevel const errorTraceLevel_, LuaError const lua_rc_) |
536 | { | 536 | { |
537 | // if we are a coroutine, we can't run the finalizers in the coroutine state! | 537 | // if we are a coroutine, we can't run the finalizers in the coroutine state! |
538 | lua_State* const _L{ lane_->S }; | 538 | lua_State* const _L{ lane_->S }; |
@@ -581,7 +581,7 @@ static [[nodiscard]] int push_stack_trace(lua_State* const L_, Lane::ErrorTraceL | |||
581 | // if no error from the main body, finalizer doesn't receive any argument, else it gets the error message and optional stack trace | 581 | // if no error from the main body, finalizer doesn't receive any argument, else it gets the error message and optional stack trace |
582 | _rc = ToLuaError(lua_pcall(_L, _args, 0, _error_handler)); // _L: ... finalizers error_handler() err_msg2? | 582 | _rc = ToLuaError(lua_pcall(_L, _args, 0, _error_handler)); // _L: ... finalizers error_handler() err_msg2? |
583 | if (_rc != LuaError::OK) { | 583 | if (_rc != LuaError::OK) { |
584 | _finalizer_pushed = 1 + push_stack_trace(_L, errorTraceLevel_, _rc, lua_gettop(_L)); // _L: ... finalizers error_handler() err_msg2? trace | 584 | _finalizer_pushed = 1 + PushStackTrace(_L, errorTraceLevel_, _rc, lua_gettop(_L)); // _L: ... finalizers error_handler() err_msg2? trace |
585 | // If one finalizer fails, don't run the others. Return this | 585 | // If one finalizer fails, don't run the others. Return this |
586 | // as the 'real' error, replacing what we could have had (or not) | 586 | // as the 'real' error, replacing what we could have had (or not) |
587 | // from the actual code. | 587 | // from the actual code. |
@@ -622,35 +622,35 @@ static [[nodiscard]] int push_stack_trace(lua_State* const L_, Lane::ErrorTraceL | |||
622 | * Add the lane to selfdestruct chain; the ones still running at the end of the | 622 | * Add the lane to selfdestruct chain; the ones still running at the end of the |
623 | * whole process will be cancelled. | 623 | * whole process will be cancelled. |
624 | */ | 624 | */ |
625 | static void selfdestruct_add(Lane* lane_) | 625 | void Lane::selfdestructAdd() |
626 | { | 626 | { |
627 | std::lock_guard<std::mutex> _guard{ lane_->U->selfdestructMutex }; | 627 | std::lock_guard<std::mutex> _guard{ U->selfdestructMutex }; |
628 | assert(lane_->selfdestruct_next == nullptr); | 628 | assert(selfdestruct_next == nullptr); |
629 | 629 | ||
630 | lane_->selfdestruct_next = lane_->U->selfdestructFirst; | 630 | selfdestruct_next = U->selfdestructFirst; |
631 | lane_->U->selfdestructFirst = lane_; | 631 | U->selfdestructFirst = this; |
632 | } | 632 | } |
633 | 633 | ||
634 | // ################################################################################################# | 634 | // ################################################################################################# |
635 | 635 | ||
636 | // A free-running lane has ended; remove it from selfdestruct chain | 636 | // A free-running lane has ended; remove it from selfdestruct chain |
637 | [[nodiscard]] static bool selfdestruct_remove(Lane* lane_) | 637 | [[nodiscard]] bool Lane::selfdestructRemove() |
638 | { | 638 | { |
639 | bool _found{ false }; | 639 | bool _found{ false }; |
640 | std::lock_guard<std::mutex> _guard{ lane_->U->selfdestructMutex }; | 640 | std::lock_guard<std::mutex> _guard{ U->selfdestructMutex }; |
641 | // Make sure (within the MUTEX) that we actually are in the chain | 641 | // Make sure (within the MUTEX) that we actually are in the chain |
642 | // still (at process exit they will remove us from chain and then | 642 | // still (at process exit they will remove us from chain and then |
643 | // cancel/kill). | 643 | // cancel/kill). |
644 | // | 644 | // |
645 | if (lane_->selfdestruct_next != nullptr) { | 645 | if (selfdestruct_next != nullptr) { |
646 | Lane* volatile* _ref = static_cast<Lane* volatile*>(&lane_->U->selfdestructFirst); | 646 | Lane* volatile* _ref = static_cast<Lane* volatile*>(&U->selfdestructFirst); |
647 | 647 | ||
648 | while (*_ref != SELFDESTRUCT_END) { | 648 | while (*_ref != SELFDESTRUCT_END) { |
649 | if (*_ref == lane_) { | 649 | if (*_ref == this) { |
650 | *_ref = lane_->selfdestruct_next; | 650 | *_ref = selfdestruct_next; |
651 | lane_->selfdestruct_next = nullptr; | 651 | selfdestruct_next = nullptr; |
652 | // the terminal shutdown should wait until the lane is done with its lua_close() | 652 | // the terminal shutdown should wait until the lane is done with its lua_close() |
653 | lane_->U->selfdestructingCount.fetch_add(1, std::memory_order_release); | 653 | U->selfdestructingCount.fetch_add(1, std::memory_order_release); |
654 | _found = true; | 654 | _found = true; |
655 | break; | 655 | break; |
656 | } | 656 | } |
@@ -665,7 +665,7 @@ static void selfdestruct_add(Lane* lane_) | |||
665 | // ########################################## Main ################################################# | 665 | // ########################################## Main ################################################# |
666 | // ################################################################################################# | 666 | // ################################################################################################# |
667 | 667 | ||
668 | static void PrepareLaneHelpers(Lane* lane_) | 668 | static void PrepareLaneHelpers(Lane* const lane_) |
669 | { | 669 | { |
670 | lua_State* const _L{ lane_->L }; | 670 | lua_State* const _L{ lane_->L }; |
671 | // Tie "set_finalizer()" to the state | 671 | // Tie "set_finalizer()" to the state |
@@ -752,7 +752,7 @@ static void lane_main(Lane* const lane_) | |||
752 | } | 752 | } |
753 | 753 | ||
754 | // in case of error and if it exists, fetch stack trace from registry and push it | 754 | // in case of error and if it exists, fetch stack trace from registry and push it |
755 | lane_->nresults += push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] | 755 | lane_->nresults += PushStackTrace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] |
756 | 756 | ||
757 | DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, 1)) << ")" << std::endl); | 757 | DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, 1)) << ")" << std::endl); |
758 | // Call finalizers, if the script has set them up. | 758 | // Call finalizers, if the script has set them up. |
@@ -765,7 +765,7 @@ static void lane_main(Lane* const lane_) | |||
765 | _rc = _rc2; // we're overruling the earlier script error or normal return | 765 | _rc = _rc2; // we're overruling the earlier script error or normal return |
766 | } | 766 | } |
767 | lane_->waiting_on = nullptr; // just in case | 767 | lane_->waiting_on = nullptr; // just in case |
768 | if (selfdestruct_remove(lane_)) { // check and remove (under lock!) | 768 | if (lane_->selfdestructRemove()) { // check and remove (under lock!) |
769 | // We're a free-running thread and no-one is there to clean us up. | 769 | // We're a free-running thread and no-one is there to clean us up. |
770 | lane_->closeState(); | 770 | lane_->closeState(); |
771 | lane_->U->selfdestructMutex.lock(); | 771 | lane_->U->selfdestructMutex.lock(); |
@@ -809,7 +809,7 @@ static LUAG_FUNC(lane_close) | |||
809 | 809 | ||
810 | // ################################################################################################# | 810 | // ################################################################################################# |
811 | 811 | ||
812 | // = thread_gc( lane_ud ) | 812 | // = lane_gc( lane_ud ) |
813 | // | 813 | // |
814 | // Cleanup for a thread userdata. If the thread is still executing, leave it | 814 | // Cleanup for a thread userdata. If the thread is still executing, leave it |
815 | // alive as a free-running thread (will clean up itself). | 815 | // alive as a free-running thread (will clean up itself). |
@@ -823,7 +823,7 @@ static LUAG_FUNC(lane_close) | |||
823 | static LUAG_FUNC(lane_gc) | 823 | static LUAG_FUNC(lane_gc) |
824 | { | 824 | { |
825 | bool _have_gc_cb{ false }; | 825 | bool _have_gc_cb{ false }; |
826 | Lane* const _lane{ ToLane(L_, 1) }; // L_: ud | 826 | Lane* const _lane{ ToLane(L_, 1) }; // L_: ud |
827 | 827 | ||
828 | // if there a gc callback? | 828 | // if there a gc callback? |
829 | lua_getiuservalue(L_, 1, 1); // L_: ud uservalue | 829 | lua_getiuservalue(L_, 1, 1); // L_: ud uservalue |
@@ -840,7 +840,7 @@ static LUAG_FUNC(lane_gc) | |||
840 | // We can read 'lane->status' without locks, but not wait for it | 840 | // We can read 'lane->status' without locks, but not wait for it |
841 | if (_lane->status < Lane::Done) { | 841 | if (_lane->status < Lane::Done) { |
842 | // still running: will have to be cleaned up later | 842 | // still running: will have to be cleaned up later |
843 | selfdestruct_add(_lane); | 843 | _lane->selfdestructAdd(); |
844 | assert(_lane->selfdestruct_next); | 844 | assert(_lane->selfdestruct_next); |
845 | if (_have_gc_cb) { | 845 | if (_have_gc_cb) { |
846 | luaG_pushstring(L_, "selfdestruct"); // L_: ud gc_cb name status | 846 | luaG_pushstring(L_, "selfdestruct"); // L_: ud gc_cb name status |