threads: fix thread-local variables

release
Slava Pestov 2010-03-28 12:33:41 -04:00
parent 9ffe0a69d1
commit 51c7e1e1e6
2 changed files with 20 additions and 3 deletions

View File

@ -40,3 +40,19 @@ yield
[ "a" [ 1 1 + ] spawn 100 sleep ] must-fail
[ ] [ 0.1 seconds sleep ] unit-test
! Test thread-local variables
<promise> "p" set
5 "x" tset
[ 5 ] [ "x" tget ] unit-test
[ ] [ "x" [ 1 + ] tchange ] unit-test
[ 6 ] [ "x" tget ] unit-test
! Are they truly thread-local?
[ "x" tget "p" get fulfill ] in-thread
[ f ] [ "p" get ?promise ] unit-test

View File

@ -36,10 +36,10 @@ sleep-entry ;
! Thread-local storage
: tnamespace ( -- assoc )
self variables>> [ H{ } clone dup self (>>variables) ] unless* ;
self variables>> ; inline
: tget ( key -- value )
self variables>> at ;
tnamespace at ;
: tset ( value key -- )
tnamespace set-at ;
@ -83,7 +83,8 @@ PRIVATE>
swap >>name
swap >>quot
\ thread counter >>id
<box> >>continuation ; inline
<box> >>continuation
H{ } clone >>variables ; inline
: <thread> ( quot name -- thread )
\ thread new-thread ;