MCPcopy Create free account
hub / github.com/OpenEndedGroup/Field2 / inMainThread

Method inMainThread

src/main/java/field/app/ThreadSync2.kt:364–394  ·  view source on GitHub ↗

kotlin interface

Source from the content-addressed store, hash-verified

362 }
363 }
364
365
366 var debug = false;
367
368 // kotlin interface
369 @JvmStatic
370 fun <T> inMainThread(c: () -> T): T {
371 if (Thread.currentThread() === RunLoop.main.mainThread) {
372 if (debug) println("||||||||||||||||||||||||||||||| already main thread")
373 return c()
374 } else {
375
376 if (debug) println(" current thread :" + Thread.currentThread() + " " + RunLoop.main.mainThread)
377
378 val f = CompletableFuture<T>()
379 if (debug) println("||||||||||||||||||||||||||||||| call once ")
380 RunLoop.main.once {
381 try {
382 f.complete(c())
383 } catch (t: Throwable) {
384 f.completeExceptionally(t)
385 }
386 }
387
388 // should be rejoin?
389 if (debug) println("||||||||||||||||||||||||||||||| begin wait ")
390 while (!f.isDone) {
391 if (debug) println("||||||||||||||||||||||||||||||| yield to main loop")
392 yield()
393 if (debug) println("||||||||||||||||||||||||||||||| back from main loop")
394 }
395 if (debug) println("||||||||||||||||||||||||||||||| f is :" + f)
396
397 return f.get()

Callers

nothing calls this directly

Calls 5

yieldFunction · 0.85
onceMethod · 0.80
getMethod · 0.65
cFunction · 0.50
completeMethod · 0.45

Tested by

no test coverage detected