Kotlin coroutines library providing comprehensive asynchronous programming support with structured concurrency for iOS x64 platforms
npx @tessl/cli install tessl/maven-org-jetbrains-kotlinx--kotlinx-coroutines-core-iosx64@1.10.0kotlinx-coroutines-core is a comprehensive Kotlin multiplatform library that provides structured concurrency support for asynchronous programming. It offers coroutine builders, reactive flows, communication channels, synchronization primitives, and platform-specific integrations while maintaining structured concurrency principles and automatic cancellation support.
build.gradle.kts dependencies:
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-iosx64:1.10.2")import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.sync.*For specific imports:
import kotlinx.coroutines.launch
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.sync.Muteximport kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
class CoroutineExample {
suspend fun basicUsage() {
// Create a coroutine scope
val scope = CoroutineScope(Dispatchers.Default)
// Launch fire-and-forget coroutines
val job = scope.launch {
delay(100)
println("Hello from coroutine!")
}
// Launch concurrent computation
val deferred = scope.async {
delay(50)
"Result from async"
}
// Work with flows
val flow = flowOf(1, 2, 3, 4, 5)
.map { it * 2 }
.filter { it > 5 }
flow.collect { value ->
println("Flow value: $value")
}
// Wait for completion
val result = deferred.await()
job.join()
}
}kotlinx-coroutines-core is built around several key architectural components:
launch, async, runBlocking for creating different types of coroutinesCore lifecycle management for coroutines including Job interface, scoping, and structured concurrency support.
interface Job : CoroutineContext.Element {
val isActive: Boolean
val isCompleted: Boolean
val isCancelled: Boolean
val children: Sequence<Job>
fun start(): Boolean
fun cancel(): Unit
suspend fun join(): Unit
}
interface CoroutineScope {
val coroutineContext: CoroutineContext
}
fun CoroutineScope(context: CoroutineContext): CoroutineScope
fun MainScope(): CoroutineScopeFunctions for creating and launching coroutines with different execution patterns and result handling.
fun CoroutineScope.launch(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> Unit
): Job
fun <T> CoroutineScope.async(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
block: suspend CoroutineScope.() -> T
): Deferred<T>
suspend fun <T> withContext(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
): TCold reactive streams with comprehensive transformation operators, hot flows for state and event broadcasting.
interface Flow<out T> {
suspend fun collect(collector: FlowCollector<T>)
}
interface FlowCollector<in T> {
suspend fun emit(value: T)
}
fun <T> flow(block: suspend FlowScope<T>.() -> Unit): Flow<T>
fun <T> flowOf(vararg elements: T): Flow<T>
interface SharedFlow<out T> : Flow<T> {
val replayCache: List<T>
}
interface StateFlow<out T> : SharedFlow<T> {
val value: T
}Communication primitives between coroutines with various buffering strategies and channel types.
interface SendChannel<in E> {
val isClosedForSend: Boolean
suspend fun send(element: E)
fun trySend(element: E): ChannelResult<Unit>
fun close(cause: Throwable? = null): Boolean
}
interface ReceiveChannel<out E> {
val isClosedForReceive: Boolean
val isEmpty: Boolean
suspend fun receive(): E
fun tryReceive(): ChannelResult<E>
}
interface Channel<E> : SendChannel<E>, ReceiveChannel<E>
fun <E> Channel(
capacity: Int = RENDEZVOUS,
onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND,
onUndeliveredElement: ((E) -> Unit)? = null
): Channel<E>Non-blocking synchronization primitives including mutex and semaphore for coroutine coordination.
interface Mutex {
val isLocked: Boolean
suspend fun lock(owner: Any? = null)
fun unlock(owner: Any? = null)
fun tryLock(owner: Any? = null): Boolean
}
suspend fun <T> Mutex.withLock(
owner: Any? = null,
action: suspend () -> T
): T
interface Semaphore {
val availablePermits: Int
suspend fun acquire()
fun release()
fun tryAcquire(): Boolean
}Execution context control for coroutines including standard dispatchers and custom dispatcher creation.
abstract class CoroutineDispatcher : ContinuationInterceptor {
abstract fun dispatch(context: CoroutineContext, block: Runnable)
open fun isDispatchNeeded(context: CoroutineContext): Boolean
fun limitedParallelism(parallelism: Int): CoroutineDispatcher
}
object Dispatchers {
val Default: CoroutineDispatcher
val Main: MainCoroutineDispatcher
val Unconfined: CoroutineDispatcher
val IO: CoroutineDispatcher
}Multi-way suspending choice allowing selection among multiple suspend operations.
suspend fun <R> select(builder: SelectBuilder<R>.() -> Unit): R
interface SelectBuilder<in R> {
fun <T> SelectClause1<T>.invoke(block: suspend (T) -> R)
fun SelectClause0.invoke(block: suspend () -> R)
fun onTimeout(timeMillis: Long, block: suspend () -> R)
}Cancellation propagation, exception handling, and timeout management for robust coroutine applications.
open class CancellationException : IllegalStateException
suspend fun <T> withTimeout(timeoutMillis: Long, block: suspend CoroutineScope.() -> T): T
suspend fun <T> withTimeoutOrNull(timeoutMillis: Long, block: suspend CoroutineScope.() -> T): T?
class TimeoutCancellationException : CancellationException
interface CoroutineExceptionHandler : CoroutineContext.Element {
fun handleException(context: CoroutineContext, exception: Throwable)
}enum class CoroutineStart {
DEFAULT,
LAZY,
ATOMIC,
UNDISPATCHED
}
interface Deferred<out T> : Job {
suspend fun await(): T
fun getCompleted(): T
fun getCompletionExceptionOrNull(): Throwable?
}
interface CompletableJob : Job {
fun complete(): Boolean
fun completeExceptionally(exception: Throwable): Boolean
}
interface CompletableDeferred<T> : Deferred<T>, CompletableJob
data class CoroutineName(val name: String) : CoroutineContext.Elementenum class BufferOverflow {
SUSPEND,
DROP_OLDEST,
DROP_LATEST
}
interface SharingStarted {
companion object {
val Eagerly: SharingStarted
val Lazily: SharingStarted
fun WhileSubscribed(
stopTimeoutMillis: Long = 0,
replayExpirationMillis: Long = Long.MAX_VALUE
): SharingStarted
}
}