Skip to content

Commit c194877

Browse files
authored
Make EventLoopImplBase properly synchronized on Kotlin/Native (#3550)
Fixes #3547
1 parent 7205177 commit c194877

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

kotlinx-coroutines-core/common/src/EventLoop.common.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
410410
* into heap to avoid overflow and corruption of heap data structure.
411411
*/
412412
@JvmField var nanoTime: Long
413-
) : Runnable, Comparable<DelayedTask>, DisposableHandle, ThreadSafeHeapNode {
413+
) : Runnable, Comparable<DelayedTask>, DisposableHandle, ThreadSafeHeapNode, SynchronizedObject() {
414414
@Volatile
415415
private var _heap: Any? = null // null | ThreadSafeHeap | DISPOSED_TASK
416416

@@ -434,8 +434,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
434434

435435
fun timeToExecute(now: Long): Boolean = now - nanoTime >= 0L
436436

437-
@Synchronized
438-
fun scheduleTask(now: Long, delayed: DelayedTaskQueue, eventLoop: EventLoopImplBase): Int {
437+
fun scheduleTask(now: Long, delayed: DelayedTaskQueue, eventLoop: EventLoopImplBase): Int = synchronized<Int>(this) {
439438
if (_heap === DISPOSED_TASK) return SCHEDULE_DISPOSED // don't add -- was already disposed
440439
delayed.addLastIf(this) { firstTask ->
441440
if (eventLoop.isCompleted) return SCHEDULE_COMPLETED // non-local return from scheduleTask
@@ -477,8 +476,7 @@ internal abstract class EventLoopImplBase: EventLoopImplPlatform(), Delay {
477476
return SCHEDULE_OK
478477
}
479478

480-
@Synchronized
481-
final override fun dispose() {
479+
final override fun dispose(): Unit = synchronized(this) {
482480
val heap = _heap
483481
if (heap === DISPOSED_TASK) return // already disposed
484482
(heap as? DelayedTaskQueue)?.remove(this) // remove if it is in heap (first)

0 commit comments

Comments
 (0)