1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! Copyright (C) 2005 Mackenzie Straight.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: arrays hashtables heaps kernel kernel.private math
5 namespaces sequences vectors continuations continuations.private
6 dlists assocs system combinators init boxes accessors math.order
7 deques strings quotations fry ;
12 ! (set-context) and (start-context) are sub-primitives, but
13 ! we don't want them inlined into callers since their behavior
14 ! depends on what frames are on the callstack
15 : set-context ( obj context -- obj' ) (set-context) ;
17 : start-context ( obj quot: ( obj -- * ) -- obj' ) (start-context) ;
19 : namestack-for ( context -- namestack )
20 [ 0 ] dip context-object-for ;
22 : catchstack-for ( context -- catchstack )
23 [ 1 ] dip context-object-for ;
25 : continuation-for ( context -- continuation )
32 } cleave <continuation> ;
36 SYMBOL: initial-thread
40 { quot callable initial: [ ] }
41 { exit-handler callable initial: [ ] }
47 { variables hashtable }
51 63 special-object { thread } declare ; inline
53 : thread-continuation ( thread -- continuation )
54 context>> check-box value>> continuation-for ;
56 ! Thread-local storage
57 : tnamespace ( -- assoc )
58 self variables>> ; inline
60 : tget ( key -- value )
63 : tset ( value key -- )
66 : tchange ( key quot -- )
67 [ tnamespace ] dip change-at ; inline
69 : threads ( -- assoc )
70 64 special-object { hashtable } declare ; inline
72 : thread-registered? ( thread -- ? )
75 ERROR: already-stopped thread ;
77 : check-unregistered ( thread -- thread )
78 dup thread-registered? [ already-stopped ] when ;
80 ERROR: not-running thread ;
82 : check-registered ( thread -- thread )
83 dup thread-registered? [ not-running ] unless ;
87 : register-thread ( thread -- )
88 check-unregistered dup id>> threads set-at ;
90 : unregister-thread ( thread -- )
91 check-registered id>> threads delete-at ;
93 : set-self ( thread -- ) 63 set-special-object ; inline
97 : run-queue ( -- dlist )
98 65 special-object { dlist } declare ; inline
100 : sleep-queue ( -- heap )
101 66 special-object { dlist } declare ; inline
103 : new-thread ( quot name class -- thread )
107 \ thread counter >>id
108 H{ } clone >>variables
109 <box> >>context ; inline
111 : <thread> ( quot name -- thread )
112 \ thread new-thread ;
114 : resume ( thread -- )
116 check-registered run-queue push-front ;
118 : resume-now ( thread -- )
120 check-registered run-queue push-back ;
122 : resume-with ( obj thread -- )
124 check-registered 2array run-queue push-front ;
126 : sleep-time ( -- nanos/f )
128 { [ run-queue deque-empty? not ] [ 0 ] }
129 { [ sleep-queue heap-empty? ] [ f ] }
130 [ sleep-queue heap-peek nip nano-count [-] ]
133 : interrupt ( thread -- )
135 dup sleep-entry>> [ sleep-queue heap-delete ] when*
144 : schedule-sleep ( thread dt -- )
145 [ check-registered dup ] dip sleep-queue heap-push*
148 : expire-sleep? ( heap -- ? )
150 [ drop f ] [ heap-peek nip nano-count <= ] if ;
152 : expire-sleep ( thread -- )
153 f >>sleep-entry resume ;
155 : expire-sleep-loop ( -- )
157 [ dup expire-sleep? ]
158 [ dup heap-pop drop expire-sleep ]
162 : start ( namestack -- obj )
172 : no-runnable-threads ( -- obj )
173 ! We should never be in a state where the only threads
174 ! are sleeping; the I/O wait thread is always runnable.
175 ! However, if it dies, we handle this case
178 ! And if sleep-time outputs f, there are no sleeping
179 ! threads either... so WTF.
181 { [ dup not ] [ drop die ] }
182 { [ dup 0 = ] [ drop ] }
186 : (next) ( obj thread -- obj' )
190 [ context>> box> set-context ] [ t >>runnable drop start ] if ;
194 run-queue dup deque-empty?
195 [ drop no-runnable-threads ]
196 [ pop-back dup array? [ first2 ] [ [ f ] dip ] if (next) ] if ;
198 : recycler-thread ( -- thread ) 68 special-object ;
200 : recycler-queue ( -- vector ) 69 special-object ;
202 : delete-context-later ( context -- )
203 recycler-queue push recycler-thread interrupt ;
208 self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi
209 context delete-context-later next
212 : suspend ( state -- obj )
214 [ context ] dip context>> >box
217 : yield ( -- ) self resume f suspend drop ;
219 GENERIC: sleep-until ( n/f -- )
221 M: integer sleep-until
222 [ self ] dip schedule-sleep "sleep" suspend drop ;
225 drop "standby" suspend drop ;
227 GENERIC: sleep ( dt -- )
230 >integer nano-count + sleep-until ;
232 : (spawn) ( thread -- )
233 [ register-thread ] [ [ namestack ] dip resume-with ] bi ;
235 : spawn ( quot name -- thread )
236 <thread> [ (spawn) ] keep ;
238 : spawn-server ( quot name -- thread )
239 [ '[ _ loop ] ] dip spawn ;
241 : in-thread ( quot -- )
243 '[ _ set-datastack @ ]
244 "Thread" spawn drop ;
246 GENERIC: error-in-thread ( error thread -- )
250 : init-thread-state ( -- )
251 H{ } clone 64 set-special-object
252 <dlist> 65 set-special-object
253 <min-heap> 66 set-special-object ;
255 : init-initial-thread ( -- )
256 [ ] "Initial" <thread>
258 [ initial-thread set-global ]
263 ! The recycler thread deletes contexts belonging to stopped
266 : recycler-loop ( -- )
267 recycler-queue [ [ delete-context ] each ] [ delete-all ] bi
271 : init-recycler ( -- )
272 [ recycler-loop ] "Context recycler" spawn 68 set-special-object
273 V{ } clone 69 set-special-object ;
275 : init-threads ( -- )
282 [ init-threads ] "threads" add-startup-hook