Skip to content

Mutex.withLock() added EXACTLY_ONCE contract #2010

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions kotlinx-coroutines-core/common/src/sync/Mutex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.internal.*
import kotlinx.coroutines.intrinsics.*
import kotlinx.coroutines.selects.*
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.coroutines.*
import kotlin.jvm.*
import kotlin.native.concurrent.*
Expand Down Expand Up @@ -106,7 +109,12 @@ public fun Mutex(locked: Boolean = false): Mutex =
*
* @return the return value of the action.
*/
@ExperimentalContracts
public suspend inline fun <T> Mutex.withLock(owner: Any? = null, action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

lock(owner)
try {
return action()
Expand Down
8 changes: 8 additions & 0 deletions kotlinx-coroutines-core/common/src/sync/Semaphore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package kotlinx.coroutines.sync
import kotlinx.atomicfu.*
import kotlinx.coroutines.*
import kotlinx.coroutines.internal.*
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.coroutines.*
import kotlin.math.*
import kotlin.native.concurrent.SharedImmutable
Expand Down Expand Up @@ -74,7 +77,12 @@ public fun Semaphore(permits: Int, acquiredPermits: Int = 0): Semaphore = Semaph
*
* @return the return value of the [action].
*/
@ExperimentalContracts
public suspend inline fun <T> Semaphore.withPermit(action: () -> T): T {
contract {
callsInPlace(action, InvocationKind.EXACTLY_ONCE)
}

acquire()
try {
return action()
Expand Down
2 changes: 2 additions & 0 deletions kotlinx-coroutines-core/common/test/sync/MutexTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package kotlinx.coroutines.sync

import kotlinx.coroutines.*
import kotlin.contracts.ExperimentalContracts
import kotlin.test.*

@ExperimentalContracts
class MutexTest : TestBase() {
@Test
fun testSimple() = runTest {
Expand Down
2 changes: 2 additions & 0 deletions kotlinx-coroutines-core/common/test/sync/SemaphoreTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kotlinx.coroutines.sync

import kotlinx.coroutines.*
import kotlin.contracts.ExperimentalContracts
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
Expand Down Expand Up @@ -59,6 +60,7 @@ class SemaphoreTest : TestBase() {
assertEquals(0, semaphore.availablePermits)
}

@ExperimentalContracts
@Test
fun withSemaphoreTest() = runTest {
val semaphore = Semaphore(1)
Expand Down
2 changes: 2 additions & 0 deletions kotlinx-coroutines-core/jvm/test/guide/example-sync-06.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package kotlinx.coroutines.guide.exampleSync06

import kotlinx.coroutines.*
import kotlinx.coroutines.sync.*
import kotlin.contracts.ExperimentalContracts
import kotlin.system.*

suspend fun massiveRun(action: suspend () -> Unit) {
Expand All @@ -27,6 +28,7 @@ suspend fun massiveRun(action: suspend () -> Unit) {
val mutex = Mutex()
var counter = 0

@ExperimentalContracts
fun main() = runBlocking {
withContext(Dispatchers.Default) {
massiveRun {
Expand Down