I still try to find a way to make threads inherit thread-local
bindings established via binding in the parent thread. I searched for
a function to get the thread bindings but couldn't find one. Did I
miss anything? So I added getThreadBindings() to Var.java:
diff --git a/trunk/src/jvm/clojure/lang/Var.java
b/trunk/src/jvm/clojure/lang/Var.java
index fcbd746..59825a5 100644
--- a/trunk/src/jvm/clojure/lang/Var.java
+++ b/trunk/src/jvm/clojure/lang/Var.java
@@ -282,6 +282,18 @@ public static void releaseThreadBindings(){
dvals.set(null);
}
+public static Associative getThreadBindings(){
+ Frame f = dvals.get();
+ Associative vars = PersistentHashMap.EMPTY;
+ for(ISeq bs = RT.keys(f.frameBindings); bs != null; bs = bs.rest())
+ {
+ Var v = (Var) bs.first();
+ if (v.sym != null)
+ vars = vars.assoc(v.sym, v);
+ }
+ return vars;
+}
+
final Box getThreadBinding(){
if(count.get() > 0)
{
and with the following two functions I can finally inherit thread
local bindings.
(defn bindings []
"Return key-value list of thread-local bindings."
(reduce
(fn [env [n v]] (conj env n (var-get v)))
[]
(clojure.lang.Var/getThreadBindings)))
(defmacro on-thread
"Apply the given function on another thread,
optionally with the given bindings."
([f]
`(let [env# (bindings)]
(eval `(on-thread ~env# ~~f))))
([env f]
`(doto (new Thread #(binding ~env (~f))) (start))))
user=> (def x)
#'user/x
user=> (doto (new Thread #(prn (bindings))) (start))
Thread[Thread-0,5,main]
user=> []
user=> (binding [x 12] (on-thread #(prn x)))
12
Thread[Thread-2,5,]
user=>
Not sure if the on-thread macro is kosher. Anyway, would it be
possible to add something like getThreadBindings to Var.java or point
out the right way to get the thread-local bindings?
Thanks,
nt
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---