factor/core/threads/threads.factor

184 lines
4.1 KiB
Factor
Raw Normal View History

2008-02-18 06:07:40 -05:00
! Copyright (C) 2004, 2008 Slava Pestov.
! Copyright (C) 2005 Mackenzie Straight.
! See http://factorcode.org/license.txt for BSD license.
2008-02-18 08:30:16 -05:00
IN: threads
2008-02-18 06:07:40 -05:00
USING: arrays hashtables heaps kernel kernel.private math
namespaces sequences vectors continuations continuations.private
dlists assocs system combinators debugger prettyprint io init ;
SYMBOL: initial-thread
TUPLE: thread
name quot error-handler
id registered?
continuation
mailbox variables ;
: self ( -- thread ) 40 getenv ; inline
! Thread-local storage
: tnamespace ( -- assoc )
self dup thread-variables
[ ] [ H{ } clone dup rot set-thread-variables ] ?if ;
2008-02-18 06:07:40 -05:00
: tget ( key -- value )
self thread-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 -- )
tnamespace change-at ; inline
2008-02-18 06:07:40 -05:00
: threads 41 getenv ;
2008-02-18 06:07:40 -05:00
threads global [ H{ } assoc-like ] change-at
: thread ( id -- thread ) threads at ;
2008-02-18 06:07:40 -05:00
<PRIVATE
: check-unregistered
dup thread-registered?
[ "Registering a thread twice" throw ] when ;
: check-registered
dup thread-registered?
[ "Unregistering a thread twice" throw ] unless ;
: register-thread ( thread -- )
check-unregistered
t over set-thread-registered?
dup thread-id threads set-at ;
2008-02-18 06:07:40 -05:00
: unregister-thread ( thread -- )
check-registered
f over set-thread-registered?
thread-id threads delete-at ;
2008-02-18 06:07:40 -05:00
: set-self ( thread -- ) 40 setenv ; inline
2008-02-18 08:30:16 -05:00
PRIVATE>
2008-02-18 06:07:40 -05:00
: <thread> ( quot name error-handler -- thread )
\ thread counter {
2008-02-18 06:07:40 -05:00
set-thread-quot
set-thread-name
set-thread-error-handler
set-thread-id
} \ thread construct ;
: run-queue 42 getenv ;
: sleep-queue 43 getenv ;
2008-02-18 06:07:40 -05:00
: resume ( thread -- )
check-registered run-queue push-front ;
2008-02-18 06:07:40 -05:00
: resume-with ( obj thread -- )
check-registered 2array run-queue push-front ;
2008-02-18 06:07:40 -05:00
<PRIVATE
: schedule-sleep ( thread ms -- )
>r check-registered r> sleep-queue heap-push ;
2008-02-18 06:07:40 -05:00
: wake-up? ( heap -- ? )
dup heap-empty?
[ drop f ] [ heap-peek nip millis <= ] if ;
: wake-up ( -- )
sleep-queue
2008-02-18 06:07:40 -05:00
[ dup wake-up? ] [ dup heap-pop drop resume ] [ ] while
drop ;
: next ( -- )
walker-hook [
continue
] [
wake-up
run-queue pop-back
2008-02-18 06:07:40 -05:00
dup array? [ first2 ] [ f swap ] if dup set-self
dup thread-continuation
f rot set-thread-continuation
continue-with
] if* ;
PRIVATE>
: sleep-time ( -- ms )
{
{ [ run-queue dlist-empty? not ] [ 0 ] }
{ [ sleep-queue heap-empty? ] [ f ] }
{ [ t ] [ sleep-queue heap-peek nip millis [-] ] }
2008-02-18 06:07:40 -05:00
} cond ;
: stop ( -- )
self unregister-thread next ;
: suspend ( quot -- obj )
[
>r self [ set-thread-continuation ] keep r> call next
] curry callcc1 ; inline
: yield ( -- ) [ resume ] suspend drop ;
: sleep ( ms -- )
>fixnum millis + [ schedule-sleep ] curry suspend drop ;
: (spawn) ( thread -- )
[
resume [
dup set-self
dup register-thread
init-namespaces
V{ } set-catchstack
{ } set-retainstack
>r { } set-datastack r>
thread-quot [ call stop ] call-clear
] 1 (throw)
] suspend 2drop ;
: spawn ( quot name -- thread )
[
global [
"Error in thread " write
dup thread-id pprint
" (" write
dup thread-name pprint ")" print
"spawned to call " write
thread-quot short.
nl
print-error flush
] bind
] <thread>
[ (spawn) ] keep ;
2008-02-18 08:30:16 -05:00
: spawn-server ( quot name -- thread )
>r [ [ ] [ ] while ] curry r> spawn ;
: in-thread ( quot -- )
>r datastack namestack r>
[ >r set-namestack set-datastack r> call ] 3curry
"Thread" spawn drop ;
2008-02-18 06:07:40 -05:00
<PRIVATE
: init-threads ( -- )
H{ } clone 41 setenv
<dlist> 42 setenv
<min-heap> 43 setenv
2008-02-18 06:07:40 -05:00
initial-thread global
[ drop f "Initial" [ die ] <thread> ] cache
f over set-thread-continuation
f over set-thread-registered?
dup register-thread
set-self ;
[ self dup thread-error-handler call stop ]
thread-error-hook set-global
PRIVATE>
2008-02-18 08:30:16 -05:00
[ init-threads ] "threads" add-init-hook