LockPerf benchmark and related fixes (nested closures)

This commit is contained in:
Alex Tkachman
2011-12-08 16:24:41 +02:00
parent b89956f1fd
commit 42a0062b79
3 changed files with 36 additions and 18 deletions
+28 -17
View File
@@ -15,36 +15,47 @@ fun thread(f: fun ()) {
thread.start()
}
fun <T> Int.latch(op: fun CountDownLatch.() : T) : T {
val cdl = CountDownLatch(this)
val res = cdl.op()
cdl.await()
return res
}
fun Int.times(action: fun Int.()) {
for(i in 0..this-1)
action()
}
fun main(args: Array<String>) {
val processors = Runtime.getRuntime().sure().availableProcessors()
var threadNum = 1
while(threadNum <= 1024) {
val counter = AtomicInteger()
val cdl = CountDownLatch(threadNum)
val lock = ReentrantLock()
val start = System.currentTimeMillis()
for(i in 0..threadNum-1) {
thread {
while(true) {
lock.lock()
try {
if (counter.get() == 100000000) {
cdl.countDown();
break;
} else {
counter.incrementAndGet();
threadNum.latch{
val lock = ReentrantLock()
threadNum.times {
thread {
while(true) {
lock.lock()
try {
if (counter.get() == 100000000) {
countDown();
break;
} else {
counter.incrementAndGet();
}
}
finally {
lock.unlock()
}
}
finally {
lock.unlock()
}
}
}
}
cdl.await()
System.out?.println(threadNum.toString() + " " + (System.currentTimeMillis() - start));
if(threadNum < 2 * processors)