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>> ; inline
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 )
87 H{ } clone >>variables ; inline
89 : <thread> ( quot name -- thread )
92 : run-queue ( -- dlist )
93 65 special-object { dlist } declare ; inline
95 : sleep-queue ( -- heap )
96 66 special-object { dlist } declare ; inline
98 : resume ( thread -- )
100 check-registered run-queue push-front ;
102 : resume-now ( thread -- )
104 check-registered run-queue push-back ;
106 : resume-with ( obj thread -- )
108 check-registered 2array run-queue push-front ;
110 : sleep-time ( -- nanos/f )
112 { [ run-queue deque-empty? not ] [ 0 ] }
113 { [ sleep-queue heap-empty? ] [ f ] }
114 [ sleep-queue heap-peek nip nano-count [-] ]
121 : schedule-sleep ( thread dt -- )
122 [ check-registered dup ] dip sleep-queue heap-push*
125 : expire-sleep? ( heap -- ? )
127 [ drop f ] [ heap-peek nip nano-count <= ] if ;
129 : expire-sleep ( thread -- )
130 f >>sleep-entry resume ;
132 : expire-sleep-loop ( -- )
134 [ dup expire-sleep? ]
135 [ dup heap-pop drop expire-sleep ]
139 : start ( namestack thread -- * )
146 self quot>> [ call stop ] call-clear
147 ] (( namestack thread -- * )) call-effect-unsafe ;
151 : no-runnable-threads ( -- * )
152 ! We should never be in a state where the only threads
153 ! are sleeping; the I/O wait thread is always runnable.
154 ! However, if it dies, we handle this case
157 ! And if sleep-time outputs f, there are no sleeping
158 ! threads either... so WTF.
160 { [ dup not ] [ drop die ] }
161 { [ dup 0 = ] [ drop ] }
165 : (next) ( arg thread -- * )
169 continuation>> box> continue-with
176 run-queue dup deque-empty? [
177 drop no-runnable-threads
179 pop-back dup array? [ first2 ] [ f swap ] if (next)
185 self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi next ;
187 : suspend ( state -- obj )
189 [ self continuation>> >box next ] callcc1 ; inline
191 : yield ( -- ) self resume f suspend drop ;
193 GENERIC: sleep-until ( n/f -- )
195 M: integer sleep-until
196 [ self ] dip schedule-sleep "sleep" suspend drop ;
199 drop "interrupt" suspend drop ;
201 GENERIC: sleep ( dt -- )
204 >integer nano-count + sleep-until ;
206 : interrupt ( thread -- )
208 dup sleep-entry>> [ sleep-queue heap-delete ] when*
213 : (spawn) ( thread -- )
214 [ register-thread ] [ namestack swap resume-with ] bi ;
216 : spawn ( quot name -- thread )
217 <thread> [ (spawn) ] keep ;
219 : spawn-server ( quot name -- thread )
220 [ '[ _ loop ] ] dip spawn ;
222 : in-thread ( quot -- )
224 '[ _ set-datastack @ ]
225 "Thread" spawn drop ;
227 GENERIC: error-in-thread ( error thread -- )
231 : init-threads ( -- )
232 H{ } clone 64 set-special-object
233 <dlist> 65 set-special-object
234 <min-heap> 66 set-special-object
235 initial-thread global
236 [ drop [ ] "Initial" <thread> ] cache
245 [ init-threads ] "threads" add-startup-hook