1 /* 2 * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.internal 6 7 import java.lang.reflect.* 8 import java.util.* 9 import java.util.concurrent.* 10 import kotlin.concurrent.withLock as withLockJvm 11 subscriberListnull12internal actual fun <E> subscriberList(): SubscribersList<E> = CopyOnWriteArrayList<E>() 13 14 @Suppress("ACTUAL_WITHOUT_EXPECT") 15 internal actual typealias ReentrantLock = java.util.concurrent.locks.ReentrantLock 16 17 internal actual inline fun <T> ReentrantLock.withLock(action: () -> T) = this.withLockJvm(action) 18 19 @Suppress("NOTHING_TO_INLINE") // So that R8 can completely remove ConcurrentKt class 20 internal actual inline fun <E> identitySet(expectedSize: Int): MutableSet<E> = 21 Collections.newSetFromMap(IdentityHashMap(expectedSize)) 22 23 private val REMOVE_FUTURE_ON_CANCEL: Method? = try { 24 ScheduledThreadPoolExecutor::class.java.getMethod("setRemoveOnCancelPolicy", Boolean::class.java) 25 } catch (e: Throwable) { 26 null 27 } 28 29 @Suppress("NAME_SHADOWING") removeFutureOnCancelnull30internal fun removeFutureOnCancel(executor: Executor): Boolean { 31 try { 32 val executor = executor as? ScheduledThreadPoolExecutor ?: return false 33 (REMOVE_FUTURE_ON_CANCEL ?: return false).invoke(executor, true) 34 return true 35 } catch (e: Throwable) { 36 return false // failed to setRemoveOnCancelPolicy, assume it does not removes future on cancel 37 } 38 } 39