factor/basis/threads/threads.factor

227 lines
5.0 KiB
Factor
Raw Normal View History

2009-03-16 21:11:36 -04:00
! Copyright (C) 2004, 2009 Slava Pestov.
2008-02-18 06:07:40 -05:00
! Copyright (C) 2005 Mackenzie Straight.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays hashtables heaps kernel kernel.private math
namespaces sequences vectors continuations continuations.private
2009-03-16 21:11:36 -04:00
dlists assocs system combinators combinators.private init boxes
accessors math.order deques strings quotations fry ;
IN: threads
2008-02-18 06:07:40 -05:00
SYMBOL: initial-thread
TUPLE: thread
2008-07-29 19:44:44 -04:00
{ name string }
{ quot callable initial: [ ] }
{ exit-handler callable initial: [ ] }
{ id integer }
continuation
state
runnable
mailbox
variables
sleep-entry ;
2008-02-18 06:07:40 -05:00
: self ( -- thread ) 63 getenv ; inline
2008-02-18 06:07:40 -05:00
! Thread-local storage
: tnamespace ( -- assoc )
2008-04-11 08:15:26 -04:00
self variables>> [ H{ } clone dup self (>>variables) ] unless* ;
2008-02-18 06:07:40 -05:00
: tget ( key -- value )
2008-04-11 08:15:26 -04:00
self variables>> at ;
2008-02-18 06:07:40 -05:00
: tset ( value key -- )
tnamespace set-at ;
2008-02-18 06:07:40 -05:00
: tchange ( key quot -- )
2008-04-17 06:16:28 -04:00
tnamespace swap change-at ; inline
2008-02-18 06:07:40 -05:00
2008-12-05 10:04:16 -05:00
: threads ( -- assoc ) 64 getenv ;
2008-02-18 06:07:40 -05:00
: thread ( id -- thread ) threads at ;
2008-02-18 06:07:40 -05:00
2008-02-21 00:13:31 -05:00
: thread-registered? ( thread -- ? )
2008-04-11 08:15:26 -04:00
id>> threads key? ;
2008-02-18 06:07:40 -05:00
2008-06-08 16:32:55 -04:00
: check-unregistered ( thread -- thread )
2008-02-18 06:07:40 -05:00
dup thread-registered?
2008-02-21 00:13:31 -05:00
[ "Thread already stopped" throw ] when ;
2008-02-18 06:07:40 -05:00
2008-06-08 16:32:55 -04:00
: check-registered ( thread -- thread )
2008-02-18 06:07:40 -05:00
dup thread-registered?
2008-02-21 00:13:31 -05:00
[ "Thread is not running" throw ] unless ;
<PRIVATE
2008-02-18 06:07:40 -05:00
: register-thread ( thread -- )
2008-04-11 08:15:26 -04:00
check-unregistered dup id>> threads set-at ;
2008-02-18 06:07:40 -05:00
: unregister-thread ( thread -- )
2008-04-11 08:15:26 -04:00
check-registered id>> threads delete-at ;
2008-02-18 06:07:40 -05:00
: set-self ( thread -- ) 63 setenv ; inline
2008-02-18 06:07:40 -05:00
2008-02-18 08:30:16 -05:00
PRIVATE>
2008-04-14 06:19:26 -04:00
: new-thread ( quot name class -- thread )
new
2008-04-11 08:15:26 -04:00
swap >>name
swap >>quot
\ thread counter >>id
2008-07-29 19:44:44 -04:00
<box> >>continuation ; inline
2008-04-14 06:19:26 -04:00
: <thread> ( quot name -- thread )
\ thread new-thread ;
2008-02-18 06:07:40 -05:00
2008-12-05 10:04:16 -05:00
: run-queue ( -- dlist ) 65 getenv ;
2008-12-05 10:04:16 -05:00
: sleep-queue ( -- heap ) 66 getenv ;
2008-02-18 06:07:40 -05:00
: resume ( thread -- )
2008-04-11 08:15:26 -04:00
f >>state
check-registered run-queue push-front ;
2008-02-18 06:07:40 -05:00
2008-02-21 02:25:08 -05:00
: resume-now ( thread -- )
2008-04-11 08:15:26 -04:00
f >>state
2008-02-21 02:25:08 -05:00
check-registered run-queue push-back ;
2008-02-18 06:07:40 -05:00
: resume-with ( obj thread -- )
2008-04-11 08:15:26 -04:00
f >>state
check-registered 2array run-queue push-front ;
2008-02-18 06:07:40 -05:00
2008-11-24 01:51:46 -05:00
: sleep-time ( -- us/f )
2008-02-25 17:48:11 -05:00
{
2008-08-19 15:06:20 -04:00
{ [ run-queue deque-empty? not ] [ 0 ] }
2008-02-25 17:48:11 -05:00
{ [ sleep-queue heap-empty? ] [ f ] }
[ sleep-queue heap-peek nip micros [-] ]
2008-02-25 17:48:11 -05:00
} cond ;
2008-04-27 04:16:12 -04:00
DEFER: stop
2008-02-18 06:07:40 -05:00
<PRIVATE
2008-05-05 20:41:44 -04:00
: schedule-sleep ( thread dt -- )
2008-12-03 09:46:16 -05:00
[ check-registered dup ] dip sleep-queue heap-push*
2008-04-11 08:15:26 -04:00
>>sleep-entry drop ;
2008-02-18 06:07:40 -05:00
2008-02-21 20:12:37 -05:00
: expire-sleep? ( heap -- ? )
2008-02-18 06:07:40 -05:00
dup heap-empty?
[ drop f ] [ heap-peek nip micros <= ] if ;
2008-02-18 06:07:40 -05:00
2008-02-21 20:12:37 -05:00
: expire-sleep ( thread -- )
2008-04-11 08:15:26 -04:00
f >>sleep-entry resume ;
2008-02-21 20:12:37 -05:00
: expire-sleep-loop ( -- )
sleep-queue
2008-02-21 20:12:37 -05:00
[ dup expire-sleep? ]
[ dup heap-pop drop expire-sleep ]
while
2008-02-18 06:07:40 -05:00
drop ;
: start ( namestack thread -- * )
2008-04-27 04:16:12 -04:00
[
set-self
set-namestack
V{ } set-catchstack
{ } set-retainstack
{ } set-datastack
self quot>> [ call stop ] call-clear
2009-03-16 21:11:36 -04:00
] (( namestack thread -- * )) call-effect-unsafe ;
2008-04-27 04:16:12 -04:00
DEFER: next
: no-runnable-threads ( -- * )
! We should never be in a state where the only threads
! are sleeping; the I/O wait thread is always runnable.
! However, if it dies, we handle this case
! semi-gracefully.
!
! And if sleep-time outputs f, there are no sleeping
! threads either... so WTF.
sleep-time [ die 0 ] unless* (sleep) next ;
: (next) ( arg thread -- * )
f >>state
dup set-self
2008-05-07 02:56:55 -04:00
dup runnable>> [
continuation>> box> continue-with
] [
t >>runnable start
] if ;
2008-04-27 04:16:12 -04:00
2008-02-25 17:48:11 -05:00
: next ( -- * )
2008-02-21 20:12:37 -05:00
expire-sleep-loop
2008-08-19 15:06:20 -04:00
run-queue dup deque-empty? [
2008-04-27 04:16:12 -04:00
drop no-runnable-threads
2008-02-25 17:48:11 -05:00
] [
2008-04-27 04:16:12 -04:00
pop-back dup array? [ first2 ] [ f swap ] if (next)
2008-02-25 17:48:11 -05:00
] if ;
2008-02-18 06:07:40 -05:00
PRIVATE>
: stop ( -- )
2009-03-17 03:19:50 -04:00
self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi next ;
2008-02-18 06:07:40 -05:00
: suspend ( quot state -- obj )
2008-02-18 06:07:40 -05:00
[
2008-12-03 09:46:16 -05:00
[ [ self swap call ] dip self (>>state) ] dip
self continuation>> >box
2008-04-27 04:16:12 -04:00
next
] callcc1 2nip ; inline
2008-02-18 06:07:40 -05:00
: yield ( -- ) [ resume ] f suspend drop ;
2008-02-21 20:12:37 -05:00
GENERIC: sleep-until ( time/f -- )
M: integer sleep-until
2008-12-03 09:46:16 -05:00
'[ _ schedule-sleep ] "sleep" suspend drop ;
M: f sleep-until
drop [ drop ] "interrupt" suspend drop ;
2008-05-05 20:41:44 -04:00
GENERIC: sleep ( dt -- )
M: real sleep
micros + >integer sleep-until ;
2008-02-21 20:12:37 -05:00
: interrupt ( thread -- )
2008-04-11 08:15:26 -04:00
dup state>> [
dup sleep-entry>> [ sleep-queue heap-delete ] when*
f >>sleep-entry
dup resume
] when drop ;
2008-02-18 06:07:40 -05:00
: (spawn) ( thread -- )
2008-04-27 04:16:12 -04:00
[ register-thread ] [ namestack swap resume-with ] bi ;
2008-02-18 06:07:40 -05:00
: spawn ( quot name -- thread )
2008-02-27 20:23:22 -05:00
<thread> [ (spawn) ] keep ;
2008-02-18 06:07:40 -05:00
2008-02-18 08:30:16 -05:00
: spawn-server ( quot name -- thread )
2008-12-03 09:46:16 -05:00
[ '[ _ loop ] ] dip spawn ;
2008-02-18 08:30:16 -05:00
: in-thread ( quot -- )
2008-12-03 09:46:16 -05:00
[ datastack ] dip
'[ _ set-datastack _ call ]
2008-02-18 08:30:16 -05:00
"Thread" spawn drop ;
2008-02-18 06:07:40 -05:00
2008-02-27 20:23:22 -05:00
GENERIC: error-in-thread ( error thread -- )
2008-02-18 06:07:40 -05:00
<PRIVATE
: init-threads ( -- )
H{ } clone 64 setenv
<dlist> 65 setenv
<min-heap> 66 setenv
2008-02-18 06:07:40 -05:00
initial-thread global
2008-07-29 19:44:44 -04:00
[ drop [ ] "Initial" <thread> ] cache
2008-04-11 08:15:26 -04:00
<box> >>continuation
2008-05-07 05:22:48 -04:00
t >>runnable
2008-04-11 08:15:26 -04:00
f >>state
2008-02-18 06:07:40 -05:00
dup register-thread
set-self ;
PRIVATE>
2008-02-18 08:30:16 -05:00
[ init-threads ] "threads" add-init-hook