Skip to content

Instantly share code, notes, and snippets.

@bobvawter
Created August 30, 2017 16:19
Show Gist options
  • Save bobvawter/4ff642d5996dfccb228425909f303306 to your computer and use it in GitHub Desktop.
Save bobvawter/4ff642d5996dfccb228425909f303306 to your computer and use it in GitHub Desktop.
A very basic Kotlin-Coroutines ReadWriteMutex example
/**
* Copyright 2017 ModelBox Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The read or write mode of the [ReadWriteMutex].
*/
enum class MutexMode {
READ,
WRITE
}
/**
* The lock state of the [ReadWriteMutex].
*/
enum class MutexState {
/**
* The mutex has been locked.
*/
LOCKED,
/**
* The mutex has been unlocked.
*/
UNLOCKED
}
data class MutexInfo(val mode: MutexMode, val state: MutexState)
/**
* A variation on [Mutex] that supports read and write operations.
*
* A "real" version may be in the works:
* https://github.com/Kotlin/kotlinx.coroutines/issues/94
*/
interface ReadWriteMutex {
/**
* Execute a read operation. Readers do not block one another, but will be blocked
* by a writer.
*/
suspend fun <T> withReadLock(block: suspend () -> T): T
/**
* Execute a write operation. Writers block all other activity in the mutex until
* they have finished.
*/
suspend fun <T> withWriteLock(fn: suspend () -> T): T
}
/**
* Construct a new [ReadWriteMutex]. The [block] can be used to provide additional
* configuration options to the mutex.
*/
fun ReadWriteMutex(block: ReadWriteMutexBuilder.() -> Unit = {}): ReadWriteMutex {
return ReadWriteMutexBuilder().apply(block).build()
}
class ReadWriteMutexBuilder internal constructor() {
private var onStateChange: (suspend (MutexInfo) -> Unit)? = null
/**
* Provide a function to invoke as the mutex transitions into new states. This can be
* used to coordinate the internal operation with externally-visible effects, such as
* acquiring filesystem locks.
*/
fun onStateChange(block: suspend (MutexInfo) -> Unit) {
onStateChange = block
}
internal fun build(): ReadWriteMutex = ReadWriteMutexImpl(onStateChange)
}
/**
* Implementation of [ReadWriteMutex].
*/
private class ReadWriteMutexImpl constructor(
private val onStateChange: (suspend (MutexInfo) -> Unit)?) : ReadWriteMutex {
/**
* A mutex to guard the creation of new readers.
*/
private val allowNewReads = Mutex()
/**
* A mutex to guard the creation of new writers.
*/
private val allowNewWrites = Mutex()
private var readers = 0
/**
* Controls access to [readers].
*/
private val stateLock = Mutex()
override suspend fun <T> withReadLock(block: suspend () -> T): T {
return try {
// Ensure new readers are allowed
allowNewReads.withLock {
stateLock.withLock {
// Increment the reader count.
if (readers++ == 0) {
// If we're the first reader, ensure that writes are locked out
allowNewWrites.lock(this)
// Invoke user callback
onStateChange?.invoke(MutexInfo(READ, LOCKED))
}
}
}
// Execute the user function.
block()
} finally {
// We don't want to use allowNewReads here, because a writer can acquire that lock
// while waiting fol allowNewWrites to be unlocked. Instead, we'll just treat clean-up
// like we're draining all outstanding readers to admit the writer.
stateLock.withLock {
// Decrement the reader count, and unlock if we were the last reader
if (--readers == 0) {
try {
// Invoke user callback in opposite order from above
onStateChange?.invoke(MutexInfo(READ, UNLOCKED))
} finally {
// If a writer is pending, this will unlock it
allowNewWrites.unlock(this)
}
}
}
}
}
override suspend fun <T> withWriteLock(fn: suspend () -> T): T {
// Prevent readers from starting any new action
return allowNewReads.withLock {
// Wait for all outstanding readers to drain.
allowNewWrites.withLock {
try {
onStateChange?.invoke(MutexInfo(WRITE, LOCKED))
fn()
} finally {
onStateChange?.invoke(MutexInfo(WRITE, UNLOCKED))
}
}
}
}
}
/**
* Copyright 2017 ModelBox Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import io.modelbox.api.MutexMode.READ
import io.modelbox.api.MutexMode.WRITE
import io.modelbox.api.MutexState.LOCKED
import io.modelbox.api.MutexState.UNLOCKED
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.runBlocking
import kotlinx.coroutines.experimental.selects.select
import kotlinx.coroutines.experimental.sync.Mutex
import kotlinx.coroutines.experimental.withTimeout
import org.junit.Test
import java.util.concurrent.TimeUnit.SECONDS
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class ReadWriteMutexTest {
private val mutex = ReadWriteMutex {
onStateChange { lastInfo = it }
}
private lateinit var lastInfo: MutexInfo
// Released by test block below
private val waitOne = Mutex(true)
private val waitTwo = Mutex(true)
// Released when the async block starts
private val startedOne = Mutex(true)
private val startedTwo = Mutex(true)
@Test
fun testExclusiveWrites() {
runBlocking {
withTimeout(1, SECONDS) {
val one = async(CommonPool) {
mutex.withWriteLock {
startedOne.unlock()
waitOne.lock()
}
}
val two = async(CommonPool) {
mutex.withWriteLock {
startedTwo.unlock()
waitTwo.lock()
}
}
// One of the writers will win the write lock and have started, but gotten blocked
val winner = select<Int> {
startedOne.onLock { 1 }
startedTwo.onLock { 2 }
}
assertEquals(MutexInfo(WRITE, LOCKED), lastInfo)
// Unlock the waiters above, and wait for the loser to complete
when (winner) {
1 -> {
assertTrue(startedTwo.isLocked)
waitOne.unlock()
waitTwo.unlock()
two.await()
}
2 -> {
assertTrue(startedOne.isLocked)
waitOne.unlock()
waitTwo.unlock()
one.await()
}
}
}
}
assertEquals(MutexInfo(WRITE, UNLOCKED), lastInfo)
}
@Test
fun testNonBlockingReads() {
val one = async(CommonPool) {
mutex.withReadLock {
startedOne.unlock()
waitOne.lock()
}
}
val two = async(CommonPool) {
mutex.withReadLock {
startedTwo.unlock()
waitTwo.lock()
}
}
runBlocking {
withTimeout(1, SECONDS) {
// Ensure that both tasks have started
startedOne.lock()
startedTwo.lock()
assertEquals(MutexInfo(READ, LOCKED), lastInfo)
// Unlock the first reader and wait for it to finish
waitOne.unlock()
one.await()
// Unlock the second reader and wait for it to finish
waitTwo.unlock()
two.await()
}
}
assertEquals(MutexInfo(READ, UNLOCKED), lastInfo)
}
@Test
fun testReadBlocksWrites() {
runBlocking {
withTimeout(1, SECONDS) {
// Start a reader
val one = async(CommonPool) {
mutex.withReadLock {
startedOne.unlock()
waitOne.lock()
}
}
// Ensure that the writer has started
startedOne.lock()
val beforeWriteLock = Mutex(true)
// Start a writer
val two = async(CommonPool) {
beforeWriteLock.unlock()
mutex.withWriteLock {
startedTwo.unlock()
waitTwo.lock()
}
}
// Ensure that the writer has started running, but hasn't entered the write block
beforeWriteLock.lock()
assertEquals(MutexInfo(READ, LOCKED), lastInfo)
assertFalse(startedTwo.tryLock())
// Unblock the reader
waitOne.unlock()
one.await()
// Ensure second has entered write block
startedTwo.lock()
waitTwo.unlock()
two.await()
}
}
}
@Test
fun testWritesBlockReads() {
runBlocking {
withTimeout(1, SECONDS) {
val one = async(CommonPool) {
mutex.withWriteLock {
startedOne.unlock()
waitOne.lock()
}
}
// Ensure that the writer has started
startedOne.lock()
val beforeReadLock = Mutex(true)
// Start a reader
val two = async(CommonPool) {
beforeReadLock.unlock()
mutex.withReadLock {
startedTwo.unlock()
waitTwo.lock()
}
}
// Ensure that the reader has started running, but hasn't entered the read block
beforeReadLock.lock()
assertEquals(MutexInfo(WRITE, LOCKED), lastInfo)
assertFalse(startedTwo.tryLock())
// Unblock the writer
waitOne.unlock()
one.await()
// Ensure second has entered read block
startedTwo.lock()
waitTwo.unlock()
two.await()
}
}
}
}
@GavinRay97
Copy link

GavinRay97 commented Sep 10, 2022

Thanks! Can't believe the standard library still doesn't have a read-write lock primitive, or any docs/pointers on what to use instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment