|
| 1 | +/* |
| 2 | + * Copyright 2016-2022 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 kotlinx.atomicfu.* |
| 8 | + |
| 9 | +/** |
| 10 | + * A thread-safe resource pool. |
| 11 | + * |
| 12 | + * [maxCapacity] is the maximum amount of elements. |
| 13 | + * [create] is the function that creates a new element. |
| 14 | + * |
| 15 | + * This is only used in the Native implementation, |
| 16 | + * but is part of the `concurrent` source set in order to test it on the JVM. |
| 17 | + */ |
| 18 | +internal class OnDemandAllocatingPool<T>( |
| 19 | + private val maxCapacity: Int, |
| 20 | + private val create: (Int) -> T |
| 21 | +) { |
| 22 | + /** |
| 23 | + * Number of existing elements + isClosed flag in the highest bit. |
| 24 | + * Once the flag is set, the value is guaranteed not to change anymore. |
| 25 | + */ |
| 26 | + private val controlState = atomic(0) |
| 27 | + private val elements = atomicArrayOfNulls<T>(maxCapacity) |
| 28 | + |
| 29 | + /** |
| 30 | + * Returns the number of elements that need to be cleaned up due to the pool being closed. |
| 31 | + */ |
| 32 | + @Suppress("NOTHING_TO_INLINE") |
| 33 | + private inline fun tryForbidNewElements(): Int { |
| 34 | + controlState.loop { |
| 35 | + if (it.isClosed()) return 0 // already closed |
| 36 | + if (controlState.compareAndSet(it, it or IS_CLOSED_MASK)) return it |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Suppress("NOTHING_TO_INLINE") |
| 41 | + private inline fun Int.isClosed(): Boolean = this and IS_CLOSED_MASK != 0 |
| 42 | + |
| 43 | + /** |
| 44 | + * Request that a new element is created. |
| 45 | + * |
| 46 | + * Returns `false` if the pool is closed. |
| 47 | + * |
| 48 | + * Note that it will still return `true` even if an element was not created due to reaching [maxCapacity]. |
| 49 | + * |
| 50 | + * Rethrows the exceptions thrown from [create]. In this case, this operation has no effect. |
| 51 | + */ |
| 52 | + fun allocate(): Boolean { |
| 53 | + controlState.loop { ctl -> |
| 54 | + if (ctl.isClosed()) return false |
| 55 | + if (ctl >= maxCapacity) return true |
| 56 | + if (controlState.compareAndSet(ctl, ctl + 1)) { |
| 57 | + elements[ctl].value = create(ctl) |
| 58 | + return true |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Close the pool. |
| 65 | + * |
| 66 | + * This will prevent any new elements from being created. |
| 67 | + * All the elements present in the pool will be returned. |
| 68 | + * |
| 69 | + * The function is thread-safe. |
| 70 | + * |
| 71 | + * [close] can be called multiple times, but only a single call will return a non-empty list. |
| 72 | + * This is due to the elements being cleaned out from the pool on the first invocation to avoid memory leaks, |
| 73 | + * and no new elements being created after. |
| 74 | + */ |
| 75 | + fun close(): List<T> { |
| 76 | + val elementsExisting = tryForbidNewElements() |
| 77 | + return (0 until elementsExisting).map { i -> |
| 78 | + // we wait for the element to be created, because we know that eventually it is going to be there |
| 79 | + loop { |
| 80 | + val element = elements[i].getAndSet(null) |
| 81 | + if (element != null) { |
| 82 | + return@map element |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // for tests |
| 89 | + internal fun stateRepresentation(): String { |
| 90 | + val ctl = controlState.value |
| 91 | + val elementsStr = (0 until (ctl and IS_CLOSED_MASK.inv())).map { elements[it].value }.toString() |
| 92 | + val closedStr = if (ctl.isClosed()) "[closed]" else "" |
| 93 | + return elementsStr + closedStr |
| 94 | + } |
| 95 | + |
| 96 | + override fun toString(): String = "OnDemandAllocatingPool(${stateRepresentation()})" |
| 97 | +} |
| 98 | + |
| 99 | +// KT-25023 |
| 100 | +private inline fun loop(block: () -> Unit): Nothing { |
| 101 | + while (true) { |
| 102 | + block() |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +private const val IS_CLOSED_MASK = 1 shl 31 |
0 commit comments