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
16 mailbox variables sleep-entry ;
18 : self ( -- thread ) 40 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 -- ? )
41 dup thread-registered?
42 [ "Thread already stopped" throw ] when ;
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 -- ) 40 setenv ; inline
60 : new-thread ( quot name class -- thread )
66 [ ] >>exit-handler ; inline
68 : <thread> ( quot name -- thread )
71 : run-queue 42 getenv ;
73 : sleep-queue 43 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 dlist-empty? not ] [ 0 ] }
90 { [ sleep-queue heap-empty? ] [ f ] }
91 [ sleep-queue heap-peek nip millis [-] ]
96 : schedule-sleep ( thread dt -- )
97 >r check-registered dup r> sleep-queue heap-push*
100 : expire-sleep? ( heap -- ? )
102 [ drop f ] [ heap-peek nip millis <= ] if ;
104 : expire-sleep ( thread -- )
105 f >>sleep-entry resume ;
107 : expire-sleep-loop ( -- )
109 [ dup expire-sleep? ]
110 [ dup heap-pop drop expire-sleep ]
116 run-queue dup dlist-empty? [
117 ! We should never be in a state where the only threads
118 ! are sleeping; the I/O wait thread is always runnable.
119 ! However, if it dies, we handle this case
122 ! And if sleep-time outputs f, there are no sleeping
123 ! threads either... so WTF.
124 drop sleep-time [ die 0 ] unless* (sleep) next
127 dup array? [ first2 ] [ f swap ] if dup set-self
136 self dup exit-handler>> call
137 unregister-thread next ;
139 : suspend ( quot state -- obj )
141 self continuation>> >box
144 ] callcc1 2nip ; inline
146 : yield ( -- ) [ resume ] f suspend drop ;
148 GENERIC: sleep-until ( time/f -- )
150 M: integer sleep-until
151 [ schedule-sleep ] curry "sleep" suspend drop ;
154 drop [ drop ] "interrupt" suspend drop ;
156 GENERIC: sleep ( dt -- )
159 millis + >integer sleep-until ;
161 : interrupt ( thread -- )
163 dup sleep-entry>> [ sleep-queue heap-delete ] when*
168 : (spawn) ( thread -- )
175 >r { } set-datastack r>
176 quot>> [ call stop ] call-clear
178 ] "spawn" suspend 2drop ;
180 : spawn ( quot name -- thread )
181 <thread> [ (spawn) ] keep ;
183 : spawn-server ( quot name -- thread )
184 >r [ [ ] [ ] while ] curry r> spawn ;
186 : in-thread ( quot -- )
187 >r datastack namestack r>
188 [ >r set-namestack set-datastack r> call ] 3curry
189 "Thread" spawn drop ;
191 GENERIC: error-in-thread ( error thread -- )
195 : init-threads ( -- )
199 initial-thread global
200 [ drop f "Initial" <thread> ] cache
206 [ self error-in-thread stop ]
207 thread-error-hook set-global
211 [ init-threads ] "threads" add-init-hook