Skip to content

Commit

Permalink
feat(coroutines): implement main thread and thread pool dispatchers
Browse files Browse the repository at this point in the history
  • Loading branch information
piiertho committed Oct 12, 2024
1 parent f252330 commit 8351024
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
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()
}

0 comments on commit 8351024

Please sign in to comment.