Permalink
Browse files

give transaction a name

  • Loading branch information...
xumingming committed Jun 19, 2013
1 parent 216d23e commit 7f4703b7c6a356cf8ada60ac47b0821054cc4862
Showing with 47 additions and 12 deletions.
  1. +13 −0 src/clj/clojure/core.clj
  2. +34 −12 src/jvm/clojure/lang/LockingTransaction.java
View
@@ -2247,6 +2247,13 @@
`(. clojure.lang.LockingTransaction `(. clojure.lang.LockingTransaction
(runInTransaction (fn [] ~@body)))) (runInTransaction (fn [] ~@body))))
(defmacro sync_xumm
"xumm's sync"
{:added "1.0"}
[txname & body]
`(. clojure.lang.LockingTransaction
(runInTransaction (fn [] ~@body) ~txname)))
(defmacro io! (defmacro io!
"If an io! block occurs in a transaction, throws an "If an io! block occurs in a transaction, throws an
@@ -4476,6 +4483,12 @@
[& exprs] [& exprs]
`(sync nil ~@exprs)) `(sync nil ~@exprs))
(defmacro dosync_xumm
"xumm's dosync"
{:added "1.0"}
[[txname] & exprs]
`(sync_xumm ~txname ~@exprs))
(defmacro with-precision (defmacro with-precision
"Sets the precision and rounding mode to be used for BigDecimal operations. "Sets the precision and rounding mode to be used for BigDecimal operations.
@@ -52,6 +52,14 @@
static class AbortException extends Exception{ static class AbortException extends Exception{
} }
public LockingTransaction() {}
private String name;
public LockingTransaction(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
/** /**
* transaction信息 * transaction信息
*/ */
@@ -164,7 +172,7 @@ void tryWriteLock(Ref ref){
{ {
if(!ref.lock.writeLock().tryLock(LOCK_WAIT_MSECS, TimeUnit.MILLISECONDS)) if(!ref.lock.writeLock().tryLock(LOCK_WAIT_MSECS, TimeUnit.MILLISECONDS))
{ {
System.out.println("[tx: " + this.hashCode() + "] LockingTransaction#tryWriteLock: retry because tryWriteLock timeout"); System.out.println("[" + this.name + "] LockingTransaction#tryWriteLock: retry because tryWriteLock timeout");
throw retryex; throw retryex;
} }
} }
@@ -191,7 +199,7 @@ Object lock(Ref ref){
unlocked = false; unlocked = false;
if(ref.tvals != null && ref.tvals.point > readPoint) { if(ref.tvals != null && ref.tvals.point > readPoint) {
System.out.println("[tx: " + this.hashCode() + "] LockingTransaction#lock: retry because ref modified after this tx begins"); System.out.println("[" + this.name + "] LockingTransaction#lock: retry because ref modified after this tx begins");
throw retryex; throw retryex;
} }
@@ -315,17 +323,31 @@ static LockingTransaction getRunning(){
* @return * @return
* @throws Exception * @throws Exception
*/ */
static public Object runInTransaction(Callable fn) throws Exception{ static public Object runInTransaction(Callable fn, String name) throws Exception{
LockingTransaction t = transaction.get(); LockingTransaction t = transaction.get();
if(t == null) if(t == null) {
transaction.set(t = new LockingTransaction()); if (name == null) {
t = new LockingTransaction();
} else {
t = new LockingTransaction(name);
}
transaction.set(t);
} else {
if (name != null) {
t.setName(name);
}
}
if(t.info != null) if(t.info != null)
return fn.call(); return fn.call();
return t.run(fn); return t.run(fn);
} }
static public Object runInTransaction(Callable fn) throws Exception {
return runInTransaction(fn, null);
}
/** /**
* notify信息,包含发生变化的ref,它的新值和旧值 * notify信息,包含发生变化的ref,它的新值和旧值
*/ */
@@ -355,7 +377,7 @@ Object run(Callable fn) throws Exception{
for(int i = 0; !done && i < RETRY_LIMIT; i++) for(int i = 0; !done && i < RETRY_LIMIT; i++)
{ {
System.out.println("[tx:" + this.hashCode() + "] round:" + i); System.out.println("[" + this.name + "] round:" + i);
try try
{ {
getReadPoint(); getReadPoint();
@@ -384,14 +406,14 @@ Object run(Callable fn) throws Exception{
try { try {
tryWriteLock(ref); tryWriteLock(ref);
} catch (RetryEx e1) { } catch (RetryEx e1) {
System.out.println("[tx: " + this.hashCode() + "] LockingTransaction#run: retry because cannt get write lock"); System.out.println("[" + this.name + "] LockingTransaction#run: retry because cannt get write lock");
throw e1; throw e1;
} }
locked.add(ref); locked.add(ref);
// 对于ensure的ref,但是在snapshot之后又有人写了,那么重试 // 对于ensure的ref,但是在snapshot之后又有人写了,那么重试
if(wasEnsured && ref.tvals != null && ref. tvals.point > readPoint) { if(wasEnsured && ref.tvals != null && ref. tvals.point > readPoint) {
System.out.println("[tx: " + this.hashCode() + "] LockingTransaction#run: retry because ref is ensured but modified by other tx"); System.out.println("[" + this.name + "] LockingTransaction#run: retry because ref is ensured but modified by other tx");
throw retryex; throw retryex;
} }
@@ -404,7 +426,7 @@ Object run(Callable fn) throws Exception{
if(refinfo != null && refinfo != info && refinfo.running()) if(refinfo != null && refinfo != info && refinfo.running())
{ {
if(!barge(refinfo)) if(!barge(refinfo))
System.out.println("[tx: " + this.hashCode() + "] LockingTransaction#run: retry because other tx are modifing the ref"); System.out.println("[" + this.name + "] LockingTransaction#run: retry because other tx are modifing the ref");
throw retryex; throw retryex;
} }
Object val = ref.tvals == null ? null : ref.tvals.val; Object val = ref.tvals == null ? null : ref.tvals.val;
@@ -426,7 +448,7 @@ Object run(Callable fn) throws Exception{
try { try {
tryWriteLock(ref); tryWriteLock(ref);
} catch (RetryEx e1) { } catch (RetryEx e1) {
System.out.println("[tx: " + this.hashCode() + "] LockingTransaction#run: retry because cannt get write lock"); System.out.println("[" + this.name + "] LockingTransaction#run: retry because cannt get write lock");
throw e1; throw e1;
} }
locked.add(ref); locked.add(ref);
@@ -596,7 +618,7 @@ Object doSet(Ref ref, Object val){
try { try {
lock(ref); lock(ref);
} catch (RetryEx e) { } catch (RetryEx e) {
System.out.println("[tx:" + this.hashCode() + "] LockingTransaction#doSet: retry because cannt get write lock"); System.out.println("[" + this.name + "] LockingTransaction#doSet: retry because cannt get write lock");
throw e; throw e;
} }
} }
@@ -620,7 +642,7 @@ void doEnsure(Ref ref){
// 如果在我们事务开始之后有人写过这个ref,那么我们释放读锁,并且抛出重试异常 // 如果在我们事务开始之后有人写过这个ref,那么我们释放读锁,并且抛出重试异常
if(ref.tvals != null && ref.tvals.point > readPoint) { if(ref.tvals != null && ref.tvals.point > readPoint) {
ref.lock.readLock().unlock(); ref.lock.readLock().unlock();
System.out.println("[tx:" + this.hashCode() + "] LockingTransaction#doEnsure: retry because ref modified after this tx begins"); System.out.println("[" + this.name + "] LockingTransaction#doEnsure: retry because ref modified after this tx begins");
throw retryex; throw retryex;
} }

0 comments on commit 7f4703b

Please sign in to comment.