]> gitweb.factorcode.org Git - factor.git/commitdiff
threads: fix thread-local variables
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 28 Mar 2010 16:33:41 +0000 (12:33 -0400)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sun, 28 Mar 2010 16:33:41 +0000 (12:33 -0400)
basis/threads/threads-tests.factor
basis/threads/threads.factor

index 6e573ccd884113ada0deeb14d763faad810d7b89..742ecaa1f778ae5731c16455f637f06b817e2734 100644 (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
index 09869924f425d9464f8317697a073a4a828ccea7..89a90f87fd7890e481073e5c5a3c65137ae8287f 100644 (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 ;