1 ! Copyright (C) 2005 Chris Double, 2007 Clemens Hofreither, 2008 James Cash.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel hashtables namespaces make continuations accessors ;
8 TUPLE: coroutine resumecc exitcc originalcc ;
10 : cocreate ( quot -- co )
12 dup current-coro associate
14 swapd , , \ with-variables ,
15 "Coroutine has terminated illegally." , \ throw ,
17 [ >>resumecc ] [ >>originalcc ] bi ;
19 : coresume ( v co -- result )
23 ! At this point, the coroutine quotation must have terminated
24 ! normally (without calling coyield, coreset, or coterminate).
25 ! This shouldn't happen.
29 : coresume* ( v co -- ) coresume drop ; inline
30 : *coresume ( co -- result ) f swap coresume ; inline
32 : coyield ( v -- result )
35 [ continue-with ] curry
37 exitcc>> continue-with
40 : coyield* ( v -- ) coyield drop ; inline
41 : *coyield ( -- v ) f coyield ; inline
43 : coterminate ( v -- )
46 exitcc>> continue-with ;
50 originalcc>> >>resumecc
51 exitcc>> continue-with ;