Skip to content
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

feat(coroutines): implement main thread and thread pool dispatchers #725

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package godot.coroutines

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

object GodotCoroutine : CoroutineScope {
override val coroutineContext = Dispatchers.Default + SupervisorJob()
override val coroutineContext = GodotDispatchers.ThreadPool + SupervisorJob()
}

fun godotCoroutine(context: CoroutineContext = Dispatchers.Default, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit) {
fun godotCoroutine(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
) {
GodotCoroutine.launch(context, start, block)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package godot.coroutines

import godot.WorkerThreadPool
import godot.core.Callable
import godot.core.asCallable
import kotlinx.coroutines.CoroutineDispatcher
import kotlin.coroutines.CoroutineContext

object GodotDispatchers {

val MainThread: CoroutineDispatcher = GodotMainThreadCoroutineDispatcher
val ThreadPool: CoroutineDispatcher = GodotThreadPoolCoroutineDispatcher

private object GodotMainThreadCoroutineDispatcher : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
Callable({ block.run() }.asCallable()).callDeferred()
}
}

private object GodotThreadPoolCoroutineDispatcher : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
WorkerThreadPool.addTask({ block.run() }.asCallable())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package godot.coroutines

import godot.core.Callable
import godot.core.asCallable
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlinx.coroutines.async


/**
Expand All @@ -15,14 +12,11 @@ import kotlin.coroutines.resume
*
* @param block the code block to execute at the end of the frame
*/
public suspend inline fun <R> awaitMainThread(
suspend inline fun <R> awaitMainThread(
crossinline block: () -> R
): R = suspendCancellableCoroutine { continuation ->
Callable(
{
if (continuation.isActive) {
continuation.resume(block())
}
}.asCallable()
).callDeferred()
): R {
val job = GodotCoroutine.async(GodotDispatchers.MainThread) {
block()
}
return job.await()
}
Loading