-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathact.lua
More file actions
608 lines (543 loc) · 15.8 KB
/
act.lua
File metadata and controls
608 lines (543 loc) · 15.8 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
--
-- Copyright (C) 2016-present Masatoshi Fukunaga
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
--
-- act.lua
-- lua-act
-- Created by Masatoshi Teruya on 16/12/25.
--
--- ignore SIGPIPE
require('act.ignsigpipe')
--- file scope variables
local pcall = pcall
local format = string.format
local concat = table.concat
local type = type
local waitpid = require('waitpid')
local gettime = require('time.clock').gettime
local fork = require('act.fork')
local aux = require('act.aux')
local is_int = aux.is_int
local is_uint = aux.is_uint
local is_unsigned = aux.is_unsigned
local Callee = require('act.callee')
local new_callee = Callee.new
local callee_acquire = Callee.acquire
local callee_resume = Callee.resume
local callee_unwait = Callee.unwait
local callee_unwait_readable = Callee.unwait_readable
local callee_unwait_writable = Callee.unwait_writable
local new_context = require('act.context').new
--- constants
--- @type act.context?
local ACT_CTX
local EVENT_CACHE_ENABLED = false
--- event_cache
--- @param enabled boolean
local function event_cache(enabled)
if callee_acquire() then
error('the event cache state cannot be changed at runtime execution', 2)
elseif type(enabled) ~= 'boolean' then
error('enabled must be boolean', 2)
end
EVENT_CACHE_ENABLED = enabled
end
--- spawn_child
--- @param is_atexit boolean
--- @param fn function
--- @param ... any
--- @return any cid
local function spawn_child(is_atexit, fn, ...)
-- create new callee
local callee = new_callee(ACT_CTX, is_atexit, fn, ...)
return callee.cid
end
--- pollable
--- @return boolean ok
local function pollable()
return callee_acquire() and true or false
end
--- pfork
--- @return fork.process? p
--- @return any err
--- @return boolean? again
local function pfork()
if callee_acquire() then
--- @type fork.process, any, boolean
local p, err, again = fork()
if not p then
return nil, err, again
elseif p:is_child() then
-- child process must be rebuilding event properties
ACT_CTX:renew()
end
return p
end
error('cannot call fork() from outside of execution context', 2)
end
--- @class waitpid.result
--- @field pid integer process id
--- @field exit integer exit code
--- @field sigcont boolean true if the process was resumed by SIGCONT
--- @field sigterm integer signal number that caused the process to terminate
--- @field sigstop integer signal number that caused the process to stop
--- pwaitpid
--- @param sec? number timeout in seconds
--- @param wpid? integer process id to wait
--- @param ... string options
--- | 'nohang' : set WNOHANG option (default)
--- | 'untraced' : set WUNTRACED option
--- | 'continued' : set WCONTINUED option
--- @return waitpid.result? res
--- @return any err
--- @return boolean? timeout
local function pwaitpid(sec, wpid, ...)
local callee = callee_acquire()
if callee then
local interval = 0.1
local deadline
if sec ~= nil then
if not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
deadline = gettime() + sec
if interval > sec then
interval = sec
end
end
if wpid ~= nil and not is_int(wpid) then
error('wpid must be integer', 2)
end
while true do
local res, err, again = waitpid(wpid, 'nohang', ...)
if res then
return res
elseif not again then
return nil, err
end
if deadline then
local remain = deadline - gettime()
if remain <= 0 then
-- timeout
return nil, nil, true
elseif remain < interval then
-- update interval
interval = remain
end
end
-- sleep
callee:sleep(interval)
end
end
error('cannot call waitpid() from outside of execution context', 2)
end
--- spawn
--- @param fn function
--- @param ... any
--- @return any cid
local function spawn(fn, ...)
if callee_acquire() then
if type(fn) ~= 'function' then
error('fn must be function', 2)
end
return spawn_child(false, fn, ...)
end
error('cannot call spawn() from outside of execution context', 2)
end
--- exit
--- @param ... any
local function exit(...)
local callee = callee_acquire()
if callee then
callee:exit(...)
end
error('cannot call exit() at outside of execution context', 2)
end
--- later
local function later()
local callee = callee_acquire()
if not callee then
error('cannot call later() from outside of execution context', 2)
end
callee:later()
end
--- yield
--- @param sec? number
--- @param ... any
--- @return boolean ok
local function yield(sec, ...)
local callee = callee_acquire()
if callee then
if sec ~= nil and not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
return callee:yield(sec, ...)
end
error('cannot call yield() from outside of execution context', 2)
end
--- awaitq_size
--- @param qsize? integer
--- @return integer qlen
local function awaitq_size(qsize)
local callee = callee_acquire()
if callee then
if qsize ~= nil and not is_int(qsize) then
error('qsize must be integer', 2)
end
return callee:awaitq_size(qsize)
end
error('cannot call awaitq_size() at outside of execution context', 2)
end
--- await
--- @param sec? number
--- @return table stat
--- @return boolean timeout
local function await(sec)
local callee = callee_acquire()
if callee then
if sec ~= nil and not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
return callee:await(sec)
end
error('cannot call await() at outside of execution context', 2)
end
--- suspend
--- @param sec? number
--- @return boolean ok
--- @return any ...
local function suspend(sec)
local callee = callee_acquire()
if callee then
if sec ~= nil and not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
return callee:suspend(sec)
end
error('cannot call suspend() at outside of execution context', 2)
end
--- resume
--- @param cid any
--- @param ... any
--- @return boolean ok
local function resume(cid, ...)
if callee_acquire() then
return callee_resume(cid, ...)
end
error('cannot call resume() at outside of execution context', 2)
end
--- sleep
--- @param sec number
--- @return integer rem
--- @return string? err
local function sleep(sec)
local callee = callee_acquire()
if callee then
if not is_unsigned(sec) then
error('sec must be unsigned number', 2)
end
return callee:sleep(sec)
end
error('cannot call sleep() from outside of execution context', 2)
end
--- sigwait
--- @param sec? number
--- @param ... any
--- @return integer signo
--- @return string? err
--- @return boolean? timeout
local function sigwait(sec, ...)
local callee = callee_acquire()
if callee then
if sec ~= nil and not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
return callee:sigwait(sec, ...)
end
error('cannot call sleep() from outside of execution context', 2)
end
--- read_lock
--- @param fd integer
--- @param sec? number
--- @return boolean ok
--- @return string? err
--- @return boolean? timeout
local function read_lock(fd, sec)
local callee = callee_acquire()
if callee then
if not is_uint(fd) then
error('fd must be unsigned integer', 2)
elseif sec ~= nil and not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
return callee:read_lock(fd, sec)
end
error('cannot call read_lock() from outside of execution context', 2)
end
--- read_unlock
--- @param fd integer
local function read_unlock(fd)
local callee = callee_acquire()
if callee then
if not is_uint(fd) then
error('fd must be unsigned integer', 2)
end
return callee:read_unlock(fd)
end
error('cannot call read_unlock() from outside of execution context', 2)
end
--- write_lock
--- @param fd integer
--- @param sec? number
--- @return boolean ok
--- @return any err
--- @return boolean? timeout
local function write_lock(fd, sec)
local callee = callee_acquire()
if callee then
if not is_uint(fd) then
error('fd must be unsigned integer', 2)
elseif sec ~= nil and not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
return callee:write_lock(fd, sec)
end
error('cannot call write_lock() from outside of execution context', 2)
end
--- write_unlock
--- @param fd integer
local function write_unlock(fd)
local callee = callee_acquire()
if callee then
if not is_uint(fd) then
error('fd must be unsigned integer', 2)
end
return callee:write_unlock(fd)
end
error('cannot call write_unlock() from outside of execution context', 2)
end
--- unwait_readable
--- @param fd integer
--- @return boolean ok
local function unwait_readable(fd)
if ACT_CTX then
if not is_uint(fd) then
error('fd must be unsigned integer', 2)
end
callee_unwait_readable(ACT_CTX, fd)
return true
end
return false
end
--- unwait_writable
--- @param fd integer
--- @return boolean ok
local function unwait_writable(fd)
if ACT_CTX then
if not is_uint(fd) then
error('fd must be unsigned integer', 2)
end
callee_unwait_writable(ACT_CTX, fd)
return true
end
return false
end
--- unwait
--- @param fd integer
--- @return boolean ok
local function unwait(fd)
if ACT_CTX then
if not is_uint(fd) then
error('fd must be unsigned integer', 2)
end
callee_unwait(ACT_CTX, fd)
return true
end
return false
end
--- wait_readable
--- @param fd integer?
--- @param sec? number
--- @param ... integer additional fds
--- @return integer? fd
--- @return any err
--- @return boolean? timeout
--- @return boolean? hup
local function wait_readable(fd, sec, ...)
local callee = callee_acquire()
if callee then
if sec ~= nil and not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
return callee:wait_readable(fd, sec, ...)
end
error('cannot call wait_readable() from outside of execution context', 2)
end
--- wait_writable
--- @param fd integer?
--- @param sec? number
--- @param ... integer additional fds
--- @return integer? fd
--- @return any err
--- @return boolean? timeout
--- @return boolean? hup
local function wait_writable(fd, sec, ...)
local callee = callee_acquire()
if callee then
if sec ~= nil and not is_unsigned(sec) then
error('sec must be unsigned number or nil', 2)
end
return callee:wait_writable(fd, sec, ...)
end
error('cannot call wait_writable() from outside of execution context', 2)
end
--- getcid
--- @return any cid
local function getcid()
local callee = callee_acquire()
if callee then
return callee.cid
end
error('cannot call getcid() at outside of execution context', 2)
end
--- atexit
--- @param exitfn function
--- @param ... any
--- @return boolean ok
local function atexit(exitfn, ...)
if callee_acquire() then
if type(exitfn) ~= 'function' then
error('exitfn must be function', 2)
end
spawn_child(true, exitfn, ...)
return true
end
error('cannot call atexit() at outside of execution context', 2)
end
--- runloop
--- @param mainfn function
--- @param ... any
--- @return boolean ok
--- @return any err
local function runloop(mainfn, ...)
-- create act context
local err
ACT_CTX, err = new_context(EVENT_CACHE_ENABLED)
if err then
return false, err
end
-- create main coroutine
spawn_child(false, mainfn, ...)
-- run act scheduler
local runq = ACT_CTX.runq
local event = ACT_CTX.event
while true do
-- consume runq
local sec = runq:consume()
-- finish if no more callee
if not ACT_CTX:has_active_callees() then
return true
end
-- consume events
local remain
remain, err = event:consume(sec)
if err then
-- got critical error
return false, err
elseif remain == 0 then
-- no more events
-- finish if no more callee
if runq:len() == 0 then
-- get traceback of active callees
local infos = ACT_CTX:getinfo_active_callees()
if #infos > 0 then
-- some callees may be waiting for resume by other callees
-- but no body resume them.
local list = {
'deadlock detected',
}
for i = 1, #infos do
list[#list + 1] =
format(' %s:%d', infos[i].short_src,
infos[i].currentline)
end
error(concat(list, '\n'))
end
return true
end
-- sleep until timer time elapsed
local ok
ok, err = runq:sleep()
if not ok then
-- got critical error
return ok, err
end
end
end
end
--- run
--- @param mainfn function
--- @param ... any
--- @return boolean ok
--- @return any err
local function run(mainfn, ...)
-- check first argument
if type(mainfn) ~= 'function' then
error('mainfn must be function', 2)
elseif ACT_CTX then
return false, 'act run already'
end
local ok, rv, err = pcall(runloop, mainfn, ...)
collectgarbage('collect')
ACT_CTX = nil
if ok then
return rv, err
end
return false, rv
end
-- exports
return {
run = run,
event_cache = event_cache,
getcid = getcid,
wait_writable = wait_writable,
wait_readable = wait_readable,
unwait = unwait,
unwait_writable = unwait_writable,
unwait_readable = unwait_readable,
write_unlock = write_unlock,
write_lock = write_lock,
read_unlock = read_unlock,
read_lock = read_lock,
sigwait = sigwait,
sleep = sleep,
resume = resume,
suspend = suspend,
await = await,
awaitq_size = awaitq_size,
atexit = atexit,
yield = yield,
later = later,
exit = exit,
spawn = spawn,
fork = pfork,
waitpid = pwaitpid,
pollable = pollable,
}