1 ! Copyright (C) 2004, 2008 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
10 SYMBOL: initial-thread
13 name quot exit-handler
15 continuation state runnable
16 mailbox variables sleep-entry ;
18 : self ( -- thread ) 63 getenv ; inline
20 ! Thread-local storage
21 : tnamespace ( -- assoc )
22 self variables>> [ H{ } clone dup self (>>variables) ] unless* ;
24 : tget ( key -- value )
27 : tset ( value key -- )
30 : tchange ( key quot -- )
31 tnamespace swap change-at ; inline
35 : thread ( id -- thread ) threads at ;
37 : thread-registered? ( thread -- ? )
40 : check-unregistered ( thread -- thread )
41 dup thread-registered?
42 [ "Thread already stopped" throw ] when ;
44 : check-registered ( thread -- thread )
45 dup thread-registered?
46 [ "Thread is not running" throw ] unless ;
50 : register-thread ( thread -- )
51 check-unregistered dup id>> threads set-at ;
53 : unregister-thread ( thread -- )
54 check-registered id>> threads delete-at ;
56 : set-self ( thread -- ) 63 setenv ; inline
60 : new-thread ( quot name class -- thread )
66 [ ] >>exit-handler ; inline
68 : <thread> ( quot name -- thread )
71 : run-queue 65 getenv ;
73 : sleep-queue 66 getenv ;
75 : resume ( thread -- )
77 check-registered run-queue push-front ;
79 : resume-now ( thread -- )
81 check-registered run-queue push-back ;
83 : resume-with ( obj thread -- )
85 check-registered 2array run-queue push-front ;
87 : sleep-time ( -- ms/f )
89 { [ run-queue dequeue-empty? not ] [ 0 ] }
90 { [ sleep-queue heap-empty? ] [ f ] }
91 [ sleep-queue heap-peek nip millis [-] ]
98 : schedule-sleep ( thread dt -- )
99 >r check-registered dup r> sleep-queue heap-push*
102 : expire-sleep? ( heap -- ? )
104 [ drop f ] [ heap-peek nip millis <= ] if ;
106 : expire-sleep ( thread -- )
107 f >>sleep-entry resume ;
109 : expire-sleep-loop ( -- )
111 [ dup expire-sleep? ]
112 [ dup heap-pop drop expire-sleep ]
116 : start ( namestack thread -- )
123 self quot>> [ call stop ] call-clear
128 : no-runnable-threads ( -- * )
129 ! We should never be in a state where the only threads
130 ! are sleeping; the I/O wait thread is always runnable.
131 ! However, if it dies, we handle this case
134 ! And if sleep-time outputs f, there are no sleeping
135 ! threads either... so WTF.
136 sleep-time [ die 0 ] unless* (sleep) next ;
138 : (next) ( arg thread -- * )
142 continuation>> box> continue-with
149 run-queue dup dequeue-empty? [
150 drop no-runnable-threads
152 pop-back dup array? [ first2 ] [ f swap ] if (next)
158 self [ exit-handler>> call ] [ unregister-thread ] bi next ;
160 : suspend ( quot state -- obj )
165 r> self continuation>> >box
167 ] callcc1 2nip ; inline
169 : yield ( -- ) [ resume ] f suspend drop ;
171 GENERIC: sleep-until ( time/f -- )
173 M: integer sleep-until
174 [ schedule-sleep ] curry "sleep" suspend drop ;
177 drop [ drop ] "interrupt" suspend drop ;
179 GENERIC: sleep ( dt -- )
182 millis + >integer sleep-until ;
184 : interrupt ( thread -- )
186 dup sleep-entry>> [ sleep-queue heap-delete ] when*
191 : (spawn) ( thread -- )
192 [ register-thread ] [ namestack swap resume-with ] bi ;
194 : spawn ( quot name -- thread )
195 <thread> [ (spawn) ] keep ;
197 : spawn-server ( quot name -- thread )
198 >r [ loop ] curry r> spawn ;
200 : in-thread ( quot -- )
202 [ >r set-datastack r> call ] 2curry
203 "Thread" spawn drop ;
205 GENERIC: error-in-thread ( error thread -- )
209 : init-threads ( -- )
213 initial-thread global
214 [ drop f "Initial" <thread> ] cache
221 [ self error-in-thread stop ]
222 thread-error-hook set-global
226 [ init-threads ] "threads" add-init-hook