@@ -48,6 +48,15 @@ import kotlin.coroutines.*
4848 * this coroutine suspends.
4949 *
5050 * To use [ThreadLocal] variable within the coroutine use [ThreadLocal.asContextElement][asContextElement] function.
51+ *
52+ * ### Reentrancy and thread-safety
53+ *
54+ * Correct implementations of this interface must expect that calls to [restoreThreadContext]
55+ * may happen in parallel to the subsequent [updateThreadContext] and [restoreThreadContext] operations.
56+ * See [CopyableThreadContextElement] for advanced interleaving details.
57+ *
58+ * All implementations of [ThreadContextElement] should be thread-safe and guard their internal mutable state
59+ * within an element accordingly.
5160 */
5261public interface ThreadContextElement <S > : CoroutineContext .Element {
5362 /* *
@@ -133,6 +142,27 @@ public interface ThreadContextElement<S> : CoroutineContext.Element {
133142 *
134143 * A coroutine using this mechanism can safely call Java code that assumes the corresponding thread local element's
135144 * value is installed into the target thread local.
145+ *
146+ * ### Reentrancy and thread-safety
147+ *
148+ * Correct implementations of this interface must expect that calls to [restoreThreadContext]
149+ * may happen in parallel to the subsequent [updateThreadContext] and [restoreThreadContext] operations.
150+ *
151+ * Even though an element is copied for each child coroutine, an implementation should be able to handle the following
152+ * interleaving when a coroutine with the corresponding element is launched on a multithreaded dispatcher:
153+ *
154+ * ```
155+ * coroutine.updateThreadContext() // Thread #1
156+ * ... coroutine body ...
157+ * // suspension + immediate dispatch happen here
158+ * coroutine.updateThreadContext() // Thread #2, coroutine is already resumed
159+ * // ... coroutine body after suspension point on Thread #2 ...
160+ * coroutine.restoreThreadContext() // Thread #1, is invoked late because Thread #1 is slow
161+ * coroutine.restoreThreadContext() // Thread #2, may happen in parallel with the previous restore
162+ * ```
163+ *
164+ * All implementations of [CopyableThreadContextElement] should be thread-safe and guard their internal mutable state
165+ * within an element accordingly.
136166 */
137167@DelicateCoroutinesApi
138168@ExperimentalCoroutinesApi
0 commit comments