aboutsummaryrefslogtreecommitdiff
path: root/src/cancel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/cancel.cpp')
-rw-r--r--src/cancel.cpp86
1 files changed, 40 insertions, 46 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp
index cff1443..82c6def 100644
--- a/src/cancel.cpp
+++ b/src/cancel.cpp
@@ -51,9 +51,9 @@ THE SOFTWARE.
51* Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, 51* Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called,
52* to make execution of the lane end. 52* to make execution of the lane end.
53*/ 53*/
54[[nodiscard]] static inline CancelRequest cancel_test(lua_State* L) 54[[nodiscard]] static inline CancelRequest cancel_test(lua_State* L_)
55{ 55{
56 Lane* const lane{ kLanePointerRegKey.readLightUserDataValue<Lane>(L) }; 56 Lane* const lane{ kLanePointerRegKey.readLightUserDataValue<Lane>(L_) };
57 // 'lane' is nullptr for the original main state (and no-one can cancel that) 57 // 'lane' is nullptr for the original main state (and no-one can cancel that)
58 return lane ? lane->cancel_request : CancelRequest::None; 58 return lane ? lane->cancel_request : CancelRequest::None;
59} 59}
@@ -68,21 +68,21 @@ THE SOFTWARE.
68// 68//
69LUAG_FUNC(cancel_test) 69LUAG_FUNC(cancel_test)
70{ 70{
71 CancelRequest test{ cancel_test(L) }; 71 CancelRequest test{ cancel_test(L_) };
72 lua_pushboolean(L, test != CancelRequest::None); 72 lua_pushboolean(L_, test != CancelRequest::None);
73 return 1; 73 return 1;
74} 74}
75 75
76// ################################################################################################# 76// #################################################################################################
77// ################################################################################################# 77// #################################################################################################
78 78
79[[nodiscard]] static void cancel_hook(lua_State* L, [[maybe_unused]] lua_Debug* ar) 79[[nodiscard]] static void cancel_hook(lua_State* L_, [[maybe_unused]] lua_Debug* ar)
80{ 80{
81 DEBUGSPEW_CODE(fprintf(stderr, "cancel_hook\n")); 81 DEBUGSPEW_CODE(fprintf(stderr, "cancel_hook\n"));
82 if (cancel_test(L) != CancelRequest::None) 82 if (cancel_test(L_) != CancelRequest::None)
83 { 83 {
84 lua_sethook(L, nullptr, 0, 0); 84 lua_sethook(L_, nullptr, 0, 0);
85 raise_cancel_error(L); 85 raise_cancel_error(L_);
86 } 86 }
87} 87}
88 88
@@ -204,16 +204,16 @@ CancelOp which_cancel_op(char const* op_string_)
204 204
205// ################################################################################################# 205// #################################################################################################
206 206
207[[nodiscard]] static CancelOp which_cancel_op(lua_State* L, int idx_) 207[[nodiscard]] static CancelOp which_cancel_op(lua_State* L_, int idx_)
208{ 208{
209 if (lua_type(L, idx_) == LUA_TSTRING) 209 if (lua_type(L_, idx_) == LUA_TSTRING)
210 { 210 {
211 char const* const str{ lua_tostring(L, idx_) }; 211 char const* const str{ lua_tostring(L_, idx_) };
212 CancelOp op{ which_cancel_op(str) }; 212 CancelOp op{ which_cancel_op(str) };
213 lua_remove(L, idx_); // argument is processed, remove it 213 lua_remove(L_, idx_); // argument is processed, remove it
214 if (op == CancelOp::Invalid) 214 if (op == CancelOp::Invalid)
215 { 215 {
216 raise_luaL_error(L, "invalid hook option %s", str); 216 raise_luaL_error(L_, "invalid hook option %s", str);
217 } 217 }
218 return op; 218 return op;
219 } 219 }
@@ -225,58 +225,52 @@ CancelOp which_cancel_op(char const* op_string_)
225// bool[,reason] = lane_h:cancel( [mode, hookcount] [, timeout] [, wake_lindas]) 225// bool[,reason] = lane_h:cancel( [mode, hookcount] [, timeout] [, wake_lindas])
226LUAG_FUNC(thread_cancel) 226LUAG_FUNC(thread_cancel)
227{ 227{
228 Lane* const lane{ ToLane(L, 1) }; 228 Lane* const lane{ ToLane(L_, 1) };
229 CancelOp const op{ which_cancel_op(L, 2) }; // this removes the op string from the stack 229 CancelOp const op{ which_cancel_op(L_, 2) }; // this removes the op string from the stack
230 230
231 int hook_count{ 0 }; 231 int hook_count{ 0 };
232 if (static_cast<int>(op) > static_cast<int>(CancelOp::Soft)) // hook is requested 232 if (static_cast<int>(op) > static_cast<int>(CancelOp::Soft)) // hook is requested
233 { 233 {
234 hook_count = static_cast<int>(luaL_checkinteger(L, 2)); 234 hook_count = static_cast<int>(luaL_checkinteger(L_, 2));
235 lua_remove(L, 2); // argument is processed, remove it 235 lua_remove(L_, 2); // argument is processed, remove it
236 if (hook_count < 1) 236 if (hook_count < 1) {
237 { 237 raise_luaL_error(L_, "hook count cannot be < 1");
238 raise_luaL_error(L, "hook count cannot be < 1");
239 } 238 }
240 } 239 }
241 240
242 lua_Duration wait_timeout{ 0.0 }; 241 lua_Duration wait_timeout{ 0.0 };
243 if (lua_type(L, 2) == LUA_TNUMBER) 242 if (lua_type(L_, 2) == LUA_TNUMBER) {
244 { 243 wait_timeout = lua_Duration{ lua_tonumber(L_, 2) };
245 wait_timeout = lua_Duration{ lua_tonumber(L, 2) }; 244 lua_remove(L_, 2); // argument is processed, remove it
246 lua_remove(L, 2); // argument is processed, remove it 245 if (wait_timeout.count() < 0.0) {
247 if (wait_timeout.count() < 0.0) 246 raise_luaL_error(L_, "cancel timeout cannot be < 0");
248 {
249 raise_luaL_error(L, "cancel timeout cannot be < 0");
250 } 247 }
251 } 248 }
252 // we wake by default in "hard" mode (remember that hook is hard too), but this can be turned off if desired 249 // we wake by default in "hard" mode (remember that hook is hard too), but this can be turned off if desired
253 bool wake_lane{ op != CancelOp::Soft }; 250 bool wake_lane{ op != CancelOp::Soft };
254 if (lua_gettop(L) >= 2) 251 if (lua_gettop(L_) >= 2) {
255 { 252 if (!lua_isboolean(L_, 2)) {
256 if (!lua_isboolean(L, 2)) 253 raise_luaL_error(L_, "wake_lindas parameter is not a boolean");
257 {
258 raise_luaL_error(L, "wake_lindas parameter is not a boolean");
259 } 254 }
260 wake_lane = lua_toboolean(L, 2); 255 wake_lane = lua_toboolean(L_, 2);
261 lua_remove(L, 2); // argument is processed, remove it 256 lua_remove(L_, 2); // argument is processed, remove it
262 } 257 }
263 STACK_CHECK_START_REL(L, 0); 258 STACK_CHECK_START_REL(L_, 0);
264 switch (thread_cancel(lane, op, hook_count, wait_timeout, wake_lane)) 259 switch (thread_cancel(lane, op, hook_count, wait_timeout, wake_lane)) {
265 { 260 default: // should never happen unless we added a case and forgot to handle it
266 default: // should never happen unless we added a case and forgot to handle it 261 LUA_ASSERT(L_, false);
267 LUA_ASSERT(L, false);
268 break; 262 break;
269 263
270 case CancelResult::Timeout: 264 case CancelResult::Timeout:
271 lua_pushboolean(L, 0); // false 265 lua_pushboolean(L_, 0); // false
272 lua_pushstring(L, "timeout"); // false "timeout" 266 lua_pushstring(L_, "timeout"); // false "timeout"
273 break; 267 break;
274 268
275 case CancelResult::Cancelled: 269 case CancelResult::Cancelled:
276 lua_pushboolean(L, 1); // true 270 lua_pushboolean(L_, 1); // true
277 lane->pushThreadStatus(L); // true status 271 lane->pushThreadStatus(L_); // true status
278 break; 272 break;
279 } 273 }
280 STACK_CHECK(L, 2); 274 STACK_CHECK(L_, 2);
281 return 2; 275 return 2;
282} 276}