1 ! Copyright (C) 2004, 2011 Slava Pestov.
2 ! Copyright (C) 2005 Mackenzie Straight.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: alien.private arrays hashtables heaps kernel kernel.private
5 math namespaces sequences vectors continuations continuations.private
6 dlists assocs system combinators init boxes accessors math.order
7 deques strings quotations fry ;
8 FROM: assocs => change-at ;
13 ! Wrap sub-primitives; we don't want them inlined into callers
14 ! since their behavior depends on what frames are on the callstack
15 : context ( -- context )
16 2 context-object ; inline
18 : set-context ( obj context -- obj' )
19 (set-context) ; inline
21 : start-context ( obj quot: ( obj -- * ) -- obj' )
22 (start-context) ; inline
24 : set-context-and-delete ( obj context -- * )
25 (set-context-and-delete) ; inline
27 : start-context-and-delete ( obj quot: ( obj -- * ) -- * )
28 (start-context-and-delete) ; inline
30 ! Context introspection
31 : namestack-for ( context -- namestack )
32 [ 0 ] dip context-object-for ;
34 : catchstack-for ( context -- catchstack )
35 [ 1 ] dip context-object-for ;
37 : continuation-for ( context -- continuation )
44 } cleave <continuation> ;
48 SYMBOL: initial-thread
52 { quot callable initial: [ ] }
53 { exit-handler callable initial: [ ] }
59 { variables hashtable }
63 63 special-object { thread } declare ; inline
65 : thread-continuation ( thread -- continuation )
66 context>> check-box value>> continuation-for ;
68 ! Thread-local storage
69 : tnamespace ( -- assoc )
70 self variables>> ; inline
72 : tget ( key -- value )
75 : tset ( value key -- )
78 : tchange ( ..a key quot: ( ..a value -- ..b newvalue ) -- ..b )
79 [ tnamespace ] dip change-at ; inline
81 : threads ( -- assoc )
82 64 special-object { hashtable } declare ; inline
84 : thread-registered? ( thread -- ? )
89 : register-thread ( thread -- )
90 dup id>> threads set-at ;
92 : unregister-thread ( thread -- )
93 id>> threads delete-at ;
95 : set-self ( thread -- ) 63 set-special-object ; inline
99 : run-queue ( -- dlist )
100 65 special-object { dlist } declare ; inline
102 : sleep-queue ( -- heap )
103 66 special-object { min-heap } declare ; inline
105 : waiting-callbacks ( -- assoc )
106 68 special-object { hashtable } declare ; inline
108 : new-thread ( quot name class -- thread )
112 \ thread counter >>id
113 H{ } clone >>variables
114 <box> >>context ; inline
116 : <thread> ( quot name -- thread )
117 \ thread new-thread ;
119 : resume ( thread -- )
120 f >>state run-queue push-front ;
122 : resume-now ( thread -- )
123 f >>state run-queue push-back ;
125 : resume-with ( obj thread -- )
126 f >>state 2array run-queue push-front ;
128 : sleep-time ( -- nanos/f )
130 { [ current-callback waiting-callbacks key? ] [ 0 ] }
131 { [ run-queue deque-empty? not ] [ 0 ] }
132 { [ sleep-queue heap-empty? ] [ f ] }
133 [ sleep-queue heap-peek nip nano-count [-] ]
136 : interrupt ( thread -- )
138 dup sleep-entry>> [ sleep-queue heap-delete ] when*
147 : schedule-sleep ( thread dt -- )
148 dupd sleep-queue heap-push* >>sleep-entry drop ;
150 : expire-sleep? ( -- ? )
151 sleep-queue dup heap-empty?
152 [ drop f ] [ heap-peek nip nano-count <= ] if ;
154 : expire-sleep ( thread -- )
155 f >>sleep-entry resume ;
157 : expire-sleep-loop ( -- )
159 [ sleep-queue heap-pop drop expire-sleep ]
170 : no-runnable-threads ( -- ) die ;
172 GENERIC: (next) ( obj thread -- obj' )
176 [ context>> box> set-context ]
177 [ t >>runnable drop [start] start-context ] if ;
179 : (stop) ( obj thread -- * )
181 [ context>> box> set-context-and-delete ]
182 [ t >>runnable drop [start] start-context-and-delete ] if ;
184 : wake-up-callbacks ( -- )
185 current-callback waiting-callbacks delete-at*
186 [ resume-now ] [ drop ] if ;
188 : next ( -- obj thread )
192 dup array? [ first2 ] [ [ f ] dip ] if
199 self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi
202 : suspend ( state -- obj )
204 [ context ] dip context>> >box
207 : yield ( -- ) self resume f suspend drop ;
209 GENERIC: sleep-until ( n/f -- )
211 M: integer sleep-until
212 [ self ] dip schedule-sleep "sleep" suspend drop ;
215 drop "standby" suspend drop ;
217 GENERIC: sleep ( dt -- )
220 >integer nano-count + sleep-until ;
222 : (spawn) ( thread -- )
223 [ register-thread ] [ [ namestack ] dip resume-with ] bi ;
225 : spawn ( quot name -- thread )
226 <thread> [ (spawn) ] keep ;
228 : spawn-server ( quot name -- thread )
229 [ '[ _ loop ] ] dip spawn ;
231 : in-thread ( quot -- )
233 '[ _ set-datastack @ ]
234 "Thread" spawn drop ;
238 : init-thread-state ( -- )
239 H{ } clone 64 set-special-object
240 <dlist> 65 set-special-object
241 <min-heap> 66 set-special-object
242 H{ } clone 68 set-special-object ;
244 : init-initial-thread ( -- )
245 [ ] "Initial" <thread>
247 [ initial-thread set-global ]
252 : init-threads ( -- )
254 init-initial-thread ;
256 : wait-for-callback ( callback -- )
257 self swap waiting-callbacks set-at
258 "Callback return" suspend drop ;
262 [ init-threads ] "threads" add-startup-hook