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) {
36
36
37
37
private companion object {
38
38
@JvmStatic
39
- private val STATE : AtomicReferenceFieldUpdater <Mutex , Any ?> =
39
+ val STATE : AtomicReferenceFieldUpdater <Mutex , Any ?> =
40
40
AtomicReferenceFieldUpdater .newUpdater(Mutex ::class .java, Any ::class .java, " state" )
41
41
42
42
@JvmStatic
43
- private val EmptyLocked = Empty (true )
43
+ val EmptyLocked = Empty (true )
44
44
45
45
@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
+
47
51
}
48
52
53
+ /* *
54
+ * Returns `true` when this mutex is locked.
55
+ */
56
+ public val isLocked: Boolean get() = isLocked(state)
57
+
49
58
/* *
50
59
* Tries to lock this mutex, returning `false` if this mutex is already locked.
51
60
*/
Original file line number Diff line number Diff line change @@ -45,12 +45,19 @@ class MutexTest : TestBase() {
45
45
@Test
46
46
fun tryLockTest () {
47
47
val mutex = Mutex ()
48
+ assertFalse(mutex.isLocked)
48
49
assertTrue(mutex.tryLock())
50
+ assertTrue(mutex.isLocked)
49
51
assertFalse(mutex.tryLock())
52
+ assertTrue(mutex.isLocked)
50
53
mutex.unlock()
54
+ assertFalse(mutex.isLocked)
51
55
assertTrue(mutex.tryLock())
56
+ assertTrue(mutex.isLocked)
52
57
assertFalse(mutex.tryLock())
58
+ assertTrue(mutex.isLocked)
53
59
mutex.unlock()
60
+ assertFalse(mutex.isLocked)
54
61
}
55
62
56
63
@Test
You can’t perform that action at this time.
0 commit comments