github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/lua/thread.quotas.lua (about) 1 local ctx, co = runtime.callcontext({kill={cpu=1000}}, coroutine.create, function (n) 2 while true do 3 local d = 0 4 while n > 0 do 5 n = n - 1 6 d = d + 2 7 end 8 n = coroutine.yield(d) 9 end 10 end) 11 12 -- coroutines are resumed in the context of the resume call, see below: the cpu 13 -- limit of 10000 is applied on each resume call 14 15 print(runtime.callcontext({kill={cpu=10000}}, coroutine.resume, co, 100)) 16 --> =done true 200 17 18 print(runtime.callcontext({kill={cpu=10000}}, coroutine.resume, co, 500)) 19 --> =done true 1000 20 21 print(runtime.callcontext({kill={cpu=10000}}, coroutine.resume, co, 500)) 22 --> =done true 1000 23 24 print(runtime.callcontext({kill={cpu=10000}}, coroutine.resume, co, 1000)) 25 --> =killed 26 27 -- If a coroutine ran out of resources, then it becomes dead and it cannot be resumed 28 29 print(coroutine.status(co)) 30 --> =dead 31 32 print(coroutine.resume(co, 100)) 33 --> =false cannot resume dead thread