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 ! Wrap sub-primitives; we don't want them inlined into callers
13 ! since their behavior depends on what frames are on the callstack
14 : set-context ( obj context -- obj' )
17 : start-context ( obj quot: ( obj -- * ) -- obj' )
20 : set-context-and-delete ( obj context -- * )
21 (set-context-and-delete) ;
23 : start-context-and-delete ( obj quot: ( obj -- * ) -- * )
24 (start-context-and-delete) ;
26 ! Context introspection
27 : namestack-for ( context -- namestack )
28 [ 0 ] dip context-object-for ;
30 : catchstack-for ( context -- catchstack )
31 [ 1 ] dip context-object-for ;
33 : continuation-for ( context -- continuation )
40 } cleave <continuation> ;
44 SYMBOL: initial-thread
48 { quot callable initial: [ ] }
49 { exit-handler callable initial: [ ] }
55 { variables hashtable }
59 63 special-object { thread } declare ; inline
61 : thread-continuation ( thread -- continuation )
62 context>> check-box value>> continuation-for ;
64 ! Thread-local storage
65 : tnamespace ( -- assoc )
66 self variables>> ; inline
68 : tget ( key -- value )
71 : tset ( value key -- )
74 : tchange ( key quot -- )
75 [ tnamespace ] dip change-at ; inline
77 : threads ( -- assoc )
78 64 special-object { hashtable } declare ; inline
80 : thread-registered? ( thread -- ? )
85 : register-thread ( thread -- )
86 dup id>> threads set-at ;
88 : unregister-thread ( thread -- )
89 id>> threads delete-at ;
91 : set-self ( thread -- ) 63 set-special-object ; inline
95 : run-queue ( -- dlist )
96 65 special-object { dlist } declare ; inline
98 : sleep-queue ( -- heap )
99 66 special-object { min-heap } declare ; inline
101 : new-thread ( quot name class -- thread )
105 \ thread counter >>id
106 H{ } clone >>variables
107 <box> >>context ; inline
109 : <thread> ( quot name -- thread )
110 \ thread new-thread ;
112 : resume ( thread -- )
113 f >>state run-queue push-front ;
115 : resume-now ( thread -- )
116 f >>state run-queue push-back ;
118 : resume-with ( obj thread -- )
119 f >>state 2array run-queue push-front ;
121 : sleep-time ( -- nanos/f )
123 { [ run-queue deque-empty? not ] [ 0 ] }
124 { [ sleep-queue heap-empty? ] [ f ] }
125 [ sleep-queue heap-peek nip nano-count [-] ]
128 : interrupt ( thread -- )
130 dup sleep-entry>> [ sleep-queue heap-delete ] when*
139 : schedule-sleep ( thread dt -- )
140 dupd sleep-queue heap-push* >>sleep-entry drop ;
142 : expire-sleep? ( -- ? )
143 sleep-queue dup heap-empty?
144 [ drop f ] [ heap-peek nip nano-count <= ] if ;
146 : expire-sleep ( thread -- )
147 f >>sleep-entry resume ;
149 : expire-sleep-loop ( -- )
151 [ sleep-queue heap-pop drop expire-sleep ]
162 : no-runnable-threads ( -- ) die ;
164 GENERIC: (next) ( obj thread -- obj' )
168 [ context>> box> set-context ]
169 [ t >>runnable drop [start] start-context ] if ;
171 : (stop) ( obj thread -- * )
173 [ context>> box> set-context-and-delete ]
174 [ t >>runnable drop [start] start-context-and-delete ] if ;
176 : next ( -- obj thread )
179 dup array? [ first2 ] [ [ f ] dip ] if
186 self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi
189 : suspend ( state -- obj )
191 [ context ] dip context>> >box
194 : yield ( -- ) self resume f suspend drop ;
196 GENERIC: sleep-until ( n/f -- )
198 M: integer sleep-until
199 [ self ] dip schedule-sleep "sleep" suspend drop ;
202 drop "standby" suspend drop ;
204 GENERIC: sleep ( dt -- )
207 >integer nano-count + sleep-until ;
209 : (spawn) ( thread -- )
210 [ register-thread ] [ [ namestack ] dip resume-with ] bi ;
212 : spawn ( quot name -- thread )
213 <thread> [ (spawn) ] keep ;
215 : spawn-server ( quot name -- thread )
216 [ '[ _ loop ] ] dip spawn ;
218 : in-thread ( quot -- )
220 '[ _ set-datastack @ ]
221 "Thread" spawn drop ;
223 GENERIC: error-in-thread ( error thread -- )
227 : init-thread-state ( -- )
228 H{ } clone 64 set-special-object
229 <dlist> 65 set-special-object
230 <min-heap> 66 set-special-object ;
232 : init-initial-thread ( -- )
233 [ ] "Initial" <thread>
235 [ initial-thread set-global ]
240 : init-threads ( -- )
242 init-initial-thread ;
246 [ init-threads ] "threads" add-startup-hook