You are looking at historical revision 19388 of this page. It may differ significantly from its current revision.

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

[procedure] (current-thread)

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

[procedure] (make-thread THUNK [NAME])

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

[procedure] (thread-specific-set! THREAD OBJECT)

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

[procedure] (thread-specific THREAD)

Returns the thread-specific object of THREAD.

[procedure] (thread-send THREAD MSG)

Sends a MSG to THREAD's mailbox.

[procedure] (thread-start! THREAD)

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

[procedure] (thread? THREAD)

Returns #t if THREAD is a mailbox-thread.

[procedure] (thread-receive [TIMEOUT [DEFAULT]])

Returns the next message of the current thread's mailbox. This will block until a message arrives unless TIMEOUT is given. If the timeout is reached DEFAULT is returned.

[procedure] thread-mailbox-next
[procedure] thread-mailbox-rewind
[procedure] thread-mailbox-extract-and-rewind

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.0
Initial release