aboutsummaryrefslogtreecommitdiff
path: root/src/lane.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lane.cpp35
1 files changed, 18 insertions, 17 deletions
diff --git a/src/lane.cpp b/src/lane.cpp
index 6f4935e..c2cbbac 100644
--- a/src/lane.cpp
+++ b/src/lane.cpp
@@ -671,17 +671,23 @@ static void PrepareLaneHelpers(Lane* lane_)
671 671
672// ################################################################################################# 672// #################################################################################################
673 673
674static void lane_main(Lane* lane_) 674static void lane_main(Lane* const lane_)
675{ 675{
676 lua_State* const _L{ lane_->L };
677 // wait until the launching thread has finished preparing L 676 // wait until the launching thread has finished preparing L
677#ifndef __PROSPERO__
678 lane_->ready.wait(); 678 lane_->ready.wait();
679#else // __PROSPERO__
680 while (!lane_->ready._My_flag) {
681 std::this_thread::yield();
682 }
683#endif // __PROSPERO__
684
685 lua_State* const _L{ lane_->L };
679 LuaError _rc{ LuaError::ERRRUN }; 686 LuaError _rc{ LuaError::ERRRUN };
680 if (lane_->status == Lane::Pending) { // nothing wrong happened during preparation, we can work 687 if (lane_->status == Lane::Pending) { // nothing wrong happened during preparation, we can work
681 // At this point, the lane function and arguments are on the stack, possibly preceded by the error handler 688 // At this point, the lane function and arguments are on the stack, possibly preceded by the error handler
682 int const _errorHandlerCount{ lane_->errorTraceLevel == Lane::Minimal ? 0 : 1}; 689 int const _errorHandlerCount{ lane_->errorTraceLevel == Lane::Minimal ? 0 : 1};
683 int const _nargs{ lua_gettop(_L) - 1 - _errorHandlerCount }; 690 int const _nargs{ lua_gettop(_L) - 1 - _errorHandlerCount };
684 DEBUGSPEW_CODE(Universe* _U = Universe::Get(_L));
685 lane_->status = Lane::Running; // Pending -> Running 691 lane_->status = Lane::Running; // Pending -> Running
686 692
687 PrepareLaneHelpers(lane_); 693 PrepareLaneHelpers(lane_);
@@ -695,11 +701,11 @@ static void lane_main(Lane* lane_)
695 // in case of error and if it exists, fetch stack trace from registry and push it 701 // in case of error and if it exists, fetch stack trace from registry and push it
696 push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] 702 push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace]
697 703
698 DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, 1)) << ")" << std::endl); 704 DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, 1)) << ")" << std::endl);
699 // Call finalizers, if the script has set them up. 705 // Call finalizers, if the script has set them up.
700 // 706 //
701 LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) }; 707 LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) };
702 DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " finalizer: " << GetErrcodeName(_rc2) << std::endl); 708 DEBUGSPEW_CODE(DebugSpew(lane_->U) << "Lane " << _L << " finalizer: " << GetErrcodeName(_rc2) << std::endl);
703 if (_rc2 != LuaError::OK) { // Error within a finalizer! 709 if (_rc2 != LuaError::OK) { // Error within a finalizer!
704 // the finalizer generated an error, and left its own error message [and stack trace] on the stack 710 // the finalizer generated an error, and left its own error message [and stack trace] on the stack
705 _rc = _rc2; // we're overruling the earlier script error or normal return 711 _rc = _rc2; // we're overruling the earlier script error or normal return
@@ -716,21 +722,16 @@ static void lane_main(Lane* lane_)
716 // we destroy our jthread member from inside the thread body, so we have to detach so that we don't try to join, as this doesn't seem a good idea 722 // we destroy our jthread member from inside the thread body, so we have to detach so that we don't try to join, as this doesn't seem a good idea
717 lane_->thread.detach(); 723 lane_->thread.detach();
718 delete lane_; 724 delete lane_;
719 lane_ = nullptr; 725 return;
720 } 726 }
721 } 727 }
722 if (lane_) {
723 // leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them
724 728
725 Lane::Status const _st{ (_rc == LuaError::OK) ? Lane::Done : kCancelError.equals(_L, 1) ? Lane::Cancelled : Lane::Error }; 729 // leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them
726 730 Lane::Status const _st{ (_rc == LuaError::OK) ? Lane::Done : kCancelError.equals(_L, 1) ? Lane::Cancelled : Lane::Error };
727 { 731 // 'doneMutex' protects the -> Done|Error|Cancelled state change
728 // 'doneMutex' protects the -> Done|Error|Cancelled state change 732 std::lock_guard _guard{ lane_->doneMutex };
729 std::lock_guard _guard{ lane_->doneMutex }; 733 lane_->status = _st;
730 lane_->status = _st; 734 lane_->doneCondVar.notify_one(); // wake up master (while 'lane_->doneMutex' is on)
731 lane_->doneCondVar.notify_one(); // wake up master (while 'lane_->doneMutex' is on)
732 }
733 }
734} 735}
735 736
736// ################################################################################################# 737// #################################################################################################