[gambit-list] mailboxes

Marijn Schouten (hkBst) hkBst at gentoo.org
Mon Oct 12 05:55:56 EDT 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

lowly coder wrote:
> I realixe, per section 13.2,
> 
> 
>    Each thread has a "mailbox" which is used for inter-thread
> communication.
> 
> 
> 
> however, is there a builtin for global mailboxes (mailboxes taht are
> just variables; rather than mailboxes associated with threads)?
> 
> Thanks

Here is a simple implementation of a mailbox:

(define (make-pipe)
;  (display "Making pipe")(newline)
  (let ((cv (make-condition-variable))
        (mutex (make-mutex))
        (data (cons '() '())))
    (lambda args
      (case (car args)
        ((cv) cv)
        ((mutex) mutex)
        ((data) data)
        ((setdata) (set! data (cadr args)))))))

(define (with-lock mutex thunk)
   (dynamic-wind
       (lambda ()(mutex-lock! mutex))
       thunk
       (lambda ()(mutex-unlock! mutex))))

(define (write-pipe pipe val)
  (with-lock (pipe 'mutex)
             (lambda ()
               (set-cdr! (pipe 'data) (cons val (cdr (pipe 'data))))
               (condition-variable-signal! (pipe 'cv)))))

(define (read-pipe pipe)
;  (display "Reading pipe")(newline)
  (let ((front (car (pipe 'data))))
    (if (pair? front)
        (let ((ret (car front)))
          ;(display "object")
          (set-car! (pipe 'data) (cdr front))
          ret)
        (let loop ()
          (mutex-lock! (pipe 'mutex))
          (let ((back (cdr (pipe 'data))))
            (if (pair? back)
                (begin (pipe 'setdata (cons (reverse back) '()))
                       (mutex-unlock! (pipe 'mutex))
                       (read-pipe pipe))
                (begin (mutex-unlock! (pipe 'mutex) (pipe 'cv))
                       (loop))))))))



- --
If you cannot read my mind, then listen to what I say.

Marijn Schouten (hkBst), Gentoo Lisp project, Gentoo ML
<http://www.gentoo.org/proj/en/lisp/>, #gentoo-{lisp,ml} on FreeNode
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkrS/SwACgkQp/VmCx0OL2zThACfYWGVWwjC+4diN2yGHkV7MTqR
FtkAoJmgQ6ngBdZVh9EHkw9/Jud6cTDX
=fIO1
-----END PGP SIGNATURE-----


More information about the Gambit-list mailing list