summaryrefslogtreecommitdiff
path: root/server/src/thread.lua
blob: af206a50da87d17c29aba0c4e6dfbc4f20f6c268 (plain)
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
local thread     = require 'bee.thread'
local response, errlog
local proto_cache

local api = {
    log = function (data)
        local level = data.level
        local buf   = data.buf
        log[level](buf)
    end,
    proto = function (data)
        if not proto_cache then
            proto_cache = {}
        end
        proto_cache[#proto_cache+1] = data
    end,
}

local function on_tick()
    if not response then
        return
    end
    local ok, msg = errlog:pop()
    if ok then
        log.error(msg)
    end
    local ok, who, data = response:pop()
    if ok then
        api[who](data)
    end
end

local function require(name)
    if not response then
        thread.newchannel 'response'
        response = thread.channel 'response'
        errlog   = thread.channel 'errlog'
    end

    local buf = ("\z
        package.cpath = %q\n\z
        package.path = %q\n\z
        require %q\n\z
    "):format(package.cpath, package.path, name)
    return thread.thread(buf)
end

local function proto()
    if not proto_cache then
        return nil
    end
    return table.remove(proto_cache, 1)
end

return {
    require = require,
    on_tick = on_tick,
    proto   = proto,
    sleep   = thread.sleep,
}