1 ! Copyright (C) 2004, 2011 Slava Pestov.
2 ! Copyright (C) 2005 Mackenzie Straight.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien.private arrays assocs boxes combinators
5 continuations continuations.private deques dlists fry hashtables
6 heaps init kernel kernel.private math math.order namespaces
7 quotations sequences strings system ;
8 FROM: assocs => change-at ;
12 PRIMITIVE: (set-context) ( obj context -- obj' )
13 PRIMITIVE: (set-context-and-delete) ( obj context -- * )
14 PRIMITIVE: (sleep) ( nanos -- )
15 PRIMITIVE: (start-context) ( obj quot -- obj' )
16 PRIMITIVE: (start-context-and-delete) ( obj quot -- * )
17 PRIMITIVE: callstack-for ( context -- array )
18 PRIMITIVE: context-object-for ( n context -- obj )
19 PRIMITIVE: datastack-for ( context -- array )
20 PRIMITIVE: retainstack-for ( context -- array )
22 ! Wrap sub-primitives; we don't want them inlined into callers
23 ! since their behavior depends on what frames are on the callstack
24 : context ( -- context )
25 CONTEXT-OBJ-CONTEXT context-object ; inline
27 : set-context ( obj context -- obj' )
28 (set-context) ; inline
30 : start-context ( obj quot: ( obj -- * ) -- obj' )
31 (start-context) ; inline
33 : set-context-and-delete ( obj context -- * )
34 (set-context-and-delete) ; inline
36 : start-context-and-delete ( obj quot: ( obj -- * ) -- * )
37 (start-context-and-delete) ; inline
39 ! Context introspection
40 : namestack-for ( context -- namestack )
41 [ CONTEXT-OBJ-NAMESTACK ] dip context-object-for ;
43 : catchstack-for ( context -- catchstack )
44 [ CONTEXT-OBJ-CATCHSTACK ] dip context-object-for ;
46 : continuation-for ( context -- continuation )
53 } cleave <continuation> ;
57 SYMBOL: initial-thread
61 { quot callable initial: [ ] }
62 { exit-handler callable initial: [ ] }
68 { variables hashtable }
72 OBJ-CURRENT-THREAD special-object { thread } declare ; inline
74 : thread-continuation ( thread -- continuation )
75 context>> check-box value>> continuation-for ;
77 ! Thread-local storage
78 : tnamespace ( -- assoc )
79 self variables>> ; inline
81 : tget ( key -- value )
84 : tset ( value key -- )
87 : tchange ( ..a key quot: ( ..a value -- ..b newvalue ) -- ..b )
88 [ tnamespace ] dip change-at ; inline
90 : threads ( -- assoc )
91 OBJ-THREADS special-object { hashtable } declare ; inline
93 : thread-registered? ( thread -- ? )
98 : register-thread ( thread -- )
99 dup id>> threads set-at ;
101 : unregister-thread ( thread -- )
102 id>> threads delete-at ;
104 : set-self ( thread -- )
105 OBJ-CURRENT-THREAD set-special-object ; inline
109 : run-queue ( -- dlist )
110 OBJ-RUN-QUEUE special-object { dlist } declare ; inline
112 : sleep-queue ( -- heap )
113 OBJ-SLEEP-QUEUE special-object { min-heap } declare ; inline
115 : waiting-callbacks ( -- assoc )
116 OBJ-WAITING-CALLBACKS special-object { hashtable } declare ; inline
118 : new-thread ( quot name class -- thread )
122 \ thread counter >>id
123 H{ } clone >>variables
124 <box> >>context ; inline
126 : <thread> ( quot name -- thread )
127 \ thread new-thread ;
129 : resume ( thread -- )
130 f >>state run-queue push-front ;
132 : resume-now ( thread -- )
133 f >>state run-queue push-back ;
135 : resume-with ( obj thread -- )
136 f >>state 2array run-queue push-front ;
138 : sleep-time ( -- nanos/f )
140 { [ current-callback waiting-callbacks key? ] [ 0 ] }
141 { [ run-queue deque-empty? not ] [ 0 ] }
142 { [ sleep-queue heap-empty? ] [ f ] }
143 [ sleep-queue heap-peek nip nano-count [-] ]
146 : interrupt ( thread -- )
149 [ sleep-queue heap-delete ] when* f
150 ] change-sleep-entry dup resume
157 : schedule-sleep ( thread dt -- )
158 dupd sleep-queue heap-push* >>sleep-entry drop ;
160 : expire-sleep? ( -- ? )
161 sleep-queue dup heap-empty?
162 [ drop f ] [ heap-peek nip nano-count <= ] if ;
164 : expire-sleep ( thread -- )
165 f >>sleep-entry resume ;
167 : expire-sleep-loop ( -- )
169 [ sleep-queue heap-pop drop expire-sleep ]
180 GENERIC: (next) ( obj thread -- obj' )
184 [ context>> box> set-context ]
185 [ t >>runnable drop [start] start-context ] if ;
187 : (stop) ( obj thread -- * )
189 [ context>> box> set-context-and-delete ]
190 [ t >>runnable drop [start] start-context-and-delete ] if ;
192 : wake-up-callbacks ( -- )
193 current-callback waiting-callbacks delete-at*
194 [ resume-now ] [ drop ] if ;
196 : next ( -- obj thread )
200 dup array? [ first2 ] [ [ f ] dip ] if
207 self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi
210 : suspend ( state -- obj )
212 [ context ] dip context>> >box
216 self resume f suspend drop ;
218 GENERIC: sleep-until ( n/f -- )
220 M: integer sleep-until
221 [ self ] dip schedule-sleep "sleep" suspend drop ;
224 drop "standby" suspend drop ;
226 GENERIC: sleep ( dt -- )
229 >integer nano-count + sleep-until ;
231 : (spawn) ( thread -- )
232 [ register-thread ] [ [ namestack ] dip resume-with ] bi ;
234 : spawn ( quot name -- thread )
235 <thread> [ (spawn) ] keep ;
237 : spawn-server ( quot name -- thread )
238 [ '[ _ loop ] ] dip spawn ;
240 : in-thread ( quot -- )
242 '[ _ set-datastack @ ]
243 "Thread" spawn drop ;
247 : init-thread-state ( -- )
248 H{ } clone OBJ-THREADS set-special-object
249 <dlist> OBJ-RUN-QUEUE set-special-object
250 <min-heap> OBJ-SLEEP-QUEUE set-special-object
251 H{ } clone OBJ-WAITING-CALLBACKS set-special-object ;
253 : init-initial-thread ( -- )
254 [ ] "Initial" <thread>
256 [ initial-thread set-global ]
261 : init-threads ( -- )
263 init-initial-thread ;
265 : wait-for-callback ( callback -- )
266 self swap waiting-callbacks set-at
267 "Callback return" suspend drop ;
271 [ init-threads ] "threads" add-startup-hook