-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
core/src/main/kotlin/cmu/pasta/sfuzz/core/concurrency/locks/CountDownLatchContext.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmu.pasta.sfuzz.core.concurrency.locks | ||
|
||
import cmu.pasta.sfuzz.core.GlobalContext | ||
import cmu.pasta.sfuzz.core.ThreadState | ||
import cmu.pasta.sfuzz.core.concurrency.operations.ThreadResumeOperation | ||
|
||
class CountDownLatchContext(var count: Long) { | ||
val latchWaiters = mutableSetOf<Long>() | ||
fun await(): Boolean { | ||
if (count > 0) { | ||
latchWaiters.add(Thread.currentThread().id) | ||
return true | ||
} | ||
assert(count == 0L) | ||
return false | ||
} | ||
|
||
fun countDown() { | ||
// If count is already zero we do not need to | ||
// do anything | ||
if (count == 0L) { | ||
return | ||
} | ||
count -= 1 | ||
if (count == 0L) { | ||
for (tid in latchWaiters) { | ||
GlobalContext.registeredThreads[tid]!!.pendingOperation = ThreadResumeOperation() | ||
GlobalContext.registeredThreads[tid]!!.state = ThreadState.Enabled | ||
} | ||
} | ||
return | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
core/src/main/kotlin/cmu/pasta/sfuzz/core/concurrency/locks/CountDownLatchManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cmu.pasta.sfuzz.core.concurrency.locks | ||
|
||
import java.util.concurrent.CountDownLatch | ||
|
||
class CountDownLatchManager { | ||
val latchStore = ReferencedContextManager {it -> | ||
if (it is CountDownLatch) { | ||
CountDownLatchContext(it.count) | ||
} else { | ||
throw IllegalArgumentException("CountDownLatchManager can only manage CountDownLatch objects") | ||
} | ||
} | ||
|
||
fun await(latch: CountDownLatch): Boolean { | ||
return latchStore.getLockContext(latch).await() | ||
} | ||
|
||
fun countDown(latch: CountDownLatch) { | ||
latchStore.getLockContext(latch).countDown() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...on/src/main/kotlin/cmu/pasta/sfuzz/instrumentation/visitors/CountDownLatchInstrumenter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmu.pasta.sfuzz.instrumentation.visitors | ||
|
||
import cmu.pasta.sfuzz.runtime.Runtime | ||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.MethodVisitor | ||
import java.util.concurrent.CountDownLatch | ||
|
||
class CountDownLatchInstrumenter(cv: ClassVisitor): ClassVisitorBase(cv, CountDownLatch::class.java.name) { | ||
override fun instrumentMethod( | ||
mv: MethodVisitor, | ||
access: Int, | ||
name: String, | ||
descriptor: String, | ||
signature: String?, | ||
exceptions: Array<out String>? | ||
): MethodVisitor { | ||
if (name == "await") { | ||
val eMv = MethodEnterVisitor(mv, Runtime::onLatchAwait, access, name, descriptor, true, false) | ||
return MethodExitVisitor(eMv, Runtime::onLatchAwaitDone, access, name, descriptor, true, false) | ||
} | ||
if (name == "countDown") { | ||
val eMv = MethodEnterVisitor(mv, Runtime::onLatchCountDown, access, name, descriptor, true, false) | ||
return MethodExitVisitor(eMv, Runtime::onLatchCountDownDone, access, name, descriptor, true, false) | ||
} | ||
return super.instrumentMethod(mv, access, name, descriptor, signature, exceptions) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
integration-tests/src/test/java/cmu/pasta/sfuzz/it/core/CountDownLatchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cmu.pasta.sfuzz.it.core; | ||
|
||
import cmu.pasta.sfuzz.it.IntegrationTestRunner; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class CountDownLatchTest extends IntegrationTestRunner { | ||
|
||
@Test | ||
public void testCountDown() { | ||
String expected = "[1]: WORKER-1 finished\n" + | ||
"[2]: WORKER-2 finished\n" + | ||
"[3]: WORKER-3 finished\n" + | ||
"[4]: WORKER-4 finished\n" + | ||
"[0]: Test worker has finished\n"; | ||
assertEquals(expected, runTest("testCountDown")); | ||
} | ||
|
||
@Test | ||
public void testAwaitAfterCountDown() { | ||
String expected = "[0]: Test worker has finished\n"; | ||
assertEquals(expected, runTest("testAwaitAfterCountDown")); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
integration-tests/src/test/java/example/CountDownLatchTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package example; | ||
|
||
import jdk.jshell.execution.Util; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
public class CountDownLatchTest { | ||
public static void testCountDown() throws InterruptedException { | ||
CountDownLatch latch = new CountDownLatch(4); | ||
Worker first = new Worker(1000, latch, | ||
"WORKER-1"); | ||
Worker second = new Worker(2000, latch, | ||
"WORKER-2"); | ||
Worker third = new Worker(3000, latch, | ||
"WORKER-3"); | ||
Worker fourth = new Worker(4000, latch, | ||
"WORKER-4"); | ||
first.start(); | ||
second.start(); | ||
third.start(); | ||
fourth.start(); | ||
latch.await(); | ||
Utils.log(Thread.currentThread().getName() + | ||
" has finished"); | ||
} | ||
|
||
public static void testAwaitAfterCountDown() throws InterruptedException { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
latch.countDown(); | ||
latch.await(); | ||
Utils.log(Thread.currentThread().getName() + | ||
" has finished"); | ||
} | ||
} | ||
|
||
// A class to represent threads for which | ||
// the main thread waits. | ||
class Worker extends Thread | ||
{ | ||
private int delay; | ||
private CountDownLatch latch; | ||
|
||
public Worker(int delay, CountDownLatch latch, | ||
String name) | ||
{ | ||
super(name); | ||
this.delay = delay; | ||
this.latch = latch; | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
latch.countDown(); | ||
Utils.log(Thread.currentThread().getName() | ||
+ " finished"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters