aboutsummaryrefslogtreecommitdiff
path: root/src/lane.h
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-07-03 09:28:33 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-07-03 09:28:33 +0200
commitf81fe873dd24f93306f0f667fc47766990a9321b (patch)
treee537fe83c6ad34c8de6a57be0ac94de391834192 /src/lane.h
parent2bc1eee14394d1c277b2dfc3a68e591c4bef66a9 (diff)
downloadlanes-f81fe873dd24f93306f0f667fc47766990a9321b.tar.gz
lanes-f81fe873dd24f93306f0f667fc47766990a9321b.tar.bz2
lanes-f81fe873dd24f93306f0f667fc47766990a9321b.zip
Add minimal coroutine support: no doc, no error handling, no cancellation handling (yet)
Diffstat (limited to 'src/lane.h')
-rw-r--r--src/lane.h26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/lane.h b/src/lane.h
index 0426240..753c230 100644
--- a/src/lane.h
+++ b/src/lane.h
@@ -17,6 +17,9 @@ static constexpr RegistryUniqueKey kExtendedStackTraceRegKey{ 0x38147AD48FB426E2
17 * error (and maybe stack trace) arguments to the finalizer functions would 17 * error (and maybe stack trace) arguments to the finalizer functions would
18 * anyways complicate that approach. 18 * anyways complicate that approach.
19 */ 19 */
20// xxh64 of string "kCoroutineRegKey" generated at https://www.pelock.com/products/hash-calculator
21static constexpr RegistryUniqueKey kCoroutineRegKey{ 0x72B049B0D130F009ull };
22
20// xxh64 of string "kFinalizerRegKey" generated at https://www.pelock.com/products/hash-calculator 23// xxh64 of string "kFinalizerRegKey" generated at https://www.pelock.com/products/hash-calculator
21static constexpr RegistryUniqueKey kFinalizerRegKey{ 0xFE936BFAA718FEEAull }; 24static constexpr RegistryUniqueKey kFinalizerRegKey{ 0xFE936BFAA718FEEAull };
22 25
@@ -45,13 +48,17 @@ class Lane
45 public: 48 public:
46 /* 49 /*
47 Pending: The Lua VM hasn't done anything yet. 50 Pending: The Lua VM hasn't done anything yet.
48 Running, Waiting: Thread is inside the Lua VM. If the thread is forcefully stopped, we can't lua_close() the Lua State. 51 Resuming: The user requested the lane to resume execution from Suspended state.
52 Suspended: returned from lua_resume, waiting for the client to request a lua_resume.
53 Running, Suspended, Waiting: Thread is inside the Lua VM.
49 Done, Error, Cancelled: Thread execution is outside the Lua VM. It can be lua_close()d. 54 Done, Error, Cancelled: Thread execution is outside the Lua VM. It can be lua_close()d.
50 */ 55 */
51 enum class Status 56 enum class Status
52 { 57 {
53 Pending, 58 Pending,
54 Running, 59 Running,
60 Suspended,
61 Resuming,
55 Waiting, 62 Waiting,
56 Done, 63 Done,
57 Error, 64 Error,
@@ -84,8 +91,9 @@ class Lane
84 91
85 std::string_view debugName{ "<unnamed>" }; 92 std::string_view debugName{ "<unnamed>" };
86 93
87 Universe* const U; 94 Universe* const U{};
88 lua_State* L; 95 lua_State* S{}; // the master state of the lane
96 lua_State* L{}; // the state we run things in (either S or a lua_newthread() state if we run in coroutine mode)
89 // 97 //
90 // M: prepares the state, and reads results 98 // M: prepares the state, and reads results
91 // S: while S is running, M must keep out of modifying the state 99 // S: while S is running, M must keep out of modifying the state
@@ -93,7 +101,7 @@ class Lane
93 Status volatile status{ Pending }; 101 Status volatile status{ Pending };
94 // 102 //
95 // M: sets to Pending (before launching) 103 // M: sets to Pending (before launching)
96 // S: updates -> Running/Waiting -> Done/Error/Cancelled 104 // S: updates -> Running/Waiting/Suspended -> Done/Error/Cancelled
97 105
98 std::condition_variable* volatile waiting_on{ nullptr }; 106 std::condition_variable* volatile waiting_on{ nullptr };
99 // 107 //
@@ -121,7 +129,7 @@ class Lane
121 // this one is for us, to make sure memory is freed by the correct allocator 129 // this one is for us, to make sure memory is freed by the correct allocator
122 static void operator delete(void* p_) { static_cast<Lane*>(p_)->U->internalAllocator.free(p_, sizeof(Lane)); } 130 static void operator delete(void* p_) { static_cast<Lane*>(p_)->U->internalAllocator.free(p_, sizeof(Lane)); }
123 131
124 Lane(Universe* U_, lua_State* L_, ErrorTraceLevel errorTraceLevel_); 132 Lane(Universe* U_, lua_State* L_, ErrorTraceLevel errorTraceLevel_, bool asCoroutine_);
125 ~Lane(); 133 ~Lane();
126 134
127 private: 135 private:
@@ -133,7 +141,13 @@ class Lane
133 141
134 CancelResult cancel(CancelOp op_, int hookCount_, std::chrono::time_point<std::chrono::steady_clock> until_, bool wakeLane_); 142 CancelResult cancel(CancelOp op_, int hookCount_, std::chrono::time_point<std::chrono::steady_clock> until_, bool wakeLane_);
135 void changeDebugName(int const nameIdx_); 143 void changeDebugName(int const nameIdx_);
136 void closeState() { lua_State* _L{ L }; L = nullptr; lua_close(_L); } 144 void closeState()
145 {
146 lua_State* _L{ S };
147 S = nullptr;
148 L = nullptr;
149 lua_close(_L); // this collects our coroutine thread at the same time
150 }
137 [[nodiscard]] std::string_view errorTraceLevelString() const; 151 [[nodiscard]] std::string_view errorTraceLevelString() const;
138 [[nodiscard]] int pushErrorHandler() const; 152 [[nodiscard]] int pushErrorHandler() const;
139 [[nodiscard]] std::string_view pushErrorTraceLevel(lua_State* L_) const; 153 [[nodiscard]] std::string_view pushErrorTraceLevel(lua_State* L_) const;