mailbox
- mailbox
- Documentation
- mailbox-timeout-condition?
- make-mailbox
- mailbox?
- mailbox-name
- mailbox-empty?
- mailbox-count
- mailbox-waiting?
- mailbox-waiters
- mailbox-send!
- mailbox-receive!
- mailbox-wait!
- mailbox-push-back!
- mailbox-push-back-list!
- make-mailbox-cursor
- mailbox-cursor?
- mailbox-cursor-mailbox
- mailbox-cursor-next
- mailbox-cursor-rewind
- mailbox-cursor-extract-and-rewind
- mailbox-cursor-rewound?
- mailbox-cursor-unwound?
- Usage
- Examples
- Requirements
- Notes
- Bugs & Limitations
- Author
- Repository
- Version history
- License
Documentation
Thread-safe queues with timeout.
mailbox-timeout-condition?
[procedure] (mailbox-timeout-condition? OBJ) --> booleanIs the OBJ a mailbox timeout condition?
A mailbox timeout condition is a composite condition of 'exn, 'mailbox, and 'timeout conditions.
The 'exn condition has properties of 'location, 'message, 'arguments, and 'call-chain.
The 'mailbox condition has properties of 'box.
The 'timeout condition has properties of 'time, and 'value.
make-mailbox
[procedure] (make-mailbox [NAME]) -> mailboxReturns a new mailbox object.
- NAME
- * : default (gensym 'mailbox)
- identify this mailbox.
mailbox?
[procedure] (mailbox? OBJ) --> booleanIs the OBJ a mailbox?
mailbox-name
[procedure] (mailbox-name MAILBOX) -> *Returns the name of MAILBOX.
mailbox-empty?
[procedure] (mailbox-empty? MAILBOX) -> booleanIf there are no queued objects in MAILBOX, returns #t, otherwise #f.
mailbox-count
[procedure] (mailbox-count MAILBOX) -> integerReturns the number of queued objects for MAILBOX.
mailbox-waiting?
[procedure] (mailbox-waiting? MAILBOX) -> booleanIs a thread waiting for MAILBOX?
mailbox-waiters
[procedure] (mailbox-waiters MAILBOX) -> (list-of thread)Returns the threads waiting for MAILBOX.
mailbox-send!
[procedure] (mailbox-send! MAILBOX OBJ)Queues the data object OBJ. If any threads exist that are waiting for input on MAILBOX, the execution of the first one will be resumed. The data will be read out of a mailbox in the same order in which is written in (in FIFO manner).
mailbox-receive!
[procedure] (mailbox-receive! MAILBOX [TIMEOUT [DEFAULT]]) -> *If there is any data in MAILBOX, then the first object will be removed and returned as the result. If the mailbox is currently empty, the current thread will suspended until data is available.
TIMEOUT is a SRFI-18 time object or the real number of seconds.
Should TIMEOUT be specified and occur the DEFAULT, if supplied, will be returned. Otherwise a mailbox timeout exception will be signaled for the calling thread. The DEFAULT value cannot be (void).
mailbox-wait!
[procedure] (mailbox-wait! MAILBOX [TIMEOUT])Similar to mailbox-receive!, but does not remove the received result from the queue of pending data.
TIMEOUT is a SRFI-18 time object or the real number of seconds.
Should TIMEOUT be specified and occur a mailbox timeout exception will be signaled for the calling thread.
mailbox-push-back!
[procedure] (mailbox-push-back! MAILBOX OBJ)Pushes the data object OBJ into the first position of a mailbox.
mailbox-push-back-list!
[procedure] (mailbox-push-back-list! MAILBOX OBJS)Pushes the list of objects OBJS back into the mailbox, so that (car OBJS) becomes the next receivable item.
make-mailbox-cursor
[procedure] (make-mailbox-cursor MAILBOX) -> mailbox-cursorReturns an object which can enumerate a mailbox.
Multiple cursors can scan, and mutate, the same mailbox.
mailbox-cursor?
[procedure] (mailbox-cursor? OBJ) --> booleanIs the OBJ a mailbox-cursor?
mailbox-cursor-mailbox
[procedure] (mailbox-cursor-mailbox MAILBOX-CURSOR) -> mailboxReturns the mailbox object associated with the mailbox cursor.
mailbox-cursor-next
[procedure] (mailbox-cursor-next MAILBOX-CURSOR [TIMEOUT [DEFAULT]]) -> *Returns the next object in the mailbox queue, waiting if necessary.
The mailbox queue is scanned from oldest to newest.
TIMEOUT is a SRFI-18 time object or the real number of seconds.
Should TIMEOUT be specified and occur the DEFAULT, if supplied, will be returned. Otherwise a mailbox timeout exception will be signaled for the calling thread. The DEFAULT value cannot be (void).
mailbox-cursor-rewind
[procedure] (mailbox-cursor-rewind MAILBOX-CURSOR)Position the cursor at the oldest message in the mailbox.
mailbox-cursor-extract-and-rewind
[procedure] (mailbox-cursor-extract-and-rewind MAILBOX-CURSOR)Remove from the associated mailbox queue the last object returned by mailbox-cursor-next and position the cursor at the oldest message in the mailbox.
The extraction is not performed without a previous call to mailbox-cursor-next.
mailbox-cursor-rewound?
[procedure] (mailbox-cursor-rewound? MAILBOX-CURSOR) -> booleanIs MAILBOX-CURSOR positioned at the start of the mailbox queue?
mailbox-cursor-unwound?
[procedure] (mailbox-cursor-unwound? MAILBOX-CURSOR) -> booleanIs MAILBOX-CURSOR positioned at the end of the mailbox queue?
Usage
(import mailbox)
Examples
(import (chicken base)) (import (srfi 18)) (import mailbox) (define (say dir msg) (print (thread-name (current-thread)) ": " dir ": " msg) ) (define (producer ch x) (make-thread (lambda () (say "writing" x) (mailbox-send! ch x) ) 'producer) ) (define (consumer ch) (make-thread (lambda () (let loop () (let ((x (mailbox-receive! ch))) (say "read" x) (unless (eq? 'quit x) (loop) ) ) ) ) 'consumer) ) (define (running-producer! ch x) (thread-start! (producer ch x))) (define (independent-producer! ch x) (thread-join! (running-producer! ch x))) (define (running-consumer! ch) (thread-start! (consumer ch))) (define PRODUCT-LINE '(good better best)) ;order/fulfillment pipeline (let ((ch (make-mailbox))) ;must have a customer before product (let ((th-consumer (running-consumer! ch))) ;now we can make something (for-each (cut independent-producer! ch <>) (append PRODUCT-LINE '(quit))) ;until satiated or the well runs dry (thread-join! th-consumer) ) )
Requirements
Notes
- 'mailbox-wait!, mailbox-receive!, and mailbox-cursor-next, are susceptible to deadlock. When a mailbox is empty the (current-thread) is suspended (indefinite block). Then the scheduler looks for the next ready thread. If one is not available it signals deadlock. Using a TIMEOUT will allow the calling thread to unblock eventually.
- A mailbox-cursor must live in an environment where mailbox entries are added and removed asynchronously. The cursor does not see a snapshot of the mailbox. The current state of the mailbox is queried.
Bugs & Limitations
- mailbox-wait!, mailbox-receive!, and mailbox-cursor-next must not be used with a timeout value when the (current-thread) is the ##sys#primordial-thread!
Author
Repository
This egg is hosted on the CHICKEN Subversion repository:
https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/mailbox
If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.
Version history
- 3.3.8
- Fix requirements.
- 3.3.7
- Fix maibox & maibox-cursor printing.
- 3.3.1
- Removed miscmacros requirement.
- 3.3.0
- Remove record-varinats requirement.
- 3.2.0
- Remove condition-utils & check-errors requirements.
- 3.0.0
- CHICKEN 5 release.
- 2.3.1
- Fix name type.
- 2.3.0
- Add types.
- 2.2.3
- 2.2.2
- Expand mailbox-timeout-condition.
- 2.2.1
- 2.2.0
- Included inline-type-checks.scm so all includes are egg-local.
- 2.1.3
- Fix for resuming a thread when mailbox empty; reported by Jeronimo Pellegrini. Added mailbox-cursor-unwound?. The mailbox-name can be an arbitrary Scheme object.
- 2.1.2
- 2.1.1
- 2.1.0
- Needs "check-errors" extension.
- 2.0.0
- Port to hygienic Chicken.
License
Copyright (c) 2003, Felix L. Winkelmann All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.