Skip to content

[native-mt] Fix the errornous isDispatchNeeded check in Dispatchers.Main.immediate on Darwin platforms. #2476

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

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/nativeDarwin/src/Dispatchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private class DarwinMainDispatcher(

init { freeze() }

override fun isDispatchNeeded(context: CoroutineContext): Boolean = !invokeImmediately || isMainThread()
override fun isDispatchNeeded(context: CoroutineContext): Boolean = !(invokeImmediately && isMainThread())

override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(dispatch_get_main_queue()) {
Expand Down
10 changes: 10 additions & 0 deletions kotlinx-coroutines-core/nativeDarwin/test/MainDispatcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@

package kotlinx.coroutines

import kotlin.coroutines.*
import kotlin.test.*

class MainDispatcherTest : TestBase() {
private val testThread = currentThread()

@Test
fun testDispatchNecessityCheckWithMainImmediateDispatcher() {
runTest {
val immediate = Dispatchers.Main.immediate
val needsDispatch = testThread != mainThread
assertEquals(needsDispatch, immediate.isDispatchNeeded(EmptyCoroutineContext))
}
}

@Test
fun testWithContext() {
if (testThread == mainThread) return // skip if already on the main thread
Expand Down