-
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
25 changed files
with
512 additions
and
69 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
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
44 changes: 44 additions & 0 deletions
44
core/src/main/kotlin/cmu/pasta/sfuzz/core/concurrency/locks/SemaphoreContext.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,44 @@ | ||
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 SemaphoreContext(var totalPermits: Int) { | ||
private val lockWaiters = mutableMapOf<Long, Int>() | ||
|
||
fun acquire(permits: Int, shouldBlock: Boolean): Boolean { | ||
if (totalPermits > permits) { | ||
totalPermits -= permits | ||
return true | ||
} else if (shouldBlock) { | ||
lockWaiters[Thread.currentThread().id] = permits | ||
} | ||
return false | ||
} | ||
|
||
fun drainPermits(): Int { | ||
val permits = totalPermits | ||
totalPermits = 0 | ||
return permits | ||
} | ||
|
||
fun release(permits: Int) { | ||
totalPermits += permits | ||
if (totalPermits > 0) { | ||
val it = lockWaiters.iterator() | ||
while (it.hasNext()) { | ||
val (tid, p) = it.next() | ||
if (totalPermits >= p) { | ||
GlobalContext.registeredThreads[tid]!!.pendingOperation = ThreadResumeOperation() | ||
GlobalContext.registeredThreads[tid]!!.state = ThreadState.Enabled | ||
lockWaiters.remove(tid) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun reducePermits(permits: Int) { | ||
totalPermits -= permits | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
core/src/main/kotlin/cmu/pasta/sfuzz/core/concurrency/locks/SemaphoreManager.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 java.util.concurrent.Semaphore | ||
|
||
class SemaphoreManager { | ||
val lockContextManager = ReferencedContextManager {it -> | ||
if (it is Semaphore) { | ||
SemaphoreContext(it.availablePermits()) | ||
} else { | ||
throw IllegalArgumentException("SemaphoreManager can only manage Semaphore objects") | ||
} | ||
} | ||
|
||
fun init(sem: Semaphore) { | ||
val context = SemaphoreContext(sem.availablePermits()) | ||
lockContextManager.addContext(sem, context) | ||
} | ||
|
||
fun acquire(sem: Semaphore, permits: Int, shouldBlock: Boolean): Boolean { | ||
return lockContextManager.getLockContext(sem).acquire(permits, shouldBlock) | ||
} | ||
|
||
fun release(sem: Semaphore, permits: Int) { | ||
lockContextManager.getLockContext(sem).release(permits) | ||
} | ||
|
||
fun drainPermits(sem: Semaphore): Int { | ||
return lockContextManager.getLockContext(sem).drainPermits() | ||
} | ||
|
||
fun reducePermits(sem: Semaphore, permits: Int) { | ||
lockContextManager.getLockContext(sem).reducePermits(permits) | ||
} | ||
} |
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
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
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
Oops, something went wrong.