aboutsummaryrefslogtreecommitdiff
path: root/src/cancel.h
blob: d60903c391e99ee2a5bdbeed22083a20441e5bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once

#ifdef __cplusplus
extern "C"
{
#endif // __cplusplus
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#ifdef __cplusplus
}
#endif // __cplusplus

#include "macros_and_utils.h"
#include "uniquekey.h"

#include <string_view>

// #################################################################################################

class Lane; // forward

/*
 * Lane cancellation request modes
 */
enum class CancelRequest
{
    None, // no pending cancel request
    // TODO: add a Wake mode: user wants to wake the waiting lindas (in effect resulting in a timeout before the initial operation duration)
    Soft, // user wants the lane to cancel itself manually on cancel_test()
    Hard // user wants the lane to be interrupted (meaning code won't return from those functions) from inside linda:send/receive calls
};

enum class CancelResult
{
    Timeout,
    Cancelled
};

enum class CancelOp
{
    Invalid = -2,
    Hard = -1,
    Soft = 0,
    MaskCall = LUA_MASKCALL,
    MaskRet = LUA_MASKRET,
    MaskLine = LUA_MASKLINE,
    MaskCount = LUA_MASKCOUNT,
};

// xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator
static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel

[[nodiscard]] CancelOp which_cancel_op(std::string_view const& opString_);
[[nodiscard]] CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hookCount_, std::chrono::time_point<std::chrono::steady_clock> until_, bool wakeLane_);

[[noreturn]] static inline void raise_cancel_error(lua_State* L_)
{
    STACK_GROW(L_, 1);
    kCancelError.pushKey(L_); // special error value
    raise_lua_error(L_);
}

// #################################################################################################
// #################################################################################################

LUAG_FUNC(cancel_test);
LUAG_FUNC(thread_cancel);

// #################################################################################################