((section 2 "Outdated egg!" (p "This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for " (int-link "/eggref/5/termite" "the CHICKEN 5 version of this egg") ", if it exists.") (p "If it does not exist, there may be equivalent functionality provided by another egg; have a look at the " (link "https://wiki.call-cc.org/chicken-projects/egg-index-5.html" "egg index") ". Otherwise, please consider porting this egg to the current version of CHICKEN.")) (section 2 "termite" (p "Erlang style concurrency for Chicken Scheme") (toc)) (section 2 "Documentation" (section 3 "Fundamental Operations" (section 4 "spawn" (def (sig (procedure "(spawn THUNK)" (id spawn))) (p "Create a process running " (tt "thunk") " and returns its " (tt "pid") "."))) (section 4 "!" (def (sig (procedure "(! PID MSG)" (id !))) (p "Send " (tt "msg") " to process " (tt "pid") "."))) (section 4 "?" (def (sig (procedure "(? [TIMEOUT [DEFAULT]])" (id ?))) (p "Fetch a message from the mailbox.  " (tt "timeout") " is in seconds."))) (section 4 "??" (def (sig (procedure "(?? PRED)" (id ??))) (p "Fetches next message that matches " (tt "pred") "."))) (section 4 "!?" (def (sig (procedure "(!? PID MSG [TIMEOUT [DEFAULT]])" (id !?))) (p "Send " (tt "msg") " to process " (tt "pid") " and wait for a reply."))) (section 4 "recv" (def (sig (procedure "(recv ((PAT VAR ACTION) [...]) [(after TIMEOUT TMO-ACTION)])" (id recv))) (p "Receive a message, when it matches " (tt "pat") " put it in " (tt "var") " and take " (tt "action") " (there can be many of these).  Optionally, do " (tt "tmo-action") " after " (tt "timeout") ".") (p "(This was documented from the termite.pdf, but it's not exported in chicken's termite.)"))) (section 4 "spawn-link" (def (sig (procedure "(spawn-link THUNK)" (id spawn-link))) (p "Create a process running " (tt "thunk") " and returns its " (tt "pid") " and link to it.  When either process dies, and exception is raised in the other process."))) (section 4 "spawn-linked-to" (def (sig (procedure "(spawn-linked-to THUNK TO)" (id spawn-linked-to))) (p "Create a process running " (tt "thunk") " and returns its " (tt "pid") " and have it link to " (tt "to") " (a pid).  When either process dies, and exception is raised in the other process.")))) (section 3 "Distributed Operations" (section 4 "make-node" (def (sig (procedure "(make-node HOST PORT)" (id make-node))) (p "Creates a node object for " (tt "host:port") " and returns the node object."))) (section 4 "node-init" (def (sig (procedure "(node-init NODE)" (id node-init))) (p "Primary setup for creating a " (tt "node") " to accept processes."))) (section 4 "remote-spawn" (def (sig (procedure "(remote-spawn NODE THUNK)" (id remote-spawn))) (p "Run " (tt "thunk") " on " (tt "node") ", returns " (tt "pid") ".  Node needs to have done a " (tt "node-init") "."))) (section 4 "remote-spawn-link" (def (sig (procedure "(remote-spawn-link NODE THUNK)" (id remote-spawn-link))) (p "Run " (tt "thunk") " on " (tt "node") " and link to it, returns " (tt "pid") "."))) (section 4 "on" (def (sig (procedure "(on NODE THUNK)" (id on))) (p "Run " (tt "thunk") " on " (tt "node") ", returns result of " (tt "thunk") ".")))) (section 3 "Error Handling" (section 4 "with-exception-catcher" (def (sig (procedure "(with-exception-catcher HANDLER BODY)" (id with-exception-catcher))) (p "Installs a dynamically scoped exception handler " (tt "handler") " for the duration of " (tt "body") ".  Resumes execution a the point where the handler was installed."))) (section 4 "with-exception-handler" (def (sig (procedure "(with-exception-handler HANDLER BODY)" (id with-exception-handler))) (p "Installs a dynamically scoped exception handler " (tt "handler") " for the duration of " (tt "body") ".  Resumes execution a the point where the exception was raised."))) (section 4 "raise" (def (sig (procedure "(raise EXCEPTION)" (id raise))) (p "Raises " (tt "exception") "."))) (section 4 "catch" (def (sig (procedure "(catch HANDLER BODY)" (id catch))) (p "Catches an exception with " (tt "handler") " during execution of " (tt "body") ".")))) (section 3 "Stopping" (section 4 "shutdown!" (def (sig (procedure "(shutdown!)" (id shutdown!))) (p "Stops gracefully.  Linked processes receive a \"normal\" exit message."))) (section 4 "halt!" (def (sig (procedure "(halt!)" (id halt!))) (p "Stops ungracefully.  Linked processes are clueless as to what happened."))) (section 4 "terminate!" (def (sig (procedure "(terminate! VICTIM)" (id terminate!))) (p "Forcefully terminate a local(!) process.  Processed linked to " (tt "victim") " get a " (tt "terminated") " message."))))) (section 2 "Examples" (section 3 "Making a \"server\" process" (pre "(define pong-server\n  (spawn\n    (lambda ()\n      (let loop ()\n        (let ((msg (?)))\n          (if (and (list? msg)\n                   (= (length msg) 2)\n                   (pid? (car msg))\n                   (eq? (cadr msg) 'ping))\n            (let ((from (car msg)))\n              (! from 'pong)\n              (loop))\n            (loop)))))))\n\n(! pong-server (list (self) 'ping))\n\n(?)                 --> pong")) (section 3 "Selective message retrieval" (pre "(! (self) 1)\n(! (self) 2)\n(! (self) 3)\n\n(?)                 --> 1\n(?? odd?)           --> 3\n(?)                 --> 2")) (section 3 "Distributed processing" (p "On node a:") (pre "(node-init (make-node \"a.example.org\" 12345))") (p "On node b:") (pre "(define node-a (make-node \"a.example.org\" 12345))\n(on node-a host-name)    --> \"a.example.org\"") (pre "(define p (remote-spawn node-a\n                        (lambda ()\n                         (let loop ()\n                          (let ((x (?)))\n                            (! (car x) ((cdr x)))\n                            (loop))))))\n(! p (cons (self) host-name))\n(?)                      --> \"a.example.org\""))) (section 2 "Notes" (p "There's a " (link "http://termite.googlecode.com/files/termite.pdf" "paper") " about Termite.")) (section 2 "Requirements" (p (int-link "/eggref/4/s11n" "s11n") " " (int-link "/eggref/4/mailbox" "mailbox") " mailbox-threads " (int-link "/eggref/4/defstruct" "defstruct"))) (section 2 "Bugs and Limitations") (section 2 "Author" (p "Gulliaume Germain, ported to Chicken by Christian Kellermann")) (section 2 "Version History") (section 2 "License" (p "Copyright (c) 2005-2008, Guillaume Germain<br> Termite is licensed under the same terms as Gambit-C (LGPL and Apache v.2)<br> It comes with absolutely no warranty of any kind.<br> LGPL 2.1")))