Skip to content

Commit 7523368

Browse files
Introduce CloseableCoroutineDispatcher abstract class (#2903)
* Introduce CloseableCoroutineDispatcher abstract class * Alias it to ExecutorCoroutineDispatcher on JVM as the most appropriate candidate * Add abstract classes to JVM and Native * This class will be implemented by new dispatchers for K/N new memory model Co-authored-by: dkhalanskyjb <[email protected]>
1 parent 12d70eb commit 7523368

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

kotlinx-coroutines-core/common/src/Annotations.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ public annotation class DelicateCoroutinesApi
3030
*/
3131
@MustBeDocumented
3232
@Retention(value = AnnotationRetention.BINARY)
33+
@Target(
34+
AnnotationTarget.CLASS,
35+
AnnotationTarget.ANNOTATION_CLASS,
36+
AnnotationTarget.PROPERTY,
37+
AnnotationTarget.FIELD,
38+
AnnotationTarget.LOCAL_VARIABLE,
39+
AnnotationTarget.VALUE_PARAMETER,
40+
AnnotationTarget.CONSTRUCTOR,
41+
AnnotationTarget.FUNCTION,
42+
AnnotationTarget.PROPERTY_GETTER,
43+
AnnotationTarget.PROPERTY_SETTER,
44+
AnnotationTarget.TYPEALIAS
45+
)
3346
@RequiresOptIn(level = RequiresOptIn.Level.WARNING)
3447
public annotation class ExperimentalCoroutinesApi
3548

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines
6+
7+
/**
8+
* [CoroutineDispatcher] that provides a method to close it,
9+
* causing the rejection of any new tasks and cleanup of all underlying resources
10+
* associated with the current dispatcher.
11+
* Examples of closeable dispatchers are dispatchers backed by `java.lang.Executor` and
12+
* by `kotlin.native.Worker`.
13+
*
14+
* **The `CloseableCoroutineDispatcher` class is not stable for inheritance in 3rd party libraries**, as new methods
15+
* might be added to this interface in the future, but is stable for use.
16+
*/
17+
@ExperimentalCoroutinesApi
18+
public expect abstract class CloseableCoroutineDispatcher() : CoroutineDispatcher {
19+
20+
/**
21+
* Initiate the closing sequence of the coroutine dispatcher.
22+
* After a successful call to [close], no new tasks will
23+
* be accepted to be [dispatched][dispatch], but the previously dispatched tasks will be run.
24+
*
25+
* Invocations of `close` are idempotent and thread-safe.
26+
*/
27+
public abstract fun close()
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines
6+
7+
public actual abstract class CloseableCoroutineDispatcher actual constructor() : CoroutineDispatcher() {
8+
public actual abstract fun close()
9+
}

kotlinx-coroutines-core/jvm/src/Executors.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public abstract class ExecutorCoroutineDispatcher: CoroutineDispatcher(), Closea
3737
public abstract override fun close()
3838
}
3939

40+
@ExperimentalCoroutinesApi
41+
public actual typealias CloseableCoroutineDispatcher = ExecutorCoroutineDispatcher
42+
4043
/**
4144
* Converts an instance of [ExecutorService] to an implementation of [ExecutorCoroutineDispatcher].
4245
*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines
6+
7+
public actual abstract class CloseableCoroutineDispatcher actual constructor() : CoroutineDispatcher() {
8+
public actual abstract fun close()
9+
}

0 commit comments

Comments
 (0)