aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/async/control.lua
blob: 57aa88dbb447f852a70d7ba3fa4043ece1eca939 (plain) (blame)
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
local a = require "mason-core.async"

---@class Condvar
local Condvar = {}
Condvar.__index = Condvar

function Condvar.new()
    return setmetatable({ handles = {} }, Condvar)
end

---@async
function Condvar:wait()
    a.wait(function(resolve)
        self.handles[#self.handles + 1] = resolve
    end)
end

function Condvar:notify()
    local handle = table.remove(self.handles)
    pcall(handle)
end

function Condvar:notify_all()
    while #self.handles > 0 do
        self:notify()
    end
    self.handles = {}
end

---@class Permit
local Permit = {}
Permit.__index = Permit

function Permit.new(semaphore)
    return setmetatable({ semaphore = semaphore }, Permit)
end

function Permit:forget()
    local semaphore = self.semaphore
    semaphore.permits = semaphore.permits + 1

    if semaphore.permits > 0 and #semaphore.handles > 0 then
        semaphore.permits = semaphore.permits - 1
        local release = table.remove(semaphore.handles, 1)
        release(Permit.new(semaphore))
    end
end

---@class Semaphore
local Semaphore = {}
Semaphore.__index = Semaphore

---@param permits integer
function Semaphore.new(permits)
    return setmetatable({ permits = permits, handles = {} }, Semaphore)
end

---@async
function Semaphore:acquire()
    if self.permits > 0 then
        self.permits = self.permits - 1
    else
        return a.wait(function(resolve)
            table.insert(self.handles, resolve)
        end)
    end

    return Permit.new(self)
end

---@class OneShotChannel
---@field has_sent boolean
---@field value any
---@field condvar Condvar
local OneShotChannel = {}
OneShotChannel.__index = OneShotChannel

function OneShotChannel.new()
    return setmetatable({
        has_sent = false,
        value = nil,
        condvar = Condvar.new(),
    }, OneShotChannel)
end

function OneShotChannel:is_closed()
    return self.has_sent
end

function OneShotChannel:send(...)
    assert(not self.has_sent, "Oneshot channel can only send once.")
    self.has_sent = true
    self.value = { ... }
    self.condvar:notify_all()
    self.condvar = nil
end

function OneShotChannel:receive()
    if not self.has_sent then
        self.condvar:wait()
    end
    return unpack(self.value)
end

---@class Channel
---@field private condvar Condvar
---@field private buffer any?
---@field is_closed boolean
local Channel = {}
Channel.__index = Channel
function Channel.new()
    return setmetatable({
        condvar = Condvar.new(),
        buffer = nil,
        is_closed = false,
    }, Channel)
end

function Channel:close()
    self.is_closed = true
end

---@async
function Channel:send(value)
    assert(not self.is_closed, "Channel is closed.")
    while self.buffer ~= nil do
        self.condvar:wait()
    end
    self.buffer = value
    self.condvar:notify()
    while self.buffer ~= nil do
        self.condvar:wait()
    end
end

---@async
function Channel:receive()
    assert(not self.is_closed, "Channel is closed.")
    while self.buffer == nil do
        self.condvar:wait()
    end
    local value = self.buffer
    self.buffer = nil
    self.condvar:notify()
    return value
end

---@async
function Channel:iter()
    return function()
        while not self.is_closed do
            return self:receive()
        end
    end
end

return {
    Condvar = Condvar,
    Semaphore = Semaphore,
    OneShotChannel = OneShotChannel,
    Channel = Channel,
}