chickadee » mailbox

mailbox

Documentation

Thread-safe queues with timeout.

mailbox-timeout-exception?

(mailbox-timeout-exception? *) => boolean procedure

Is the * a mailbox timeout exception?

A mailbox timeout exception is a composite condition of 'exn, 'mailbox, and 'timeout, with properties of 'location and 'arguments.

make-mailbox

(make-mailbox [NAME]) => mailbox procedure

Returns a new mailbox object. NAME is an optional object to identify this mailbox and defaults to some gensym'd symbol.

mailbox?

(mailbox? *) => boolean procedure

Is the * a mailbox?

mailbox-name

(mailbox-name MAILBOX) => * procedure

Returns the name of the MAILBOX.

mailbox-empty?

(mailbox-empty? MAILBOX) => boolean procedure

If there are no queued objects in the MAILBOX, then this procedure returns #t, otherwise it returns #f.

mailbox-count

(mailbox-count MAILBOX) => integer procedure

Returns the number of queued objects for the MAILBOX.

mailbox-waiting?

(mailbox-waiting? MAILBOX) => boolean procedure

Is any thread waiting for the MAILBOX?

mailbox-waiters

(mailbox-waiters MAILBOX) => list procedure

Returns a list of the threads waiting for the MAILBOX.

mailbox-send!

(mailbox-send! MAILBOX X) procedure

Queues the data object X. 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!

(mailbox-receive! MAILBOX [TIMEOUT [DEFAULT]]) => * procedure

If there is any data in the 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!

(mailbox-wait! MAILBOX [TIMEOUT]) procedure

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!

(mailbox-push-back! MAILBOX X) procedure

Pushes the data object X into the first position of a mailbox.

mailbox-push-back-list!

(mailbox-push-back-list! MAILBOX XS) procedure

Pushes the list of objects XS back into the mailbox, so that (car XS) becomes the next receivable item.

make-mailbox-cursor

(make-mailbox-cursor MAILBOX) => mailbox-cursor procedure

Returns an object which can enumerate a mailbox.

Multiple cursors can scan, and mutate, the same mailbox.

mailbox-cursor?

(mailbox-cursor? *) => boolean procedure

Is the * a mailbox-cursor?

mailbox-cursor-mailbox

(mailbox-cursor-mailbox MAILBOX-CURSOR) => mailbox procedure

Returns the mailbox object associated with the mailbox cursor.

mailbox-cursor-next

(mailbox-cursor-next MAILBOX-CURSOR [TIMEOUT [DEFAULT]]) => * procedure

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

(mailbox-cursor-rewind MAILBOX-CURSOR) procedure

Position the cursor at the oldest message in the mailbox.

mailbox-cursor-extract-and-rewind

(mailbox-cursor-extract-and-rewind MAILBOX-CURSOR) procedure

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?

(mailbox-cursor-rewound? MAILBOX-CURSOR) => boolean procedure

Is the MAILBOX-CURSOR positioned at the start of the mailbox queue?

mailbox-cursor-more?

(mailbox-cursor-unwound? MAILBOX-CURSOR) => boolean procedure

Is the MAILBOX-CURSOR positioned at the end of the mailbox queue?

Usage

(require-extension mailbox)

or

(require-library mailbox)
...
(import mailbox)

Examples

(define (consumer ch)
  (make-thread
    (lambda ()
      (let loop ()
        (print (current-thread) ": reading " (mailbox-receive! ch))
        (loop) ) ) ) )

(define ch (make-mailbox))
(thread-start! (consumer ch))
(for-each
  (lambda (x)
    (print (current-thread) ": writing " x)
    (mailbox-send! ch x) )
  '(33 44 55 hello) )

Notes

Using a timeout will allow the calling thread to unblock eventually.

Author

felix winkelmann

Kon Lovett

Requirements

check-errors

Version history

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?. mailbox-name can be 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.

Contents »