Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the trivial-channels Reference Manual, version 1.0, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Aug 15 06:02:33 2022 GMT+0.
Next: Systems, Previous: The trivial-channels Reference Manual, Up: The trivial-channels Reference Manual [Contents][Index]
This is a very, very trivial implementation of channels (and a queue). I find myself using it in a few places where very trivial message passing is needed and a more complex, robust solution would be overkill.
(let ((channel (make-channel)))
(sendmsg channel 'anything)
(recvmsg channel)) ;; => ANYTHING
(make-channel)
: Make a channel(sendmsg CHANNEL OBJECT)
: Send OBJECT
on CHANNEL
(recvmsg CHANNEL &optional TIMEOUT)
: Receive on CHANNEL
,
optionally timing out after TIMEOUT
seconds(getmsg CHANNEL)
: Get a message if available, or NIL
(hasmsg CHANNEL)
: Whether the channel has a message. No guarantee
it will still have one after the call, if there are multiple
receivers sharing a channel.Notably, recvmsg
supports a timeout. These functions properly lock
and it's safe to share a channel between threads (that being the
entire purpose).
While trivial-channels should be simple enough you can adapt it to many usage patterns, for simple bi-directional message passing I have found it easiest to simply pass a message with a return-channel included:
;;; Sender:
(defvar *global-listener* (make-channel))
(defvar *done* nil)
(let* ((return-channel (make-channel))
(msg (cons 'value return-channel)))
(sendmsg *global-listener* msg)
(recvmsg return-channel))
;;; Meanwhile, in another thread:
(loop until *done* do
(let ((msg (recvmsg *global-listener*)))
(let ((value (car msg))
(channel (cdr msg)))
;; insert useful things here
(sendmsg channel ...))))
Of course, you needn't create a new return channel every time, either, if you are worried about consing, but this is an easy way to pass functions to a specific thread, implement actors, etc.
The queue used to implement this is also available via the package
:trivial-channels.queue
, since it's sometimes handy to have a
trivial queue, too.
Queues do not lock.
Queues are implemented with conses.
(make-queue)
: Make a queue(queue-head Q)
: Head of the queue(queue-tail Q)
: Tail of the queue(queue-add-cons Q CONS)
: CONS
becomes the tail of the queue; its
CDR may be destructively modified(queue-add Q ITEM)
: Add ITEM
to the tail of the queue(queue-push Q ITEM)
: Push ITEM
onto the front of the queue(queue-pop Q)
: Pop and return and item from the queue, or NIL(queue-pop-cons Q)
: Pop and return the cons from the queue, or NIL(queue-has-item-p Q)
: If there are items in the queue, or NIL(queue-peek Q)
: The front item in the queue, or NIL(queue-pop-to Q1 Q2)
: Pop an item from Q1
and add it to Q2
without consing(queue-prepend-to Q1 Q2)
: Remove all items from Q1
and prepend
them to Q2
without consing; no value returnedAll add or remove type operations return the item (or cons) being handled, unless otherwise noted.
Next: Files, Previous: Introduction, Up: The trivial-channels Reference Manual [Contents][Index]
The main system appears first, followed by any subsystem dependency.
Really simple channels and queue
Ryan Pavlik
BSD-2-Clause
1.0
Next: Packages, Previous: Systems, Up: The trivial-channels Reference Manual [Contents][Index]
Files are sorted by type and then listed depth-first from the systems components trees.
Next: trivial-channels/package.lisp, Previous: Lisp, Up: Lisp [Contents][Index]
trivial-channels (system).
Next: trivial-channels/trivial-channels.lisp, Previous: trivial-channels/trivial-channels.asd, Up: Lisp [Contents][Index]
trivial-channels (system).
Previous: trivial-channels/package.lisp, Up: Lisp [Contents][Index]
package.lisp (file).
trivial-channels (system).
Next: Definitions, Previous: Files, Up: The trivial-channels Reference Manual [Contents][Index]
Packages are listed by definition order.
Next: trivial-channels.asdf, Previous: Packages, Up: Packages [Contents][Index]
Next: trivial-channels.queue, Previous: trivial-channels, Up: Packages [Contents][Index]
Previous: trivial-channels.asdf, Up: Packages [Contents][Index]
common-lisp.
Next: Indexes, Previous: Packages, Up: The trivial-channels Reference Manual [Contents][Index]
Definitions are sorted by export status, category, package, and then by lexicographic order.
Next: Internals, Previous: Definitions, Up: Definitions [Contents][Index]
Next: Structures, Previous: Public Interface, Up: Public Interface [Contents][Index]
head.
Peek at the head of the queue.
Pop from ‘Q1‘, adding to ‘Q2‘, without consing.
Prepend all items in ‘Q1‘ to ‘Q2‘, removing them from ‘Q1‘
tail.
Previous: Ordinary functions, Up: Public Interface [Contents][Index]
structure-object.
trivial-channels.queue:queue
(trivial-channels.queue:make-queue)
(bordeaux-threads:make-condition-variable)
(bordeaux-threads:make-lock)
Previous: Public Interface, Up: Definitions [Contents][Index]
By default we use TRIVIAL-TIMEOUTS; this can be changed for implementations later should it prove less-than-optimal.
Previous: Definitions, Up: The trivial-channels Reference Manual [Contents][Index]
Jump to: | (
C F G H M Q R S W |
---|
Jump to: | (
C F G H M Q R S W |
---|
Next: Data types, Previous: Functions, Up: Indexes [Contents][Index]
Jump to: | H Q S T |
---|
Jump to: | H Q S T |
---|
Jump to: | C F P Q S T |
---|
Jump to: | C F P Q S T |
---|