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 quotations
9 TUPLE: coroutine resumecc exitcc originalcc ;
11 : cocreate ( quot -- co )
13 dup current-coro associate
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). This shouldn't happen.
28 : coresume* ( v co -- ) coresume drop ; inline
29 : *coresume ( co -- result ) f swap coresume ; inline
31 : coyield ( v -- result )
34 [ continue-with ] curry
36 exitcc>> continue-with
39 : coyield* ( v -- ) coyield drop ; inline
40 : *coyield ( -- v ) f coyield ; inline
42 : coterminate ( v -- )
45 exitcc>> continue-with ;
49 originalcc>> >>resumecc
50 exitcc>> continue-with ;