chickadee » mailbox-threads

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

mailbox-threads

This extension extends srfi-18 in a way that mailboxes are attached to each thread. This makes sending messages to threads possible.

Documentation

interface

current-threadprocedure

Returns the current thread as an object, converting it to a mailbox-thread if necessary.

make-thread THUNK #!optional NAMEprocedure

Creates a mailbox thread that will run THUNK and names it after NAME.

thread-specific-set! THREAD OBJECTprocedure

Like srfi-18's procedure this one sets the thread-specific of THREAD to OBJECT.

thread-specific THREADprocedure

Returns the thread-specific object of THREAD.

thread-send THREAD MSGprocedure

Sends a MSG to THREAD's mailbox.

thread-start! THREADprocedure

Starts THREAD if it is a thread, if THREAD is a procedure make-thread is called first.

thread? THREADprocedure

Returns #t if THREAD is a mailbox-thread.

thread-receive #!optional TIMEOUT DEFAULTprocedure

Returns the next message of the current thread's mailbox. This will block until a message arrives unless TIMEOUT is given (for definition of time units see mailbox-receive! procedure). If the timeout is reached DEFAULT is returned.

thread-mailbox-nextprocedure
thread-mailbox-rewindprocedure
thread-mailbox-extract-and-rewindprocedure

Those work like their SRFI-18's brothers.

implementation

The thread's mailboxes are stored as the usual SRFI-18 thread-specific. To be able to tell mailbox-threads apart from SRFI-18 threads a tag will be added, so the specific looks like this:

('mboxthread specific mbox mbox-cursor)

Therefore the thread-specific and thread-specific-set! procedures are redefined in this module.

Some procedures will add these structures silently to existing threads, current-thread for example.

Example

(use mailbox-threads)

(define (t1-thunk)
   (let ((msg (thread-receive 999)))
      (if (equal? msg 'quit)

          (begin (print "Bye Ma!")
          (thread-terminate! (current-thread)))

          (begin
           (print msg)
          (t1-thunk)))))

(define t1 (make-thread t1-thunk 't1))

(thread-send t1 "Hi there!")
(thread-start! t1)
(thread-send t1 "still awake!")
(thread-send t1 "Look Ma! Threads with mailboxes!")
;(thread-send t1 'quit)
(thread-start! (lambda () (thread-send t1 "hi from two")))
(thread-specific t1)
(thread-specific-set! t1 "hi")
(thread-specific t1)

Requirements

/eggref/4/mailbox, /man/4/Unit srfi-18

Author

christian kellermann

License

Copyright (c) 2009, The CHICKEN Team
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.

Version History

1.4 ; Export current-thread procedure, found by megane
1.3 ; Fix returning and using a unspecified value. Thanks to the new scrutinizer.
1.2
Fix author entry, thanks to Peter Bex.
1.0
Initial release

Contents »