Skip to content

Commit 656112e

Browse files
authored
Replace synchronized with ReentrantLock in SynchronizedLazy (#3211)
1 parent d3fad45 commit 656112e

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Diff for: misk-action-scopes/src/main/kotlin/misk/scope/SynchronizedLazy.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package misk.scope
22

3+
import java.util.concurrent.locks.ReentrantLock
4+
import kotlin.concurrent.withLock
5+
36
private object UNINITIALIZED_VALUE
47

58
internal class SynchronizedLazy(
@@ -8,13 +11,15 @@ internal class SynchronizedLazy(
811
@Volatile
912
private var _value: Any? = UNINITIALIZED_VALUE
1013

14+
private val lock = ReentrantLock()
15+
1116
override val value: Any?
1217
get() {
1318
if (_value !== UNINITIALIZED_VALUE) {
1419
return _value
1520
}
1621

17-
return synchronized(this) {
22+
return lock.withLock {
1823
val existingValue = _value
1924
if (existingValue != UNINITIALIZED_VALUE) {
2025
existingValue

0 commit comments

Comments
 (0)