-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic threading strategy support
- Loading branch information
Showing
11 changed files
with
174 additions
and
55 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.juul.kable | ||
|
||
import android.os.Build | ||
import android.os.Handler | ||
import android.os.HandlerThread | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.DelicateCoroutinesApi | ||
import kotlinx.coroutines.ExecutorCoroutineDispatcher | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.android.asCoroutineDispatcher | ||
import kotlinx.coroutines.newSingleThreadContext | ||
|
||
public sealed class Threading { | ||
|
||
internal abstract val dispatcher: CoroutineDispatcher | ||
|
||
/** Used on Android O (API 26) and above. */ | ||
internal data class Handler( | ||
val thread: HandlerThread, | ||
val handler: android.os.Handler, | ||
override val dispatcher: CoroutineDispatcher, | ||
) : Threading() | ||
|
||
/** Used on Android versions **lower** than Android O (API 26). */ | ||
internal data class SingleThreadContext( | ||
val name: String, | ||
override val dispatcher: ExecutorCoroutineDispatcher, | ||
) : Threading() | ||
} | ||
|
||
public val Threading.name: String | ||
get() = when (this) { | ||
is Threading.Handler -> thread.name | ||
is Threading.SingleThreadContext -> name | ||
} | ||
|
||
public fun Threading.shutdown() { | ||
when (this) { | ||
is Threading.Handler -> thread.quit() | ||
is Threading.SingleThreadContext -> dispatcher.close() | ||
} | ||
} | ||
|
||
/** | ||
* Creates [Threading] that can be used for Bluetooth communication. The returned [Threading] is | ||
* returned in a started state and must be [shutdown] when no longer needed. | ||
*/ | ||
public fun Threading(name: String): Threading = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val thread = HandlerThread(name).apply { start() } | ||
val handler = Handler(thread.looper) | ||
val dispatcher = handler.asCoroutineDispatcher() | ||
Threading.Handler(thread, handler, dispatcher) | ||
} else { // Build.VERSION.SDK_INT < Build.VERSION_CODES.O | ||
@OptIn(DelicateCoroutinesApi::class, ExperimentalCoroutinesApi::class) | ||
Threading.SingleThreadContext(name, newSingleThreadContext(name)) | ||
} |
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,62 @@ | ||
package com.juul.kable | ||
|
||
import kotlinx.atomicfu.atomic | ||
import kotlinx.atomicfu.locks.reentrantLock | ||
import kotlinx.atomicfu.locks.withLock | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.CoroutineStart.LAZY | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlin.time.TimeMark | ||
import kotlin.time.TimeSource | ||
|
||
private val threadNumber = atomic(0) | ||
private fun generateThreadName() = "Kable#${threadNumber.incrementAndGet()}" | ||
|
||
public interface ThreadingStrategy { | ||
public fun acquire(): Threading | ||
public fun release(threading: Threading) | ||
} | ||
|
||
public object OnDemandThreadingStrategy : ThreadingStrategy { | ||
|
||
override fun acquire(): Threading = Threading(generateThreadName()) | ||
|
||
override fun release(threading: Threading) { | ||
threading.shutdown() | ||
} | ||
} | ||
|
||
public class PooledThreadingStrategy( | ||
scope: CoroutineScope = GlobalScope, | ||
private val evictAfter: Duration = 1.minutes, | ||
) : ThreadingStrategy { | ||
|
||
private val pool = mutableListOf<Pair<TimeMark, Threading>>() | ||
private val guard = reentrantLock() | ||
|
||
private val evictionJob = scope.launch(start = LAZY) { | ||
while (true) { | ||
guard.withLock { | ||
pool.removeAll { (timeMark) -> timeMark.hasPassedNow() } | ||
} | ||
delay(evictAfter / 2) | ||
} | ||
} | ||
|
||
internal fun start(): Boolean = evictionJob.start() | ||
|
||
override fun acquire(): Threading = guard.withLock { | ||
pool.removeFirstOrNull()?.second | ||
} ?: Threading(generateThreadName()) | ||
|
||
override fun release(threading: Threading) { | ||
guard.withLock { | ||
val evictAt = TimeSource.Monotonic.markNow() + evictAfter | ||
pool.add(evictAt to threading) | ||
} | ||
} | ||
} |
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