|
| 1 | +-- SPDX-License-Identifier: MIT |
| 2 | +-- Author: Jianhui Zhao <zhaojh329@gmail.com> |
| 3 | + |
| 4 | +local sync = require 'eco.sync' |
| 5 | + |
| 6 | +local M = {} |
| 7 | + |
| 8 | +local methods = {} |
| 9 | + |
| 10 | +function methods:length() |
| 11 | + return #self.buf |
| 12 | +end |
| 13 | + |
| 14 | +function methods:close() |
| 15 | + if self.closed then |
| 16 | + return |
| 17 | + end |
| 18 | + |
| 19 | + self.closed = true |
| 20 | + self.cond_recv:signal() |
| 21 | +end |
| 22 | + |
| 23 | +-- On success, true is returned |
| 24 | +-- On error, false is returned with a string describing the error |
| 25 | +function methods:send(v, timeout) |
| 26 | + assert(not self.closed, 'sending on closed channel') |
| 27 | + |
| 28 | + local buf = self.buf |
| 29 | + |
| 30 | + if #buf == self.capacity then |
| 31 | + local ok, err = self.cond_send:wait(timeout) |
| 32 | + if not ok then |
| 33 | + return false, err |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + buf[#buf + 1] = v |
| 38 | + |
| 39 | + self.cond_recv:signal() |
| 40 | + |
| 41 | + return true |
| 42 | +end |
| 43 | + |
| 44 | +-- On success, the value received is returned |
| 45 | +-- On closed, nil is returned |
| 46 | +-- On error, nil is returned with a string describing the error |
| 47 | +function methods:recv(timeout) |
| 48 | + local buf = self.buf |
| 49 | + |
| 50 | + if #buf < 1 then |
| 51 | + if self.closed then |
| 52 | + return nil |
| 53 | + end |
| 54 | + |
| 55 | + local ok, err = self.cond_recv:wait(timeout) |
| 56 | + if not ok then |
| 57 | + return nil, err |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + if #buf < 1 then |
| 62 | + return nil |
| 63 | + end |
| 64 | + |
| 65 | + local v = buf[1] |
| 66 | + |
| 67 | + table.remove(buf, 1) |
| 68 | + |
| 69 | + self.cond_send:signal() |
| 70 | + |
| 71 | + return v |
| 72 | +end |
| 73 | + |
| 74 | +local metatable = { |
| 75 | + __index = methods |
| 76 | +} |
| 77 | + |
| 78 | +--[[ |
| 79 | +A channel provides a mechanism for communication between coroutines by sending and receiving values. |
| 80 | +
|
| 81 | +When a coroutine sends data to a channel, if the channel is full, the sending operation is blocked |
| 82 | +until another coroutine has taken data from the channel. |
| 83 | +
|
| 84 | +Similarly, when a coroutine receives data from a channel, if there is no data available in the channel, |
| 85 | +the receiving operation will be blocked until another coroutine has sent data to the channel. |
| 86 | +
|
| 87 | +Closing a channel notifies other coroutines that the channel is no longer in use. After a channel is closed, |
| 88 | +other coroutines can still receive data from it, but they can no longer send data to it. |
| 89 | +
|
| 90 | +A channel can have a buffer, default is 1. |
| 91 | +--]] |
| 92 | +function M.new(capacity) |
| 93 | + assert(capacity == nil or math.type(capacity) == 'integer', 'capacity must be an integer') |
| 94 | + |
| 95 | + if not capacity or capacity < 1 then |
| 96 | + capacity = 1 |
| 97 | + end |
| 98 | + |
| 99 | + local ch = { |
| 100 | + cond_send = sync.cond(), |
| 101 | + cond_recv = sync.cond(), |
| 102 | + capacity = capacity, |
| 103 | + closed = false, |
| 104 | + buf = {} |
| 105 | + } |
| 106 | + |
| 107 | + return setmetatable(ch, metatable) |
| 108 | +end |
| 109 | + |
| 110 | +return M |
0 commit comments