File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed
kotlinx-coroutines-core/src
main/kotlin/kotlinx/coroutines/experimental
test/kotlin/kotlinx/coroutines/experimental Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -36,16 +36,25 @@ public class Mutex(locked: Boolean = false) {
3636
3737 private companion object {
3838 @JvmStatic
39- private val STATE : AtomicReferenceFieldUpdater <Mutex , Any ?> =
39+ val STATE : AtomicReferenceFieldUpdater <Mutex , Any ?> =
4040 AtomicReferenceFieldUpdater .newUpdater(Mutex ::class .java, Any ::class .java, " state" )
4141
4242 @JvmStatic
43- private val EmptyLocked = Empty (true )
43+ val EmptyLocked = Empty (true )
4444
4545 @JvmStatic
46- private val EmptyUnlocked = Empty (false )
46+ val EmptyUnlocked = Empty (false )
47+
48+ @JvmStatic
49+ fun isLocked (state : Any? ) = state !is Empty || state.locked
50+
4751 }
4852
53+ /* *
54+ * Returns `true` when this mutex is locked.
55+ */
56+ public val isLocked: Boolean get() = isLocked(state)
57+
4958 /* *
5059 * Tries to lock this mutex, returning `false` if this mutex is already locked.
5160 */
Original file line number Diff line number Diff line change @@ -45,12 +45,19 @@ class MutexTest : TestBase() {
4545 @Test
4646 fun tryLockTest () {
4747 val mutex = Mutex ()
48+ assertFalse(mutex.isLocked)
4849 assertTrue(mutex.tryLock())
50+ assertTrue(mutex.isLocked)
4951 assertFalse(mutex.tryLock())
52+ assertTrue(mutex.isLocked)
5053 mutex.unlock()
54+ assertFalse(mutex.isLocked)
5155 assertTrue(mutex.tryLock())
56+ assertTrue(mutex.isLocked)
5257 assertFalse(mutex.tryLock())
58+ assertTrue(mutex.isLocked)
5359 mutex.unlock()
60+ assertFalse(mutex.isLocked)
5461 }
5562
5663 @Test
You can’t perform that action at this time.
0 commit comments