Skip to content

Commit

Permalink
Fix while-committing callback ordering semantics.
Browse files Browse the repository at this point in the history
  • Loading branch information
nbronson committed Dec 14, 2010
1 parent 43766fe commit bbfb52a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/scala/scala/concurrent/stm/ccstm/InTxnImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ private[ccstm] class InTxnImpl extends AccessHistory with skel.AbstractInTxn {
return false
}

commitWrites(cv)
_pendingFailure = fireWhileCommittingCallbacks(exec)
commitWrites(cv)
root.setCommitted()

return true
Expand Down
45 changes: 45 additions & 0 deletions src/test/scala/scala/concurrent/stm/TxnSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,51 @@ class TxnSuite extends FunSuite {
assert(count == 1)
}

test("whileCommitting ordering") {
val numThreads = 10
val numPutsPerThread = 100000
val startingGate = new java.util.concurrent.CountDownLatch(1)
val active = Ref(numThreads)
val failure = Ref(null : Throwable)

val x = Ref(0)
val notifier = new scala.concurrent.forkjoin.LinkedTransferQueue[Int]()
val EOF = -1

for (i <- 1 to numThreads) {
(new Thread {
override def run() {
try {
startingGate.await()
for (i <- 1 to numPutsPerThread) {
atomic { implicit txn =>
x() = x() + 1
val y = x()
Txn.whileCommitting { _ =>
if ((i & 127) == 0) // try to perturb the timing
Thread.`yield`
notifier.put(y)
}
}
}
} catch {
case xx => failure.single() = xx
}
if (active.single.transformAndGet( _ - 1 ) == 0)
notifier.put(EOF)
}
}).start
}

startingGate.countDown
for (expected <- 1 to numThreads * numPutsPerThread)
assert(expected === notifier.take())
assert(EOF === notifier.take())

if (failure.single() != null)
throw failure.single()
}

// TODO: more whilePreparing and whileCommitting callback usage

// TODO: exception behavior from all types of callbacks
Expand Down

0 comments on commit bbfb52a

Please sign in to comment.