factor/basis/threads/threads.factor

250 lines
5.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
! Wrap sub-primitives; we don't want them inlined into callers
! since their behavior depends on what frames are on the callstack
2010-04-01 22:12:45 -04:00
: context ( -- context )
2 context-object ; inline
: set-context ( obj context -- obj' )
2010-04-01 22:12:45 -04:00
(set-context) ; inline
: start-context ( obj quot: ( obj -- * ) -- obj' )
2010-04-01 22:12:45 -04:00
(start-context) ; inline
: set-context-and-delete ( obj context -- * )
2010-04-01 22:12:45 -04:00
(set-context-and-delete) ; inline
: start-context-and-delete ( obj quot: ( obj -- * ) -- * )
2010-04-01 22:12:45 -04:00
(start-context-and-delete) ; inline
! Context introspection
: 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
2008-02-21 00:13:31 -05:00
<PRIVATE
2008-02-18 06:07:40 -05:00
: register-thread ( thread -- )
2010-04-01 20:05:32 -04:00
dup id>> threads set-at ;
2008-02-18 06:07:40 -05:00
: unregister-thread ( thread -- )
2010-04-01 20:05:32 -04:00
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 )
2010-04-01 20:05:32 -04:00
66 special-object { min-heap } 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 -- )
2010-04-01 20:05:32 -04:00
f >>state run-queue push-front ;
2008-02-18 06:07:40 -05:00
2008-02-21 02:25:08 -05:00
: resume-now ( thread -- )
2010-04-01 20:05:32 -04:00
f >>state run-queue push-back ;
2008-02-21 02:25:08 -05:00
2008-02-18 06:07:40 -05:00
: resume-with ( obj thread -- )
2010-04-01 20:05:32 -04:00
f >>state 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 -- )
2010-04-01 20:05:32 -04:00
dupd sleep-queue heap-push* >>sleep-entry drop ;
2008-02-18 06:07:40 -05:00
2010-04-01 20:05:32 -04:00
: expire-sleep? ( -- ? )
sleep-queue 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 ( -- )
2010-04-01 20:05:32 -04:00
[ expire-sleep? ]
[ sleep-queue heap-pop drop expire-sleep ]
while ;
2008-02-18 06:07:40 -05:00
CONSTANT: [start]
2008-04-27 04:16:12 -04:00
[
set-namestack
init-catchstack
self quot>> call
stop
]
: no-runnable-threads ( -- ) die ;
2008-04-27 04:16:12 -04:00
2010-04-01 20:05:32 -04:00
GENERIC: (next) ( obj thread -- obj' )
M: thread (next)
dup runnable>>
[ context>> box> set-context ]
[ t >>runnable drop [start] start-context ] if ;
: (stop) ( obj thread -- * )
dup runnable>>
[ context>> box> set-context-and-delete ]
[ t >>runnable drop [start] start-context-and-delete ] if ;
: next ( -- obj thread )
expire-sleep-loop
run-queue pop-back
dup array? [ first2 ] [ [ f ] dip ] if
f >>state
dup set-self ;
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
next (stop) ;
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 (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 ;
: init-threads ( -- )
init-thread-state
init-initial-thread ;
2008-02-18 06:07:40 -05:00
PRIVATE>
[ init-threads ] "threads" add-startup-hook