Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbid casting Mutex to a Semaphore #4176

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/common/src/sync/Mutex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public suspend inline fun <T> Mutex.withLock(owner: Any? = null, action: () -> T
}


internal open class MutexImpl(locked: Boolean) : SemaphoreImpl(1, if (locked) 1 else 0), Mutex {
internal open class MutexImpl(locked: Boolean) : SemaphoreAndMutexImpl(1, if (locked) 1 else 0), Mutex {
/**
* After the lock is acquired, the corresponding owner is stored in this field.
* The [unlock] operation checks the owner and either re-sets it to [NO_OWNER],
Expand Down
16 changes: 10 additions & 6 deletions kotlinx-coroutines-core/common/src/sync/Semaphore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public suspend inline fun <T> Semaphore.withPermit(action: () -> T): T {
}

@Suppress("UNCHECKED_CAST")
internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int) : Semaphore {
internal open class SemaphoreAndMutexImpl(private val permits: Int, acquiredPermits: Int) {
/*
The queue of waiting acquirers is essentially an infinite array based on the list of segments
(see `SemaphoreSegment`); each segment contains a fixed number of slots. To determine a slot for each enqueue
Expand Down Expand Up @@ -144,11 +144,11 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
* cannot be greater than 2^31 in any real application.
*/
private val _availablePermits = atomic(permits - acquiredPermits)
override val availablePermits: Int get() = max(_availablePermits.value, 0)
val availablePermits: Int get() = max(_availablePermits.value, 0)

private val onCancellationRelease = { _: Throwable, _: Unit, _: CoroutineContext -> release() }

override fun tryAcquire(): Boolean {
fun tryAcquire(): Boolean {
while (true) {
// Get the current number of available permits.
val p = _availablePermits.value
Expand All @@ -167,7 +167,7 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
}
}

override suspend fun acquire() {
suspend fun acquire() {
// Decrement the number of available permits.
val p = decPermits()
// Is the permit acquired?
Expand Down Expand Up @@ -239,7 +239,7 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
}
}

override fun release() {
fun release() {
while (true) {
// Increment the number of available permits.
val p = _availablePermits.getAndIncrement()
Expand Down Expand Up @@ -346,12 +346,16 @@ internal open class SemaphoreImpl(private val permits: Int, acquiredPermits: Int
} else false
}
is SelectInstance<*> -> {
trySelect(this@SemaphoreImpl, Unit)
trySelect(this@SemaphoreAndMutexImpl, Unit)
}
else -> error("unexpected: $this")
}
}

private class SemaphoreImpl(
permits: Int, acquiredPermits: Int
): SemaphoreAndMutexImpl(permits, acquiredPermits), Semaphore

private fun createSegment(id: Long, prev: SemaphoreSegment?) = SemaphoreSegment(id, prev, 0)

private class SemaphoreSegment(id: Long, prev: SemaphoreSegment?, pointers: Int) : Segment<SemaphoreSegment>(id, prev, pointers) {
Expand Down
5 changes: 5 additions & 0 deletions kotlinx-coroutines-core/common/test/sync/MutexTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,9 @@ class MutexTest : TestBase() {
}
}
}

@Test
fun testMutexIsNotSemaphore() {
assertIsNot<Semaphore>(Mutex())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.strategy.managed.modelchecking.*

abstract class SemaphoreLincheckTestBase(permits: Int) : AbstractLincheckTest() {
private val semaphore = SemaphoreImpl(permits = permits, acquiredPermits = 0)
private val semaphore = SemaphoreAndMutexImpl(permits = permits, acquiredPermits = 0)

@Operation
fun tryAcquire() = semaphore.tryAcquire()
Expand Down