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.internal 6 7 import java.lang.reflect.* 8 import java.util.* 9 import java.util.concurrent.* 10 import kotlin.concurrent.withLock as withLockJvm 11 12 @Suppress("ACTUAL_WITHOUT_EXPECT") 13 internal actual typealias ReentrantLock = java.util.concurrent.locks.ReentrantLock 14 withLocknull15internal actual inline fun <T> ReentrantLock.withLock(action: () -> T) = this.withLockJvm(action) 16 17 @Suppress("NOTHING_TO_INLINE") // So that R8 can completely remove ConcurrentKt class 18 internal actual inline fun <E> identitySet(expectedSize: Int): MutableSet<E> = 19 Collections.newSetFromMap(IdentityHashMap(expectedSize)) 20 21 private val REMOVE_FUTURE_ON_CANCEL: Method? = try { 22 ScheduledThreadPoolExecutor::class.java.getMethod("setRemoveOnCancelPolicy", Boolean::class.java) 23 } catch (e: Throwable) { 24 null 25 } 26 27 @Suppress("NAME_SHADOWING") removeFutureOnCancelnull28internal fun removeFutureOnCancel(executor: Executor): Boolean { 29 try { 30 val executor = executor as? ScheduledThreadPoolExecutor ?: return false 31 (REMOVE_FUTURE_ON_CANCEL ?: return false).invoke(executor, true) 32 return true 33 } catch (e: Throwable) { 34 return false // failed to setRemoveOnCancelPolicy, assume it does not removes future on cancel 35 } 36 } 37