From 51c7e1e1e64b123acebc4a70c8b8537d6acdf13d Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sun, 28 Mar 2010 12:33:41 -0400 Subject: [PATCH] threads: fix thread-local variables --- basis/threads/threads-tests.factor | 16 ++++++++++++++++ basis/threads/threads.factor | 7 ++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/basis/threads/threads-tests.factor b/basis/threads/threads-tests.factor index 6e573ccd88..742ecaa1f7 100644 --- a/basis/threads/threads-tests.factor +++ b/basis/threads/threads-tests.factor @@ -40,3 +40,19 @@ yield [ "a" [ 1 1 + ] spawn 100 sleep ] must-fail [ ] [ 0.1 seconds sleep ] unit-test + +! Test thread-local variables + "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 diff --git a/basis/threads/threads.factor b/basis/threads/threads.factor index 09869924f4..89a90f87fd 100644 --- a/basis/threads/threads.factor +++ b/basis/threads/threads.factor @@ -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 - >>continuation ; inline + >>continuation + H{ } clone >>variables ; inline : ( quot name -- thread ) \ thread new-thread ;