Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored executors (and fixed some problems), required by #19
- Loading branch information
Showing
8 changed files
with
104 additions
and
85 deletions.
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
android-bindings/src/main/kotlin/net/aquadc/properties/android/LooperExecutorFactory.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
properties/src/main/kotlin/net/aquadc/properties/executor/android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.aquadc.properties.executor | ||
|
||
import android.os.Handler | ||
import java.util.concurrent.Executor | ||
|
||
/** | ||
* [Executor] implementation based on Android's [Handler]. | ||
* Will cause [NoClassDefFoundError] if called out of Android. | ||
*/ | ||
class HandlerAsExecutor( | ||
private val handler: Handler | ||
) : Executor { | ||
|
||
override fun execute(command: Runnable) = | ||
check(handler.post(command)) | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
properties/src/main/kotlin/net/aquadc/properties/executor/fx.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.aquadc.properties.executor | ||
|
||
import javafx.application.Platform | ||
import java.util.concurrent.Executor | ||
|
||
/** | ||
* Wraps [Platform] to run JavaFX Application Thread. | ||
*/ | ||
object FxApplicationThreadExecutor : Executor { | ||
|
||
override fun execute(command: Runnable) = | ||
Platform.runLater(command) | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
properties/src/main/kotlin/net/aquadc/properties/executor/util.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package net.aquadc.properties.executor | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import javafx.application.Platform | ||
import java.util.concurrent.* | ||
|
||
|
||
internal object ScheduledDaemonHolder { | ||
internal val scheduledDaemon = | ||
ScheduledThreadPoolExecutor(1, ThreadFactory { Thread(it).also { it.isDaemon = true } }) | ||
} | ||
|
||
internal object PlatformExecutors { | ||
private val executors = ConcurrentHashMap<Thread, Executor>(4) | ||
private val executorFactories: Array<() -> Executor?> | ||
|
||
init { | ||
val facs = ArrayList<() -> Executor?>(2) | ||
|
||
try { | ||
Looper.myLooper() // ensure class available | ||
facs.add { | ||
Looper.myLooper()?.let { myLooper -> HandlerAsExecutor(Handler(myLooper)) } | ||
} | ||
} catch (ignored: NoClassDefFoundError) {} | ||
|
||
try { | ||
Platform.isFxApplicationThread() // ensure class available | ||
facs.add { | ||
if (Platform.isFxApplicationThread()) FxApplicationThreadExecutor else null | ||
} | ||
} catch (ignored: NoClassDefFoundError) {} | ||
|
||
executorFactories = facs.toTypedArray() | ||
} | ||
|
||
internal fun executorForCurrentThread(): Executor { | ||
val thread = Thread.currentThread() | ||
|
||
val ex = executors[thread] | ||
if (ex != null) return ex | ||
|
||
val newEx = createForCurrentThread() | ||
|
||
// if putIfAbsent returns non-null value, the executor was set concurrently, | ||
// use it and throw away our then | ||
return executors.putIfAbsent(thread, newEx) ?: newEx | ||
} | ||
|
||
private fun createForCurrentThread(): Executor { | ||
executorFactories.forEach { it()?.let { return it } } | ||
throw UnsupportedOperationException("Can't execute task on $this") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters