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
7 math.order deques strings quotations fry ;
10 SYMBOL: initial-thread
14 { quot callable initial: [ ] }
15 { exit-handler callable initial: [ ] }
24 : self ( -- thread ) 63 getenv ; inline
26 ! Thread-local storage
27 : tnamespace ( -- assoc )
28 self variables>> [ H{ } clone dup self (>>variables) ] unless* ;
30 : tget ( key -- value )
33 : tset ( value key -- )
36 : tchange ( key quot -- )
37 tnamespace swap change-at ; inline
39 : threads ( -- assoc ) 64 getenv ;
41 : thread ( id -- thread ) threads at ;
43 : thread-registered? ( thread -- ? )
46 : check-unregistered ( thread -- thread )
47 dup thread-registered?
48 [ "Thread already stopped" throw ] when ;
50 : check-registered ( thread -- thread )
51 dup thread-registered?
52 [ "Thread is not running" throw ] unless ;
56 : register-thread ( thread -- )
57 check-unregistered dup id>> threads set-at ;
59 : unregister-thread ( thread -- )
60 check-registered id>> threads delete-at ;
62 : set-self ( thread -- ) 63 setenv ; inline
66 : new-thread ( quot name class -- thread )
71 <box> >>continuation ; inline
73 : <thread> ( quot name -- thread )
76 : run-queue ( -- dlist ) 65 getenv ;
78 : sleep-queue ( -- heap ) 66 getenv ;
80 : resume ( thread -- )
82 check-registered run-queue push-front ;
84 : resume-now ( thread -- )
86 check-registered run-queue push-back ;
88 : resume-with ( obj thread -- )
90 check-registered 2array run-queue push-front ;
92 : sleep-time ( -- us/f )
94 { [ run-queue deque-empty? not ] [ 0 ] }
95 { [ sleep-queue heap-empty? ] [ f ] }
96 [ sleep-queue heap-peek nip micros [-] ]
103 : schedule-sleep ( thread dt -- )
104 [ check-registered dup ] dip sleep-queue heap-push*
107 : expire-sleep? ( heap -- ? )
109 [ drop f ] [ heap-peek nip micros <= ] if ;
111 : expire-sleep ( thread -- )
112 f >>sleep-entry resume ;
114 : expire-sleep-loop ( -- )
116 [ dup expire-sleep? ]
117 [ dup heap-pop drop expire-sleep ]
121 : start ( namestack thread -- )
128 self quot>> [ call stop ] call-clear
133 : no-runnable-threads ( -- * )
134 ! We should never be in a state where the only threads
135 ! are sleeping; the I/O wait thread is always runnable.
136 ! However, if it dies, we handle this case
139 ! And if sleep-time outputs f, there are no sleeping
140 ! threads either... so WTF.
141 sleep-time [ die 0 ] unless* (sleep) next ;
143 : (next) ( arg thread -- * )
147 continuation>> box> continue-with
154 run-queue dup deque-empty? [
155 drop no-runnable-threads
157 pop-back dup array? [ first2 ] [ f swap ] if (next)
163 self [ exit-handler>> call ] [ unregister-thread ] bi next ;
165 : suspend ( quot state -- obj )
167 [ [ self swap call ] dip self (>>state) ] dip
168 self continuation>> >box
170 ] callcc1 2nip ; inline
172 : yield ( -- ) [ resume ] f suspend drop ;
174 GENERIC: sleep-until ( time/f -- )
176 M: integer sleep-until
177 '[ _ schedule-sleep ] "sleep" suspend drop ;
180 drop [ drop ] "interrupt" suspend drop ;
182 GENERIC: sleep ( dt -- )
185 micros + >integer sleep-until ;
187 : interrupt ( thread -- )
189 dup sleep-entry>> [ sleep-queue heap-delete ] when*
194 : (spawn) ( thread -- )
195 [ register-thread ] [ namestack swap resume-with ] bi ;
197 : spawn ( quot name -- thread )
198 <thread> [ (spawn) ] keep ;
200 : spawn-server ( quot name -- thread )
201 [ '[ _ loop ] ] dip spawn ;
203 : in-thread ( quot -- )
205 '[ _ set-datastack _ call ]
206 "Thread" spawn drop ;
208 GENERIC: error-in-thread ( error thread -- )
212 : init-threads ( -- )
216 initial-thread global
217 [ drop [ ] "Initial" <thread> ] cache
226 [ init-threads ] "threads" add-init-hook