threads: fix thread-local variables
parent
9ffe0a69d1
commit
51c7e1e1e6
|
@ -40,3 +40,19 @@ yield
|
||||||
[ "a" [ 1 1 + ] spawn 100 sleep ] must-fail
|
[ "a" [ 1 1 + ] spawn 100 sleep ] must-fail
|
||||||
|
|
||||||
[ ] [ 0.1 seconds sleep ] unit-test
|
[ ] [ 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
|
||||||
|
|
|
@ -36,10 +36,10 @@ sleep-entry ;
|
||||||
|
|
||||||
! Thread-local storage
|
! Thread-local storage
|
||||||
: tnamespace ( -- assoc )
|
: tnamespace ( -- assoc )
|
||||||
self variables>> [ H{ } clone dup self (>>variables) ] unless* ;
|
self variables>> ; inline
|
||||||
|
|
||||||
: tget ( key -- value )
|
: tget ( key -- value )
|
||||||
self variables>> at ;
|
tnamespace at ;
|
||||||
|
|
||||||
: tset ( value key -- )
|
: tset ( value key -- )
|
||||||
tnamespace set-at ;
|
tnamespace set-at ;
|
||||||
|
@ -83,7 +83,8 @@ PRIVATE>
|
||||||
swap >>name
|
swap >>name
|
||||||
swap >>quot
|
swap >>quot
|
||||||
\ thread counter >>id
|
\ thread counter >>id
|
||||||
<box> >>continuation ; inline
|
<box> >>continuation
|
||||||
|
H{ } clone >>variables ; inline
|
||||||
|
|
||||||
: <thread> ( quot name -- thread )
|
: <thread> ( quot name -- thread )
|
||||||
\ thread new-thread ;
|
\ thread new-thread ;
|
||||||
|
|
Loading…
Reference in New Issue