Skip to content

Commit

Permalink
add support for forcibly inserting yields, locked behind a kernel arg…
Browse files Browse the repository at this point in the history
…ument because it slows down the system so much
  • Loading branch information
Ocawesome101 committed Jun 11, 2021
1 parent 936b94e commit e8e2c0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions base/load.lua
@@ -0,0 +1,37 @@
-- wrap load() to forcibly insert yields --

k.log(k.loglevels.info, "base/load")

if k.cmdline.force_yields then
local patterns = {
{ "if([ %(])(.-)([ %)])then([ \n])", "if%1%2%3then%4coroutine.yield(0)" },
{ "while([ %(])(.-)([ %)])do([ \n])", "while%1%2%3do%4coroutine.yield(0) " },
{ "for([ %(])(.-)([ %)])do([ \n])", "for%1%2%3do%4coroutine.yield(0) " },
{ "repeat([ \n])", "repeat%1coroutine.yield(0) " },
}

local old_load = load

function _G.load(chunk, name, mode, env)
checkArg(1, chunk, "function", "string")
checkArg(2, name, "string", "nil")
checkArg(3, mode, "string", "nil")
checkArg(4, env, "table", "nil")

local data = ""
if type(chunk) == "string" then
data = chunk
else
repeat
local ch = chunk()
data = data .. (ch or "")
until not ch
end

for i=1, #patterns, 1 do
chunk = chunk:gsub(patterns[i][1], patterns[i][2])
end

return old_load(chunk, name, mode, env)
end
end
1 change: 1 addition & 0 deletions init.lua
Expand Up @@ -24,6 +24,7 @@ end
--#include "base/types.lua"
--#include "base/struct.lua"
--#include "base/syslog.lua"
--#include "base/load.lua"
--#include "base/thread.lua"
--#include "base/process.lua"
--#include "base/scheduler.lua"
Expand Down

0 comments on commit e8e2c0c

Please sign in to comment.