]> gitweb.factorcode.org Git - factor.git/blob - basis/threads/threads.factor
Merge branch 'irc' of git://tiodante.com/git/factor
[factor.git] / basis / threads / threads.factor
1 ! Copyright (C) 2004, 2009 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 ;
8 IN: threads
9
10 SYMBOL: initial-thread
11
12 TUPLE: thread
13 { name string }
14 { quot callable initial: [ ] }
15 { exit-handler callable initial: [ ] }
16 { id integer }
17 continuation
18 state
19 runnable
20 mailbox
21 variables
22 sleep-entry ;
23
24 : self ( -- thread ) 63 getenv ; inline
25
26 ! Thread-local storage
27 : tnamespace ( -- assoc )
28     self variables>> [ H{ } clone dup self (>>variables) ] unless* ;
29
30 : tget ( key -- value )
31     self variables>> at ;
32
33 : tset ( value key -- )
34     tnamespace set-at ;
35
36 : tchange ( key quot -- )
37     tnamespace swap change-at ; inline
38
39 : threads ( -- assoc ) 64 getenv ;
40
41 : thread ( id -- thread ) threads at ;
42
43 : thread-registered? ( thread -- ? )
44     id>> threads key? ;
45
46 ERROR: already-stopped thread ;
47
48 : check-unregistered ( thread -- thread )
49     dup thread-registered? [ already-stopped ] when ;
50
51 ERROR: not-running thread ;
52
53 : check-registered ( thread -- thread )
54     dup thread-registered? [ not-running ] unless ;
55
56 <PRIVATE
57
58 : register-thread ( thread -- )
59     check-unregistered dup id>> threads set-at ;
60
61 : unregister-thread ( thread -- )
62     check-registered id>> threads delete-at ;
63
64 : set-self ( thread -- ) 63 setenv ; inline
65
66 PRIVATE>
67
68 : new-thread ( quot name class -- thread )
69     new
70         swap >>name
71         swap >>quot
72         \ thread counter >>id
73         <box> >>continuation ; inline
74
75 : <thread> ( quot name -- thread )
76     \ thread new-thread ;
77
78 : run-queue ( -- dlist ) 65 getenv ;
79
80 : sleep-queue ( -- heap ) 66 getenv ;
81
82 : resume ( thread -- )
83     f >>state
84     check-registered run-queue push-front ;
85
86 : resume-now ( thread -- )
87     f >>state
88     check-registered run-queue push-back ;
89
90 : resume-with ( obj thread -- )
91     f >>state
92     check-registered 2array run-queue push-front ;
93
94 : sleep-time ( -- us/f )
95     {
96         { [ run-queue deque-empty? not ] [ 0 ] }
97         { [ sleep-queue heap-empty? ] [ f ] }
98         [ sleep-queue heap-peek nip micros [-] ]
99     } cond ;
100
101 DEFER: stop
102
103 <PRIVATE
104
105 : schedule-sleep ( thread dt -- )
106     [ check-registered dup ] dip sleep-queue heap-push*
107     >>sleep-entry drop ;
108
109 : expire-sleep? ( heap -- ? )
110     dup heap-empty?
111     [ drop f ] [ heap-peek nip micros <= ] if ;
112
113 : expire-sleep ( thread -- )
114     f >>sleep-entry resume ;
115
116 : expire-sleep-loop ( -- )
117     sleep-queue
118     [ dup expire-sleep? ]
119     [ dup heap-pop drop expire-sleep ]
120     while
121     drop ;
122
123 : start ( namestack thread -- * )
124     [
125         set-self
126         set-namestack
127         V{ } set-catchstack
128         { } set-retainstack
129         { } set-datastack
130         self quot>> [ call stop ] call-clear
131     ] (( namestack thread -- * )) call-effect-unsafe ;
132
133 DEFER: next
134
135 : no-runnable-threads ( -- * )
136     ! We should never be in a state where the only threads
137     ! are sleeping; the I/O wait thread is always runnable.
138     ! However, if it dies, we handle this case
139     ! semi-gracefully.
140     !
141     ! And if sleep-time outputs f, there are no sleeping
142     ! threads either... so WTF.
143     sleep-time [ die 0 ] unless* (sleep) next ;
144
145 : (next) ( arg thread -- * )
146     f >>state
147     dup set-self
148     dup runnable>> [
149         continuation>> box> continue-with
150     ] [
151         t >>runnable start
152     ] if ;
153
154 : next ( -- * )
155     expire-sleep-loop
156     run-queue dup deque-empty? [
157         drop no-runnable-threads
158     ] [
159         pop-back dup array? [ first2 ] [ f swap ] if (next)
160     ] if ;
161
162 PRIVATE>
163
164 : stop ( -- )
165     self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi next ;
166
167 : suspend ( quot state -- obj )
168     [
169         [ [ self swap call ] dip self (>>state) ] dip
170         self continuation>> >box
171         next
172     ] callcc1 2nip ; inline
173
174 : yield ( -- ) [ resume ] f suspend drop ;
175
176 GENERIC: sleep-until ( time/f -- )
177
178 M: integer sleep-until
179     '[ _ schedule-sleep ] "sleep" suspend drop ;
180
181 M: f sleep-until
182     drop [ drop ] "interrupt" suspend drop ;
183
184 GENERIC: sleep ( dt -- )
185
186 M: real sleep
187     micros + >integer sleep-until ;
188
189 : interrupt ( thread -- )
190     dup state>> [
191         dup sleep-entry>> [ sleep-queue heap-delete ] when*
192         f >>sleep-entry
193         dup resume
194     ] when drop ;
195
196 : (spawn) ( thread -- )
197     [ register-thread ] [ namestack swap resume-with ] bi ;
198
199 : spawn ( quot name -- thread )
200     <thread> [ (spawn) ] keep ;
201
202 : spawn-server ( quot name -- thread )
203     [ '[ _ loop ] ] dip spawn ;
204
205 : in-thread ( quot -- )
206     [ datastack ] dip
207     '[ _ set-datastack _ call ]
208     "Thread" spawn drop ;
209
210 GENERIC: error-in-thread ( error thread -- )
211
212 <PRIVATE
213
214 : init-threads ( -- )
215     H{ } clone 64 setenv
216     <dlist> 65 setenv
217     <min-heap> 66 setenv
218     initial-thread global
219     [ drop [ ] "Initial" <thread> ] cache
220     <box> >>continuation
221     t >>runnable
222     f >>state
223     dup register-thread
224     set-self ;
225
226 PRIVATE>
227
228 [ init-threads ] "threads" add-init-hook