factor/basis/threads/threads.factor

283 lines
6.4 KiB
Factor
Raw Normal View History

2010-03-27 12:03:06 -04:00
! Copyright (C) 2004, 2010 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
dlists assocs system combinators init boxes accessors math.order
deques strings quotations fry ;
IN: threads
2008-02-18 06:07:40 -05:00
<PRIVATE
! (set-context) and (start-context) are sub-primitives, but
! we don't want them inlined into callers since their behavior
! depends on what frames are on the callstack
2010-03-27 12:03:06 -04:00
: set-context ( obj context -- obj' ) (set-context) ;
2010-03-27 12:03:06 -04:00
: start-context ( obj quot: ( obj -- * ) -- obj' ) (start-context) ;
: namestack-for ( context -- namestack )
[ 0 ] dip context-object-for ;
: catchstack-for ( context -- catchstack )
[ 1 ] dip context-object-for ;
: continuation-for ( context -- continuation )
{
[ datastack-for ]
[ callstack-for ]
[ retainstack-for ]
[ namestack-for ]
[ catchstack-for ]
} cleave <continuation> ;
PRIVATE>
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 }
{ context box }
2008-07-29 19:44:44 -04:00
state
runnable
mailbox
2010-03-27 12:03:06 -04:00
{ variables hashtable }
2008-07-29 19:44:44 -04:00
sleep-entry ;
2008-02-18 06:07:40 -05:00
2010-03-27 12:03:06 -04:00
: self ( -- thread )
63 special-object { thread } declare ; inline
2008-02-18 06:07:40 -05:00
: thread-continuation ( thread -- continuation )
context>> check-box value>> continuation-for ;
2008-02-18 06:07:40 -05:00
! Thread-local storage
: tnamespace ( -- assoc )
2010-03-28 12:33:41 -04:00
self variables>> ; inline
2008-02-18 06:07:40 -05:00
: tget ( key -- value )
2010-03-28 12:33:41 -04:00
tnamespace 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 -- )
[ tnamespace ] dip change-at ; inline
2008-02-18 06:07:40 -05:00
2010-03-27 12:03:06 -04:00
: threads ( -- assoc )
64 special-object { hashtable } declare ; inline
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
2009-07-07 14:00:58 -04:00
ERROR: already-stopped thread ;
2008-06-08 16:32:55 -04:00
: check-unregistered ( thread -- thread )
2009-07-07 14:00:58 -04:00
dup thread-registered? [ already-stopped ] when ;
ERROR: not-running thread ;
2008-02-18 06:07:40 -05:00
2008-06-08 16:32:55 -04:00
: check-registered ( thread -- thread )
2009-07-07 14:00:58 -04:00
dup thread-registered? [ not-running ] unless ;
2008-02-21 00:13:31 -05:00
<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 set-special-object ; inline
2008-02-18 06:07:40 -05:00
2008-02-18 08:30:16 -05:00
PRIVATE>
: run-queue ( -- dlist )
65 special-object { dlist } declare ; inline
: sleep-queue ( -- heap )
66 special-object { dlist } declare ; inline
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
H{ } clone >>variables
<box> >>context ; inline
2008-04-14 06:19:26 -04:00
: <thread> ( quot name -- thread )
\ thread new-thread ;
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
: sleep-time ( -- nanos/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 nano-count [-] ]
2008-02-25 17:48:11 -05:00
} cond ;
: interrupt ( thread -- )
dup state>> [
dup sleep-entry>> [ sleep-queue heap-delete ] when*
f >>sleep-entry
dup resume
] when drop ;
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 nano-count <= ] 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 -- obj )
2008-04-27 04:16:12 -04:00
[
set-namestack
init-catchstack
self quot>> call
stop
] start-context ;
2008-04-27 04:16:12 -04:00
DEFER: next
: no-runnable-threads ( -- obj )
2008-04-27 04:16:12 -04:00
! 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.
2009-12-15 07:20:41 -05:00
sleep-time {
{ [ dup not ] [ drop die ] }
{ [ dup 0 = ] [ drop ] }
[ (sleep) ]
} cond next ;
2008-04-27 04:16:12 -04:00
: (next) ( obj thread -- obj' )
2008-04-27 04:16:12 -04:00
f >>state
dup set-self
dup runnable>>
[ context>> box> set-context ] [ t >>runnable drop start ] if ;
2008-04-27 04:16:12 -04:00
: next ( -- obj )
2008-02-21 20:12:37 -05:00
expire-sleep-loop
run-queue dup deque-empty?
[ drop no-runnable-threads ]
[ pop-back dup array? [ first2 ] [ [ f ] dip ] if (next) ] if ;
: recycler-thread ( -- thread ) 68 special-object ;
: recycler-queue ( -- vector ) 69 special-object ;
: delete-context-later ( context -- )
recycler-queue push recycler-thread interrupt ;
2008-02-18 06:07:40 -05:00
PRIVATE>
2010-03-27 12:03:06 -04:00
: stop ( -- * )
self [ exit-handler>> call( -- ) ] [ unregister-thread ] bi
context delete-context-later next
die 1 exit ;
2008-02-18 06:07:40 -05:00
2010-03-27 12:03:06 -04:00
: suspend ( state -- obj )
[ self ] dip >>state
[ context ] dip context>> >box
next ;
2008-02-18 06:07:40 -05:00
2010-03-27 12:03:06 -04:00
: yield ( -- ) self resume f suspend drop ;
2008-02-21 20:12:37 -05:00
GENERIC: sleep-until ( n/f -- )
M: integer sleep-until
2010-03-27 12:03:06 -04:00
[ self ] dip schedule-sleep "sleep" suspend drop ;
M: f sleep-until
drop "standby" suspend drop ;
2008-05-05 20:41:44 -04:00
GENERIC: sleep ( dt -- )
M: real sleep
>integer nano-count + sleep-until ;
2008-02-21 20:12:37 -05:00
2008-02-18 06:07:40 -05:00
: (spawn) ( thread -- )
[ register-thread ] [ [ namestack ] dip 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
2010-03-27 12:03:06 -04:00
'[ _ set-datastack @ ]
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-thread-state ( -- )
H{ } clone 64 set-special-object
<dlist> 65 set-special-object
<min-heap> 66 set-special-object ;
: init-initial-thread ( -- )
[ ] "Initial" <thread>
2008-05-07 05:22:48 -04:00
t >>runnable
[ initial-thread set-global ]
[ register-thread ]
[ set-self ]
tri ;
! The recycler thread deletes contexts belonging to stopped
! threads
: recycler-loop ( -- )
recycler-queue [ [ delete-context ] each ] [ delete-all ] bi
f sleep-until
recycler-loop ;
: init-recycler ( -- )
[ recycler-loop ] "Context recycler" spawn 68 set-special-object
V{ } clone 69 set-special-object ;
: init-threads ( -- )
init-thread-state
init-initial-thread
init-recycler ;
2008-02-18 06:07:40 -05:00
PRIVATE>
[ init-threads ] "threads" add-startup-hook