From c562d0f9e336251f9ff599a840310835820b0461 Mon Sep 17 00:00:00 2001 From: ame Date: Wed, 8 May 2024 10:55:05 -0500 Subject: work on threads --- docs/net.md | 13 +--- docs/thread.md | 202 ++++++++++++++++++++++++++++----------------------------- 2 files changed, 103 insertions(+), 112 deletions(-) (limited to 'docs') diff --git a/docs/net.md b/docs/net.md index 28a7070..474cdbe 100644 --- a/docs/net.md +++ b/docs/net.md @@ -10,15 +10,6 @@ the function will be ran on initilization, the argument has info on the server a ** right now everything within a server:GET function is partially global, it can read global variables (by making a copy), it can not read/copy local variables or modify globals - -also, (for now) all globals must be refrenced as _G, -ie: -function foo() - ... -end - -_G.foo() -_G.llib.crypto.md5("hewwo purr") ** ```lua @@ -53,14 +44,14 @@ the second (req) contains info on the request, the path allows for wildcards, mu the actual name of the function will change based on what request method you want to accept, all requests are treated the exact same on the backend, besides HEAD requests which will also use all GET requets, and the 'all' variant will get everything ```lua -server:all("*", function(res, req, next) +server:all("*", function(res, req) if(req['Version'] ~= "HTTP/1.1") then res:close() end end) ... -server:GET("/", function(res, req) do +server:GET("/", function(res, req) --version will always be 1.1, as per the middleware ... end) diff --git a/docs/thread.md b/docs/thread.md index a8c10ad..0c8de77 100644 --- a/docs/thread.md +++ b/docs/thread.md @@ -1,101 +1,101 @@ -# threads ** - -## lock, unlock** - -'takes an integer - -locks any other thread reaching this lock id until a corresponding unlock is met - -```lua -llib.thread.lock(5) -... -llib.thread.unlock(5) -``` - -more indepth - -```lua -local t = llib.thread.async(function(info) - ... - llib.thread.lock(5) - ... - res(N) -end) - -... -llib.thread.unlock(5) -t:await() -``` - -## aync ** - -'takes a function which will be ran in a separate thread with a single parameter with thread info - -these have the same backend (and limitations) of network threads - -```lua -local thread = llib.thread.async(function(res, rej) - local N = 0 - ... - res(N) -end) -``` - -### thread function parameters ** - -as used with "res" above - -#### res:res() ** - -> will be eventually used as just res() - -'takes any amount of "any" values - -send a value(s) to thread:await() call then stalls the thread until cleaned -#### res:send() ** - -'takes "any" value - -send a value which can be retrieved from outside the thread with thread:next() - -```lua -info:send(5) -info:send("hello") -``` - -### thread return object ** - -#### thread:await() ** - -'optional timeout in ms and boolean whether to keep or not - -waits for the thread to return, and returns whatever it returned then closes it, or nil if timeout was exceeded -if the input is the boolean value true, it will keep the thread alive (otherwise await() can not be called again) - -```lua -thread:await() -- value of N (above) -``` - -```lua -thread:await(20) -- value of N (above) or nil -``` - -```lua -thread:await(true) -- value of N (above) -thread:await() -- same -thread:await() -- error, function above performed cleanup -``` - -#### thread:next() ** - -gets the most oldest value sent using info:send() and pops it - -```lua ---(continued from above) -thread:next() -- 5 -thread:next() -- "hello" -``` - -#### thread:kill() ** - -kills the thread +# threads ** + +## lock, unlock + +'takes an integer + +locks any other thread reaching this lock id until a corresponding unlock is met + +```lua +llib.thread.lock(5) +... +llib.thread.unlock(5) +``` + +more indepth + +```lua +llib.thread.lock(5) +local t = llib.thread.async(function(info) + ... + llib.thread.lock(5) + ... + res(N) +end) + +... +llib.thread.unlock(5) +t:await() +``` + +## aync ** + +'takes a function which will be ran in a separate thread with a single parameter with thread info + +these have the same backend (and limitations) of network threads + +```lua +local thread = llib.thread.async(function(res, rej) + local N = 0 + ... + res(N) +end) +``` + +### thread function parameters ** + +as used with "res" above + +#### res() + +'takes any amount of "any" values + +send a value(s) to thread:await() call then stalls the thread until cleaned + +#### res:send() ** + +'takes "any" value + +send a value which can be retrieved from outside the thread with thread:next() + +```lua +res:send(5) +res:send("hello") +``` + +### thread return object ** + +#### thread:await() ** + +'optional timeout in ms and boolean whether to keep or not + +waits for the thread to return, and returns whatever it returned then closes it, or nil if timeout was exceeded +if the input is the boolean value true, it will keep the thread alive (otherwise await() can not be called again) + +```lua +thread:await() -- value of N (above) +``` + +```lua +thread:await(20) -- value of N (above) or nil +``` + +```lua +thread:await(true) -- value of N (above) +thread:await() -- same +thread:await() -- error, function above performed cleanup +``` + +#### thread:next() ** + +gets the most oldest value sent using info:send() and pops it + +```lua +--(continued from above) +thread:next() -- 5 +thread:next() -- "hello" +``` + +#### thread:kill() ** + +kills the thread -- cgit v1.2.3