table of contents
| dnsjit.core.channel(3) | Library Functions Manual | dnsjit.core.channel(3) |
NAME¶
dnsjit.core.channel - Send data to another thread
SYNOPSIS¶
local chan = require("dnsjit.core.channel").new()
local thr = require("dnsjit.core.thread").new()
thr:start(function(thr)
local chan = thr:pop()
while true do
local obj = chan:get()
if obj == nil then -- nil indicates the channel is now closed
break
end
-- do work with obj
end
end)
thr:push(chan)
-- send work to thread using:
-- chan:put(...)
chan:close() -- close channel by sending a special close object
thr:stop()
DESCRIPTION¶
A channel can be used to send data to another thread, this is done by putting a pointer to the data into a wait-free and lock-free ring buffer (concurrency kit). The channel uses the single producer, single consumer model (SPSC) so there can only be one writer and one reader. The minimum size of the channel is 16 due to smaller size having huge effect on performance.
Attributes¶
- int closed
- Is 1 if the channel has been closed. NOTE; May be out of sync depending on memory order architectures, like ARM, and should only be used by the consumer.
Functions¶
- Channel.new(capacity)
- Create a new Channel, use the optional capacity to specify the capacity of the channel (buffer). Capacity must be a power-of-two greater than or equal to 4. Default capacity is 2048 and minimum size is 16.
- Channel:log()
- Return the Log object to control logging of this instance or module.
- Return information to use when sharing this object between threads.
- Channel:put(obj)
- Put an object into the channel, if the channel is full then it will stall and wait until space becomes available. Object may be nil.
- Channel:try_put(obj)
- Try and put an object into the channel. Returns 0 on success.
- Channel:get()
- Get an object from the channel, if the channel is empty it will wait until an object is available. Returns nil if the channel is/was closed or if a nil object was explicitly put into the channel.
- Channel:try_get()
- Try and get an object from the channel. Returns nil if there was no objects to get, if the channel is/was closed or if a nil object was explicitly put into the channel.
- Channel:size()
- Return number of enqueued objects. NOTE; This includes the special object used for closing the channel, see close().
- Channel:full()
- Returns true when channel is full.
- Channel:close()
- Close the channel by sending a special object to the consumer using put(). Will block execution until space becomes available in channel.
- Channel:try_close()
- Close the channel by sending a special object to the consumer using try_put(). Return 0 on success.
- Channel:receive()
- Return the C functions and context for receiving objects.
- Channel:receiver(o)
- Set the receiver to pass objects to. NOTE; The channel keeps no reference of the receiver, it needs to live as long as the channel does.
- Channel:run()
- Retrieve all objects from the channel and send it to the receiver.
SEE ALSO¶
AUTHORS and CONTRIBUTORS¶
Jerry Lundström (DNS-OARC), Tomáš Křížek (CZ.NIC), Petr Špaček (ISC)
Maintained by DNS-OARC
BUGS¶
For issues and feature requests please use:
For question and help please use:
| 1.6.0 | dnsjit |