Skip to content

Commit

Permalink
Fix example code in comment of CopyableThreadContextElement (Kotlin#3157
Browse files Browse the repository at this point in the history
)
  • Loading branch information
OliverO2 authored and pablobaxter committed Sep 14, 2022
1 parent cadd49b commit 5ae8611
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions kotlinx-coroutines-core/jvm/src/ThreadContextElement.kt
Expand Up @@ -97,26 +97,25 @@ public interface ThreadContextElement<S> : CoroutineContext.Element {
* is in a coroutine:
*
* ```
* class TraceContextElement(val traceData: TraceData?) : CopyableThreadContextElement<TraceData?> {
* companion object Key : CoroutineContext.Key<ThreadTraceContextElement>
* override val key: CoroutineContext.Key<ThreadTraceContextElement>
* get() = Key
* class TraceContextElement(private val traceData: TraceData?) : CopyableThreadContextElement<TraceData?> {
* companion object Key : CoroutineContext.Key<TraceContextElement>
* override val key: CoroutineContext.Key<TraceContextElement> = Key
*
* override fun updateThreadContext(context: CoroutineContext): TraceData? {
* val oldState = traceThreadLocal.get()
* traceThreadLocal.set(data)
* traceThreadLocal.set(traceData)
* return oldState
* }
*
* override fun restoreThreadContext(context: CoroutineContext, oldData: TraceData?) {
* override fun restoreThreadContext(context: CoroutineContext, oldState: TraceData?) {
* traceThreadLocal.set(oldState)
* }
*
* override fun copyForChildCoroutine(): CopyableThreadContextElement<MyData?> {
* override fun copyForChildCoroutine(): CopyableThreadContextElement<TraceData?> {
* // Copy from the ThreadLocal source of truth at child coroutine launch time. This makes
* // ThreadLocal writes between resumption of the parent coroutine and the launch of the
* // child coroutine visible to the child.
* return CopyForChildCoroutineElement(traceThreadLocal.get())
* return TraceContextElement(traceThreadLocal.get()?.copy())
* }
* }
* ```
Expand Down

0 comments on commit 5ae8611

Please sign in to comment.