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
Kotlin coroutines ReadWriteMutex #671
Conversation
…asier to transition in the future
Codecov Report
@@ Coverage Diff @@
## master #671 +/- ##
============================================
+ Coverage 35.48% 35.64% +0.16%
Complexity 485 485
============================================
Files 203 205 +2
Lines 4312 4337 +25
Branches 744 748 +4
============================================
+ Hits 1530 1546 +16
- Misses 2559 2568 +9
Partials 223 223
Continue to review full report at Codecov.
|
...coroutines/src/test/java/org/ccci/gto/android/common/kotlin/coroutines/ReadWriteMutexTest.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions, but only one of them is relatively important. This looks good to me, otherwise.
...lin-coroutines/src/main/java/org/ccci/gto/android/common/kotlin/coroutines/ReadWriteMutex.kt
Outdated
Show resolved
Hide resolved
...lin-coroutines/src/main/java/org/ccci/gto/android/common/kotlin/coroutines/ReadWriteMutex.kt
Show resolved
Hide resolved
...lin-coroutines/src/main/java/org/ccci/gto/android/common/kotlin/coroutines/ReadWriteMutex.kt
Outdated
Show resolved
Hide resolved
we need to make sure we use the same owner between locking and unlocking the write lock from within multiple read locks.
`stateMutex` is purely used internally, so we don't need to pass in an arbitrary owner
Thanks for the thorough review @mattdrees you definitely caught some things that I missed |
You're welcome! And your fixes look good to me. |
Unfortunately Kotlin Coroutines doesn't have a native ReadWriteMutex yet. This implements one with basic functionality as a stop-gap solution until they provide an official one.
a
ReadWriteMutex
should function similarly to aReadWriteReentrantLock
.This implementation doesn't address fairness for writers, so it is possible for readers to starve out waiting writers. but my current use-cases for this lock uses write access to perform background maintenance, and the background maintenance isn't critical for functionality, so it's alright if the writers are starved.
Relevant coroutines issue: Kotlin/kotlinx.coroutines#94