diff options
Diffstat (limited to 'src/lanes_private.h')
-rw-r--r-- | src/lanes_private.h | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/lanes_private.h b/src/lanes_private.h new file mode 100644 index 0000000..a7e21d7 --- /dev/null +++ b/src/lanes_private.h | |||
@@ -0,0 +1,117 @@ | |||
1 | #if !defined __lanes_private_h__ | ||
2 | #define __lanes_private_h__ 1 | ||
3 | |||
4 | #include "uniquekey.h" | ||
5 | |||
6 | /* | ||
7 | * Lane cancellation request modes | ||
8 | */ | ||
9 | enum e_cancel_request | ||
10 | { | ||
11 | CANCEL_NONE, // no pending cancel request | ||
12 | CANCEL_SOFT, // user wants the lane to cancel itself manually on cancel_test() | ||
13 | CANCEL_HARD // user wants the lane to be interrupted (meaning code won't return from those functions) from inside linda:send/receive calls | ||
14 | }; | ||
15 | |||
16 | // NOTE: values to be changed by either thread, during execution, without | ||
17 | // locking, are marked "volatile" | ||
18 | // | ||
19 | struct s_Lane | ||
20 | { | ||
21 | THREAD_T thread; | ||
22 | // | ||
23 | // M: sub-thread OS thread | ||
24 | // S: not used | ||
25 | |||
26 | char const* debug_name; | ||
27 | |||
28 | lua_State* L; | ||
29 | Universe* U; | ||
30 | // | ||
31 | // M: prepares the state, and reads results | ||
32 | // S: while S is running, M must keep out of modifying the state | ||
33 | |||
34 | volatile enum e_status status; | ||
35 | // | ||
36 | // M: sets to PENDING (before launching) | ||
37 | // S: updates -> RUNNING/WAITING -> DONE/ERROR_ST/CANCELLED | ||
38 | |||
39 | SIGNAL_T* volatile waiting_on; | ||
40 | // | ||
41 | // When status is WAITING, points on the linda's signal the thread waits on, else NULL | ||
42 | |||
43 | volatile enum e_cancel_request cancel_request; | ||
44 | // | ||
45 | // M: sets to FALSE, flags TRUE for cancel request | ||
46 | // S: reads to see if cancel is requested | ||
47 | |||
48 | #if THREADWAIT_METHOD == THREADWAIT_CONDVAR | ||
49 | SIGNAL_T done_signal; | ||
50 | // | ||
51 | // M: Waited upon at lane ending (if Posix with no PTHREAD_TIMEDJOIN) | ||
52 | // S: sets the signal once cancellation is noticed (avoids a kill) | ||
53 | |||
54 | MUTEX_T done_lock; | ||
55 | // | ||
56 | // Lock required by 'done_signal' condition variable, protecting | ||
57 | // lane status changes to DONE/ERROR_ST/CANCELLED. | ||
58 | #endif // THREADWAIT_METHOD == THREADWAIT_CONDVAR | ||
59 | |||
60 | volatile enum | ||
61 | { | ||
62 | NORMAL, // normal master side state | ||
63 | KILLED // issued an OS kill | ||
64 | } mstatus; | ||
65 | // | ||
66 | // M: sets to NORMAL, if issued a kill changes to KILLED | ||
67 | // S: not used | ||
68 | |||
69 | struct s_Lane* volatile selfdestruct_next; | ||
70 | // | ||
71 | // M: sets to non-NULL if facing lane handle '__gc' cycle but the lane | ||
72 | // is still running | ||
73 | // S: cleans up after itself if non-NULL at lane exit | ||
74 | |||
75 | #if HAVE_LANE_TRACKING | ||
76 | struct s_Lane* volatile tracking_next; | ||
77 | #endif // HAVE_LANE_TRACKING | ||
78 | // | ||
79 | // For tracking only | ||
80 | }; | ||
81 | typedef struct s_Lane Lane; | ||
82 | |||
83 | // To allow free-running threads (longer lifespan than the handle's) | ||
84 | // 'Lane' are malloc/free'd and the handle only carries a pointer. | ||
85 | // This is not deep userdata since the handle's not portable among lanes. | ||
86 | // | ||
87 | #define lua_toLane( L, i) (*((Lane**) luaL_checkudata( L, i, "Lane"))) | ||
88 | |||
89 | // crc64/we of string "CANCEL_ERROR" generated at http://www.nitrxgen.net/hashgen/ | ||
90 | static DECLARE_CONST_UNIQUE_KEY(CANCEL_ERROR, 0xe97d41626cc97577); // 'cancel_error' sentinel | ||
91 | |||
92 | // crc64/we of string "CANCEL_TEST_KEY" generated at http://www.nitrxgen.net/hashgen/ | ||
93 | static DECLARE_CONST_UNIQUE_KEY(CANCEL_TEST_KEY, 0xe66f5960c57d133a); // used as registry key | ||
94 | |||
95 | static inline Lane* get_lane_from_registry( lua_State* L) | ||
96 | { | ||
97 | Lane* s; | ||
98 | STACK_GROW( L, 1); | ||
99 | STACK_CHECK( L); | ||
100 | push_unique_key( L, CANCEL_TEST_KEY); | ||
101 | lua_rawget( L, LUA_REGISTRYINDEX); | ||
102 | s = lua_touserdata( L, -1); // lightuserdata (true 's_lane' pointer) / nil | ||
103 | lua_pop( L, 1); | ||
104 | STACK_END( L, 0); | ||
105 | return s; | ||
106 | } | ||
107 | |||
108 | static inline int cancel_error( lua_State* L) | ||
109 | { | ||
110 | STACK_GROW( L, 1); | ||
111 | push_unique_key( L, CANCEL_ERROR); // special error value | ||
112 | return lua_error( L); // doesn't return | ||
113 | } | ||
114 | |||
115 | |||
116 | |||
117 | #endif // __lanes_private_h__ \ No newline at end of file | ||