Previous: , Up: Vats   [Contents]


5.2.2 Rolling your own vats

Goblins comes with a built-in vat implementation that is general-purpose and useful in most situations. However, using Goblins with a foreign event loop (such as a GUI widget toolkit or game engine) calls for a specialized vat that knows how to hook into it. The API described below is considered unstable and likely to change in future releases of Goblins, so be prepared to update custom vat code at a later date.

The make-vat constructor is used to make specialized vats. Below is some pseudocode that illustrates the basic pattern:

> (define (make-custom-vat)
    (define thread #f)
    (define (start churn)
      (define (loop)
        (churn (dequeue-msg))
        (loop))
      (set! thread (call-with-new-thread loop)))
    (define (halt)
      (cancel-thread thread))
    (define (send msg return?)
      (if return?
          (enqueue-msg-and-wait-for-result msg)
          (enqueue-msg msg)))
    (make-vat #:start start
              #:halt halt
              #:send send))

How to implement enqueue-msg, enqueue-msg-and-wait-for-result, and dequeue-msg depends on the integration target and is left as an exercise for the implementer.

Vat implementations must provide three hook procedures to make-vat:

Custom vat implementations must enqueue and dequeue messages in a thread-safe manner because incoming messages from other vats may be, and likely are, running in a different thread.

The vat-start! procedure initiates the processing of messages sent to the vat.

> (define custom-vat (make-custom-vat))
> (vat-start! custom-vat)

Once a custom vat has been started, it can be used as usual:

> (define alice
    (with-vat custom-vat
     (spawn ^greeter "Alice")))

Previous: , Up: Vats   [Contents]