• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / copas / 30290467123

27 Jul 2026 05:42PM UTC coverage: 84.244% (-1.1%) from 85.334%
30290467123

push

github

web-flow
Merge 53b2d76fc into c0eb97fbe

43 of 71 new or added lines in 1 file covered. (60.56%)

106 existing lines in 4 files now uncovered.

1465 of 1739 relevant lines covered (84.24%)

59952.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.91
/src/copas/queue.lua
1
local copas = require "copas"
6✔
2
local gettime = copas.gettime
6✔
3
local Sema = copas.semaphore
6✔
4
local Lock = copas.lock
6✔
5

6

7
local Queue = {}
6✔
8
Queue.__index = Queue
6✔
9

10

11
local new_name do
6✔
12
  local count = 0
6✔
13

UNCOV
14
  function new_name()
5✔
15
    count = count + 1
36✔
16
    return "copas_queue_" .. count
36✔
17
  end
18
end
19

20

21
-- Creates a new Queue instance
22
function Queue.new(opts)
6✔
23
  opts = opts or {}
36✔
24
  local self = {}
36✔
25
  setmetatable(self, Queue)
36✔
26
  self.name = opts.name or new_name()
42✔
27
  self.sema = Sema.new(10^9)
42✔
28
  self.head = 1
36✔
29
  self.tail = 1
36✔
30
  self.list = {}
36✔
31
  self.workers = setmetatable({}, { __mode = "k" })
36✔
32
  self.stopping = false
36✔
33
  self.worker_id = 0
36✔
34
  self.exit_semaphore = Sema.new(10^9)
42✔
35
  return self
36✔
36
end
37

38

39
-- Pushes an item in the queue (can be 'nil')
40
-- returns true, or nil+err ("stopping", or "destroyed")
41
function Queue:push(item)
6✔
42
  if self.stopping then
108✔
43
    return nil, "stopping"
×
44
  end
45
  self.list[self.head] = item
108✔
46
  self.head = self.head + 1
108✔
47
  self.sema:give()
108✔
48
  return true
108✔
49
end
50

51

52
-- Pops and item from the queue. If there are no items in the queue it will yield
53
-- until there are or a timeout happens (exception is when `timeout == 0`, then it will
54
-- not yield but return immediately). If the timeout is `math.huge` it will wait forever.
55
-- Returns item, or nil+err ("timeout", or "destroyed")
56
function Queue:pop(timeout)
6✔
57
  local ok, err = self.sema:take(1, timeout)
132✔
58
  if not ok then
132✔
59
    return ok, err
30✔
60
  end
61

62
  local item = self.list[self.tail]
102✔
63
  self.list[self.tail] = nil
102✔
64
  self.tail = self.tail + 1
102✔
65

66
  if self.tail == self.head then
102✔
67
    -- reset queue
68
    self.list = {}
42✔
69
    self.tail = 1
42✔
70
    self.head = 1
42✔
71
    if self.stopping then
42✔
72
      -- we're stopping and last item being returned, so we're done
73
      self:destroy()
6✔
74
    end
75
  end
76
  return item
102✔
77
end
78

79

80
-- return the number of items left in the queue
81
function Queue:get_size()
6✔
82
  return self.head - self.tail
60✔
83
end
84

85

86
-- instructs the queue to stop. Will not accept any more 'push' calls.
87
-- will autocall 'destroy' when the queue is empty.
88
-- returns immediately. See `finish`
89
function Queue:stop()
6✔
90
  if not self.stopping then
36✔
91
    self.stopping = true
36✔
92
    self.lock = Lock.new(nil, true)
42✔
93
    self.lock:get() -- close the lock
36✔
94
    if self:get_size() == 0 then
42✔
95
      -- queue is already empty, so "pop" function cannot call destroy on next
96
      -- pop, so destroy now.
97
      self:destroy()
24✔
98
    end
99
  end
100
  return true
36✔
101
end
102

103

104
-- Finishes a queue. Calls stop and then waits for the queue to run empty (and be
105
-- destroyed) before returning. returns true or nil+err ("timeout", or "destroyed")
106
-- Parameter no_destroy_on_timeout indicates if the queue is not to be forcefully
107
-- destroyed on a timeout.
108
function Queue:finish(timeout, no_destroy_on_timeout)
6✔
109
  self:stop()
24✔
110
  timeout = timeout or self.lock.timeout
24✔
111
  local endtime = gettime() + timeout
24✔
112
  local _, err = self.lock:get(timeout)
27✔
113
  -- the lock never gets released, only destroyed, so we have to check the error string
114
  if err == "timeout" then
24✔
115
    if not no_destroy_on_timeout then
6✔
116
      self:destroy()
6✔
117
    end
118
    return nil, err
6✔
119
  end
120

121
  -- if we get here, the lock was destroyed, so the queue is empty, now wait for all workers to exit
122
  if not next(self.workers) then
18✔
123
    -- all workers already exited, we're done
124
    return true
×
125
  end
126

127
  -- multiple threads can call this "finish" method, so we must check exiting workers
128
  -- one by one.
129
  while true do
130
    local _, err = self.exit_semaphore:take(1, math.max(0, endtime - gettime()))
24✔
131
    if err == "destroyed" then
24✔
132
      return true  -- someone else destroyed/finished it, so we're done
×
133
    end
134
    if err == "timeout" then
24✔
135
      if not no_destroy_on_timeout then
×
136
        self:destroy()
×
137
      end
138
      return nil, "timeout"
×
139
    end
140
    if not next(self.workers) then
24✔
141
      self.exit_semaphore:destroy()
18✔
142
      return true  -- all workers exited, we're done
18✔
143
    end
144
  end
145
end
146

147

148
do
149
  local destroyed_func = function()
150
    return nil, "destroyed"
24✔
151
  end
152

153
  local destroyed_queue_mt = {
6✔
154
    __index = function()
155
      return destroyed_func
24✔
156
    end
157
  }
158

159
  -- destroys a queue immediately. Abandons what is left in the queue.
160
  -- Releases all waiting threads with `nil+"destroyed"`
161
  function Queue:destroy()
6✔
162
    if self.lock then
36✔
163
      self.lock:destroy()
36✔
164
    end
165
    self.sema:destroy()
36✔
166
    setmetatable(self, destroyed_queue_mt)
36✔
167

168
    -- clear anything left in the queue
169
    for key in pairs(self.list) do
42✔
170
      self.list[key] = nil
6✔
171
    end
172

173
    return true
36✔
174
  end
175
end
176

177

178
-- adds a worker that will handle whatever is passed into the queue. Can be called
179
-- multiple times to add more workers.
180
-- The threads automatically exit when the queue is destroyed.
181
-- worker function signature: `function(item)` (Note: worker functions run
182
-- unprotected, so wrap code in an (x)pcall if errors are expected, otherwise the
183
-- worker will exit on an error, and queue handling will stop)
184
-- Returns the coroutine added.
185
function Queue:add_worker(worker)
6✔
186
  assert(type(worker) == "function", "expected worker to be a function")
36✔
187
  local coro
188

189
  self.worker_id = self.worker_id + 1
36✔
190
  local worker_name = self.name .. ":worker_" .. self.worker_id
36✔
191

192
  coro = copas.addnamedthread(worker_name, function()
72✔
193
    while true do
194
      local item, err = self:pop(math.huge) -- wait forever
92✔
195
      if err then
90✔
196
        break -- queue destroyed, exit
18✔
197
      end
198
      worker(item) -- TODO: wrap in errorhandling
63✔
199
    end
200
    self.workers[coro] = nil
36✔
201
    if self.exit_semaphore then
36✔
202
      self.exit_semaphore:give(1)
36✔
203
    end
204
  end)
205

206
  self.workers[coro] = true
36✔
207
  return coro
36✔
208
end
209

210
-- returns a list/array of current workers (coroutines) handling the queue.
211
-- (only the workers added by `add_worker`, and still active, will be in this list)
212
function Queue:get_workers()
6✔
213
  local lst = {}
×
214
  for coro in pairs(self.workers) do
×
215
    if coroutine.status(coro) ~= "dead" then
×
216
      lst[#lst+1] = coro
×
217
    end
218
  end
219
  return lst
×
220
end
221

222
return Queue
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc