blob: dc2fd9ae31112bf6713083c1b9d22ee96b774b17 [file] [log] [blame]
Roman Elizarova7db8ec2017-12-21 22:45:12 +03001/*
2 * Copyright 2016-2017 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Roman Elizarove1c0b652017-12-01 14:02:57 +030017package kotlinx.coroutines.experimental
18
Roman Elizarovaa461cf2018-04-11 13:20:29 +030019import kotlinx.coroutines.experimental.internal.*
20
Roman Elizarove1c0b652017-12-01 14:02:57 +030021/**
22 * Handler for [Job.invokeOnCompletion].
23 *
24 * Installed handler should not throw any exceptions. If it does, they will get caught,
25 * wrapped into [CompletionHandlerException], and rethrown, potentially causing crash of unrelated code.
26 *
27 * **Note**: This type is a part of internal machinery that supports parent-child hierarchies
28 * and allows for implementation of suspending functions that wait on the Job's state.
29 * This type should not be used in general application code.
30 * Implementations of `CompletionHandler` must be fast and _lock-free_.
31 */
32public typealias CompletionHandler = (cause: Throwable?) -> Unit
Roman Elizarovaa461cf2018-04-11 13:20:29 +030033
34// We want class that extends LockFreeLinkedListNode & CompletionHandler but we cannot do it on Kotlin/JS,
35// so this expect class provides us with the corresponding abstraction in a platform-agnostic way.
36internal expect abstract class CompletionHandlerNode() : LockFreeLinkedListNode {
37 val asHandler: CompletionHandler
38 abstract fun invoke(cause: Throwable?)
39}
40
41// :KLUDGE: We have to invoke a handler in platform-specific way via `invokeIt` extension,
42// because we play type tricks on Kotlin/JS and handler is not necessarily a function there
43internal expect fun CompletionHandler.invokeIt(cause: Throwable?)