summaryrefslogtreecommitdiff
path: root/indra/newview/scripts/lua/ErrorQueue.lua
blob: 076742815a1e6a2e85e82241b2e592bb3215b481 (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
-- ErrorQueue isa WaitQueue with the added feature that a producer can push an
-- error through the queue. Once that error is dequeued, every consumer will
-- raise that error.

local WaitQueue = require('WaitQueue')
-- local debug = require('printf')
local function debug(...) end

local ErrorQueue = WaitQueue:new()

function ErrorQueue:Error(message)
    -- Setting Error() is a marker, like closing the queue. Once we reach the
    -- error, every subsequent Dequeue() call will raise the same error.
    debug('Setting self._closed to %q', message)
    self._closed = message
    self:_wake_waiters()
end

function ErrorQueue:Dequeue()
    local value = WaitQueue.Dequeue(self)
    debug('ErrorQueue:Dequeue: base Dequeue() got %s', value)
    if value ~= nil then
        -- queue not yet closed, show caller
        return value
    end
    if self._closed == true then
        -- WaitQueue:close() sets true: queue has only been closed, tell caller
        return nil
    end
    -- self._closed is a message set by Error()
    error(self._closed)
end

return ErrorQueue