forked from LuaJIT/LuaJIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreading.lua
More file actions
156 lines (130 loc) · 5.01 KB
/
Copy paththreading.lua
File metadata and controls
156 lines (130 loc) · 5.01 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
---@meta threading
---@alias threading.timeout_error "timeout"
---@alias threading.thread_fn fun(...):...
---@alias threading.recv_status true|false|"timeout"
---@alias threading.peek_status true|false
---@alias threading.send_result true|nil
---@class threading.thread: userdata
local threading_thread = {}
---Wait for this thread to finish.
---
---Returns `true, ...` when the child function returns, `false, err` when the
---child function errors, or `nil, "timeout"` when a timeout is supplied and the
---thread is still running.
---@overload fun(self: threading.thread): true, ...
---@overload fun(self: threading.thread): false, any
---@overload fun(self: threading.thread, timeout: number): true, ...
---@overload fun(self: threading.thread, timeout: number): false, any
---@overload fun(self: threading.thread, timeout: number): nil, threading.timeout_error
---@param timeout? number seconds to wait; omitted blocks indefinitely.
---@return true|false|nil ok true for child success, false for child error, nil on timeout.
---@return any ...
function threading_thread:join(timeout) end
---@return integer id
---@nodiscard
function threading_thread:id() end
---@return boolean running
---@nodiscard
function threading_thread:running() end
---@return "threading.thread"
---@nodiscard
function threading_thread:__tostring() end
---@class threading.mutex: userdata
local threading_mutex = {}
---Block until the mutex is acquired.
---@return nil
function threading_mutex:lock() end
---@return boolean locked
---@nodiscard
function threading_mutex:trylock() end
---Release the mutex.
---
---Errors if the mutex is not currently locked.
---@return nil
function threading_mutex:unlock() end
---@return "threading.mutex"
---@nodiscard
function threading_mutex:__tostring() end
---@generic T
---@class threading.channel<T>: userdata
---@field send fun(self: threading.channel<T>, value: T, timeout?: number): threading.send_result, threading.timeout_error|nil
---@field recv fun(self: threading.channel<T>, timeout?: number): T|nil, threading.recv_status
---@field peek fun(self: threading.channel<T>): T|nil, threading.peek_status
---@field close fun(self: threading.channel<T>): nil
local threading_channel = {}
---Send a value to the channel.
---
---Errors if the channel is closed. If this is a rendezvous channel, success
---means a receiver has taken the value.
---@generic T
---@overload fun(self: threading.channel<T>, value: T): true
---@overload fun(self: threading.channel<T>, value: T, timeout: number): true
---@overload fun(self: threading.channel<T>, value: T, timeout: number): nil, threading.timeout_error
---@param value T
---@param timeout? number seconds to wait; omitted blocks indefinitely.
---@return threading.send_result ok
---@return threading.timeout_error|nil err
function threading_channel:send(value, timeout) end
---Receive a value from the channel.
---
---Returns `value, true` on success, `nil, false` when the channel is closed, or
---`nil, "timeout"` when a timeout is supplied and no value is available.
---@generic T
---@overload fun(self: threading.channel<T>): T, true
---@overload fun(self: threading.channel<T>): nil, false
---@overload fun(self: threading.channel<T>, timeout: number): T, true
---@overload fun(self: threading.channel<T>, timeout: number): nil, false
---@overload fun(self: threading.channel<T>, timeout: number): nil, threading.timeout_error
---@param timeout? number seconds to wait; omitted blocks indefinitely.
---@return T|nil value
---@return threading.recv_status status
function threading_channel:recv(timeout) end
---Inspect the next value without removing it from the channel.
---
---Returns `value, true` on success, or `nil, false` when the channel is empty
---or closed.
---@generic T
---@overload fun(self: threading.channel<T>): T, true
---@overload fun(self: threading.channel<T>): nil, false
---@return T|nil value
---@return threading.peek_status status
function threading_channel:peek() end
---Close the channel.
---@return nil
function threading_channel:close() end
---@return "threading.channel"
---@nodiscard
function threading_channel:__tostring() end
---@class threadinglib
local threading = {}
---@return integer count
---@nodiscard
function threading.cpucount() end
---Issue a cross-thread memory fence.
---@return nil
function threading.fence() end
---@param seconds? number seconds to sleep; defaults to 0.
---@return nil
function threading.sleep(seconds) end
---Spawn a new OS thread that calls `fn(...)`.
---@param fn threading.thread_fn
---@param ... any
---@return threading.thread thread
---@nodiscard
function threading.spawn(fn, ...) end
---@return threading.thread thread
---@nodiscard
function threading.current() end
---@return threading.mutex mutex
---@nodiscard
function threading.mutex() end
---Create a channel.
---
---Capacity 0 creates a rendezvous channel. Buffered channels have at least the
---requested capacity.
---@generic T
---@param capacity? integer buffered slot count; defaults to 0.
---@return threading.channel<T> channel
---@nodiscard
function threading.channel(capacity) end
return threading