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 combinators.private init boxes
7 accessors math.order 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) ;
16 : start-context ( obj quot: ( obj -- * ) -- obj' ) (start-context) ;
20 SYMBOL: initial-thread
24 { quot callable initial: [ ] }
25 { exit-handler callable initial: [ ] }
31 { variables hashtable }
35 63 special-object { thread } declare ; inline
37 ! Thread-local storage
38 : tnamespace ( -- assoc )
39 self variables>> [ H{ } clone dup self (>>variables) ] unless* ;
41 : tget ( key -- value )
44 : tset ( value key -- )
47 : tchange ( key quot -- )
48 tnamespace swap change-at ; inline
50 : threads ( -- assoc )
51 64 special-object { hashtable } declare ; inline
53 : thread ( id -- thread )
56 : thread-registered? ( thread -- ? )
59 ERROR: already-stopped thread ;
61 : check-unregistered ( thread -- thread )
62 dup thread-registered? [ already-stopped ] when ;
64 ERROR: not-running thread ;
66 : check-registered ( thread -- thread )
67 dup thread-registered? [ not-running ] unless ;
71 : register-thread ( thread -- )
72 check-unregistered dup id>> threads set-at ;
74 : unregister-thread ( thread -- )
75 check-registered id>> threads delete-at ;
77 : set-self ( thread -- ) 63 set-special-object ; inline
81 : new-thread ( quot name class -- thread )
86 <box> >>continuation ; inline
88 : <thread> ( quot name -- thread )
91 : run-queue ( -- dlist )
92 65 special-object { dlist } declare ; inline
94 : sleep-queue ( -- heap )
95 66 special-object { dlist } declare ; inline
97 : resume ( thread -- )
99 check-registered run-queue push-front ;
101 : resume-now ( thread -- )
103 check-registered run-queue push-back ;
105 : resume-with ( obj thread -- )
107 check-registered 2array run-queue push-front ;
109 : sleep-time ( -- nanos/f )
111 { [ run-queue deque-empty? not ] [ 0 ] }
112 { [ sleep-queue heap-empty? ] [ f ] }
113 [ sleep-queue heap-peek nip nano-count [-] ]
120 : schedule-sleep ( thread dt -- )
121 [ check-registered dup ] dip sleep-queue heap-push*
124 : expire-sleep? ( heap -- ? )
126 [ drop f ] [ heap-peek nip nano-count <= ] if ;
128 : expire-sleep ( thread -- )
129 f >>sleep-entry resume ;
131 : expire-sleep-loop ( -- )
133 [ dup expire-sleep? ]
134 [ dup heap-pop drop expire-sleep ]
138 : start ( namestack thread -- * )
145 self quot>> [ call stop ] call-clear
146 ] (( namestack thread -- * )) call-effect-unsafe ;
150 : no-runnable-threads ( -- * )
151 ! We should never be in a state where the only threads
152 ! are sleeping; the I/O wait thread is always runnable.
153 ! However, if it dies, we handle this case
156 ! And if sleep-time outputs f, there are no sleeping
157 ! threads either... so WTF.
159 { [ dup not ] [ drop die ] }
160 { [ dup 0 = ] [ drop ] }
164 : (next) ( arg thread -- * )
168 continuation>> box> continue-with
175 run-queue dup deque-empty? [
176 drop no-runnable-threads
178 pop-back dup array? [ first2 ] [ f swap ] if (next)
184 self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi next ;
186 : suspend ( state -- obj )
188 [ self continuation>> >box next ] callcc1 ; inline
190 : yield ( -- ) self resume f suspend drop ;
192 GENERIC: sleep-until ( n/f -- )
194 M: integer sleep-until
195 [ self ] dip schedule-sleep "sleep" suspend drop ;
198 drop "interrupt" suspend drop ;
200 GENERIC: sleep ( dt -- )
203 >integer nano-count + sleep-until ;
205 : interrupt ( thread -- )
207 dup sleep-entry>> [ sleep-queue heap-delete ] when*
212 : (spawn) ( thread -- )
213 [ register-thread ] [ namestack swap resume-with ] bi ;
215 : spawn ( quot name -- thread )
216 <thread> [ (spawn) ] keep ;
218 : spawn-server ( quot name -- thread )
219 [ '[ _ loop ] ] dip spawn ;
221 : in-thread ( quot -- )
223 '[ _ set-datastack @ ]
224 "Thread" spawn drop ;
226 GENERIC: error-in-thread ( error thread -- )
230 : init-threads ( -- )
231 H{ } clone 64 set-special-object
232 <dlist> 65 set-special-object
233 <min-heap> 66 set-special-object
234 initial-thread global
235 [ drop [ ] "Initial" <thread> ] cache
244 [ init-threads ] "threads" add-startup-hook