summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cli/runtime/RT.cs82
-rw-r--r--src/cli/runtime/ThreadLocalData.cs75
-rw-r--r--src/cli/runtime/Transaction.cs25
-rw-r--r--src/org/clojure/runtime/RT.java80
-rw-r--r--src/org/clojure/runtime/ThreadLocalData.java54
-rw-r--r--src/org/clojure/runtime/Transaction.java29
6 files changed, 78 insertions, 267 deletions
diff --git a/src/cli/runtime/RT.cs b/src/cli/runtime/RT.cs
index 06455b47..fcae27b6 100644
--- a/src/cli/runtime/RT.cs
+++ b/src/cli/runtime/RT.cs
@@ -290,81 +290,13 @@ static public bool isLineNumberingReader(TextReader r)
/*-------------------------------- values --------------*/
-static public Object setValues(ThreadLocalData tld, Object arg1)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 1;
- tld.mvArray[0] = arg1;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 2;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2, Object arg3)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 3;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- tld.mvArray[2] = arg3;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 4;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- tld.mvArray[2] = arg3;
- tld.mvArray[3] = arg4;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4,
- Object arg5)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 5;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- tld.mvArray[2] = arg3;
- tld.mvArray[3] = arg4;
- tld.mvArray[4] = arg5;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4,
- Object arg5, ISeq args) /**/
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 5;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- tld.mvArray[2] = arg3;
- tld.mvArray[3] = arg4;
- tld.mvArray[4] = arg5;
- for(int i = 5; args != null && i < ThreadLocalData.MULTIPLE_VALUES_LIMIT; i++, args = args.rest())
- {
- tld.mvArray[i] = args.first();
- }
- if(args != null)
- throw new ArgumentException("Too many arguments to values (> ThreadLocalData.MULTIPLE_VALUES_LIMIT)");
- return arg1;
- }
+static public Object setValues(params Object[] vals)
+ {
+ ThreadLocalData.setValues(vals);
+ if(vals.Length > 0)
+ return vals[0];
+ return null;
+ }
}
} \ No newline at end of file
diff --git a/src/cli/runtime/ThreadLocalData.cs b/src/cli/runtime/ThreadLocalData.cs
index 3b57c632..f48f3cc4 100644
--- a/src/cli/runtime/ThreadLocalData.cs
+++ b/src/cli/runtime/ThreadLocalData.cs
@@ -15,60 +15,29 @@ using System.Collections.Specialized;
namespace org.clojure.runtime
{
-public class ThreadLocalData{
-
-public const int MULTIPLE_VALUES_LIMIT = 20;
-public int mvCount = 0;
-public Object[] mvArray = new Object[MULTIPLE_VALUES_LIMIT];
-
-internal HybridDictionary dynamicBindings = new HybridDictionary();
-
-internal Transaction transaction;
+public class ThreadLocalData{
+
+[ThreadStatic]
+private static Transaction transaction;
+[ThreadStatic]
+private static Object[] values;
+
+static public Object[] getValues(){
+ return values;
+}
+
+static public void setValues(Object[] vals) {
+ values = vals;
+}
+
+static public Transaction getTransaction() {
+ return transaction;
+}
+
+static public void setTransaction(Transaction t){
+ transaction = t;
+}
-public Transaction getTransaction() {
- if(transaction == null)
- throw new Exception("No active transaction");
- return transaction;
-}
-
-public ThreadLocalData(HybridDictionary dynamicBindings)
- {
- this.mvCount = 0;
- this.mvArray = new Object[MULTIPLE_VALUES_LIMIT];
- this.dynamicBindings = dynamicBindings;
- }
-
-public ThreadLocalData():
- this(new HybridDictionary())
- {
- }
-
-public static ThreadLocalData get()
- {
- if(tld == null)
- tld = new ThreadLocalData();
- return tld;
- }
-
-/*
-note this is not the same semantics as InheritableThreadLocal - aargh
-might need to make Java side non-inheritable
-*/
-[ThreadStatic]
- static ThreadLocalData tld;
- /* was this in Java
-static InheritableThreadLocal tld = new InheritableThreadLocal(){
- protected Object childValue(Object object)
- {
- return new ThreadLocalData((HybridDictionary) ((ThreadLocalData) object).dynamicBindings.clone());
- }
-
- protected Object initialValue()
- {
- return new ThreadLocalData();
- }
-};
-*/
}
}
diff --git a/src/cli/runtime/Transaction.cs b/src/cli/runtime/Transaction.cs
index dc328ab7..a64e7f1c 100644
--- a/src/cli/runtime/Transaction.cs
+++ b/src/cli/runtime/Transaction.cs
@@ -51,42 +51,43 @@ Dictionary<TRef,ISeq> commutates;
static public Object runInTransaction(ThreadLocalData tld,IFn fn) {
- if(tld.transaction != null)
+ if(ThreadLocalData.getTransaction() != null)
return fn.invoke(tld);
- tld.transaction = new Transaction();
+ Transaction t = new Transaction();
+ ThreadLocalData.setTransaction(t);
try{
- return tld.transaction.run(tld, fn);
+ return t.run(fn);
}
finally{
- tld.transaction = null;
+ ThreadLocalData.setTransaction(null);
}
}
static public TRef tref(Object val) {
- Transaction trans = ThreadLocalData.get().getTransaction();
+ Transaction trans = ThreadLocalData.getTransaction();
TRef tref = new TRef();
trans.set(tref, val);
return tref;
}
static public Object get2(TRef tref) {
- return ThreadLocalData.get().getTransaction().get(tref);
+ return ThreadLocalData.getTransaction().get(tref);
}
static public Object set2(TRef tref, Object val) {
- return ThreadLocalData.get().getTransaction().set(tref,val);
+ return ThreadLocalData.getTransaction().set(tref,val);
}
static public void touch2(TRef tref) {
- ThreadLocalData.get().getTransaction().touch(tref);
+ ThreadLocalData.getTransaction().touch(tref);
}
static public void commutate2(TRef tref, IFn fn) {
- ThreadLocalData.get().getTransaction().commutate(tref, fn);
+ ThreadLocalData.getTransaction().commutate(tref, fn);
}
-Object run(ThreadLocalData tld, IFn fn) {
+Object run(IFn fn) {
bool done = false;
Object ret = null;
List<TRef> locks = null;
@@ -95,7 +96,7 @@ Object run(ThreadLocalData tld, IFn fn) {
while(!done){
try
{
- ret = fn.invoke(tld);
+ ret = fn.invoke();
if(locks == null && (sets != null || commutates != null))
locks = new List<TRef>();
if(sets != null)
@@ -133,7 +134,7 @@ Object run(ThreadLocalData tld, IFn fn) {
for(ISeq c = e.Value;c!=null;c = c.rest())
{
IFn f = (IFn) c.first();
- val = f.invoke(tld, val);
+ val = f.invoke(val);
}
sets[tref] = val;
}
diff --git a/src/org/clojure/runtime/RT.java b/src/org/clojure/runtime/RT.java
index f051f345..33c86974 100644
--- a/src/org/clojure/runtime/RT.java
+++ b/src/org/clojure/runtime/RT.java
@@ -308,80 +308,12 @@ static public boolean isLineNumberingReader(Reader r) {
///////////////////////////////// values //////////////////////////
-static public Object setValues(ThreadLocalData tld, Object arg1)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 1;
- tld.mvArray[0] = arg1;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 2;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2, Object arg3)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 3;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- tld.mvArray[2] = arg3;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 4;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- tld.mvArray[2] = arg3;
- tld.mvArray[3] = arg4;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4,
- Object arg5)
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 5;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- tld.mvArray[2] = arg3;
- tld.mvArray[3] = arg4;
- tld.mvArray[4] = arg5;
- return arg1;
- }
-
-static public Object setValues(ThreadLocalData tld, Object arg1, Object arg2, Object arg3, Object arg4,
- Object arg5, ISeq args) throws Exception
- {
- if(tld == null)
- tld = ThreadLocalData.get();
- tld.mvCount = 5;
- tld.mvArray[0] = arg1;
- tld.mvArray[1] = arg2;
- tld.mvArray[2] = arg3;
- tld.mvArray[3] = arg4;
- tld.mvArray[4] = arg5;
- for(int i = 5; args != null && i < ThreadLocalData.MULTIPLE_VALUES_LIMIT; i++, args = args.rest())
- {
- tld.mvArray[i] = args.first();
- }
- if(args != null)
- throw new IllegalArgumentException("Too many arguments to values (> ThreadLocalData.MULTIPLE_VALUES_LIMIT)");
- return arg1;
+static public Object setValues(Object... vals)
+ {
+ ThreadLocalData.setValues(vals);
+ if(vals.length > 0)
+ return vals[0];
+ return null;
}
}
diff --git a/src/org/clojure/runtime/ThreadLocalData.java b/src/org/clojure/runtime/ThreadLocalData.java
index 0bc9c665..bb63d217 100644
--- a/src/org/clojure/runtime/ThreadLocalData.java
+++ b/src/org/clojure/runtime/ThreadLocalData.java
@@ -12,50 +12,26 @@
package org.clojure.runtime;
-import java.util.IdentityHashMap;
-
public class ThreadLocalData{
-final public static int MULTIPLE_VALUES_LIMIT = 20;
-public int mvCount = 0;
-public Object[] mvArray = new Object[MULTIPLE_VALUES_LIMIT];
+private static ThreadLocal<Transaction> transaction = new ThreadLocal<Transaction>();
+private static ThreadLocal<Object[]> values = new ThreadLocal<Object[]>();
+
+static public Object[] getValues(){
+ return values.get();
+}
+
+static public void setValues(Object[] vals) {
+ values.set(vals);
+}
-IdentityHashMap dynamicBindings = new IdentityHashMap();
-Transaction transaction;
+static public Transaction getTransaction() {
+ return transaction.get();
-public Transaction getTransaction() throws Exception{
- if(transaction == null)
- throw new Exception("No active transaction");
- return transaction;
}
-public ThreadLocalData(IdentityHashMap dynamicBindings)
- {
- this.mvCount = 0;
- this.mvArray = new Object[MULTIPLE_VALUES_LIMIT];
- this.dynamicBindings = dynamicBindings;
- }
-
-public ThreadLocalData()
- {
- this(new IdentityHashMap());
- }
-
-public static ThreadLocalData get()
- {
- return (ThreadLocalData) tld.get();
- }
-
-static InheritableThreadLocal tld = new InheritableThreadLocal(){
- protected Object childValue(Object object)
- {
- return new ThreadLocalData((IdentityHashMap) ((ThreadLocalData) object).dynamicBindings.clone());
- }
-
- protected Object initialValue()
- {
- return new ThreadLocalData();
- }
-};
+static public void setTransaction(Transaction t){
+ transaction.set(t);
+}
}
diff --git a/src/org/clojure/runtime/Transaction.java b/src/org/clojure/runtime/Transaction.java
index 08b3a5dc..e3239c2a 100644
--- a/src/org/clojure/runtime/Transaction.java
+++ b/src/org/clojure/runtime/Transaction.java
@@ -47,20 +47,21 @@ IdentityHashMap<TRef,Object> sets;
IdentityHashMap<TRef,ISeq> commutates;
-static public Object runInTransaction(ThreadLocalData tld,IFn fn) throws Exception{
- if(tld.transaction != null)
- return fn.invoke(tld);
- tld.transaction = new Transaction();
+static public Object runInTransaction(IFn fn) throws Exception{
+ if(ThreadLocalData.getTransaction() != null)
+ return fn.invoke();
+ Transaction t = new Transaction();
+ ThreadLocalData.setTransaction(t);
try{
- return tld.transaction.run(tld, fn);
+ return t.run(fn);
}
finally{
- tld.transaction = null;
+ ThreadLocalData.setTransaction(null);
}
}
static public TRef tref(Object val) throws Exception{
- Transaction trans = ThreadLocalData.get().getTransaction();
+ Transaction trans = ThreadLocalData.getTransaction();
TRef tref = new TRef();
trans.set(tref, val);
return tref;
@@ -68,23 +69,23 @@ static public TRef tref(Object val) throws Exception{
//*
static public Object get2(TRef tref) throws Exception{
- return ThreadLocalData.get().getTransaction().get(tref);
+ return ThreadLocalData.getTransaction().get(tref);
}
static public Object set2(TRef tref, Object val) throws Exception{
- return ThreadLocalData.get().getTransaction().set(tref,val);
+ return ThreadLocalData.getTransaction().set(tref,val);
}
static public void touch2(TRef tref) throws Exception{
- ThreadLocalData.get().getTransaction().touch(tref);
+ ThreadLocalData.getTransaction().touch(tref);
}
static public void commutate2(TRef tref, IFn fn) throws Exception{
- ThreadLocalData.get().getTransaction().commutate(tref, fn);
+ ThreadLocalData.getTransaction().commutate(tref, fn);
}
//*/
-Object run(ThreadLocalData tld, IFn fn) throws Exception{
+Object run(IFn fn) throws Exception{
boolean done = false;
Object ret = null;
ArrayList<TRef> locks = null;
@@ -94,7 +95,7 @@ Object run(ThreadLocalData tld, IFn fn) throws Exception{
while(!done){
try
{
- ret = fn.invoke(tld);
+ ret = fn.invoke();
if(locks == null && (sets != null || commutates != null))
locks = new ArrayList<TRef>();
if(sets != null)
@@ -132,7 +133,7 @@ Object run(ThreadLocalData tld, IFn fn) throws Exception{
for(ISeq c = e.getValue();c!=null;c = c.rest())
{
IFn f = (IFn) c.first();
- val = f.invoke(tld, val);
+ val = f.invoke(val);
}
sets.put(tref, val);
}