|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 Square, Inc. |
| 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 | +package okio |
| 17 | + |
| 18 | +import okio.SegmentPool.LOCK |
| 19 | +import okio.SegmentPool.MAX_SIZE |
| 20 | +import okio.SegmentPool.recycle |
| 21 | +import okio.SegmentPool.take |
| 22 | +import java.util.concurrent.atomic.AtomicLong |
| 23 | +import java.util.concurrent.atomic.AtomicReference |
| 24 | + |
| 25 | +/** |
| 26 | + * This class pools segments in a lock-free singly-linked stack. Though this code is lock-free it |
| 27 | + * does use a sentinel [LOCK] value to defend against races. |
| 28 | + * |
| 29 | + * When popping, a caller swaps the stack's next pointer with the [LOCK] sentinel. If the stack was |
| 30 | + * not already locked, the caller replaces the head node with its successor. |
| 31 | + * |
| 32 | + * When pushing, a caller swaps the head with a new node whose successor is the replaced head. |
| 33 | + * |
| 34 | + * If operations conflict, segments are not pushed into the stack. A [recycle] call that loses a |
| 35 | + * race will not add to the pool, and a [take] call that loses a race will not take from the pool. |
| 36 | + * Under significant contention this pool will have fewer hits and the VM will do more GC and zero |
| 37 | + * filling of arrays. |
| 38 | + * |
| 39 | + * Note that the [MAX_SIZE] may be exceeded if multiple calls to [recycle] race. Exceeding the |
| 40 | + * target pool size by a few segments doesn't harm performance, and imperfect enforcement is less |
| 41 | + * code. |
| 42 | + */ |
| 43 | +internal actual object SegmentPool { |
| 44 | + /** The maximum number of bytes to pool. */ |
| 45 | + // TODO: Is 64 KiB a good maximum size? Do we ever have that many idle segments? |
| 46 | + actual val MAX_SIZE = 64 * 1024L // 64 KiB. |
| 47 | + |
| 48 | + /** A sentinel segment to indicate that the linked list is currently being modified. */ |
| 49 | + private val LOCK = Segment(ByteArray(0), pos = 0, limit = 0, shared = false, owner = false) |
| 50 | + |
| 51 | + /** Singly-linked list of segments. */ |
| 52 | + private var firstRef = AtomicReference<Segment?>() |
| 53 | + |
| 54 | + /** Total bytes in this pool. */ |
| 55 | + private var atomicByteCount = AtomicLong() |
| 56 | + |
| 57 | + actual val byteCount: Long |
| 58 | + get() = atomicByteCount.get() |
| 59 | + |
| 60 | + @JvmStatic |
| 61 | + actual fun take(): Segment { |
| 62 | + val first = firstRef.getAndSet(LOCK) |
| 63 | + when { |
| 64 | + first === LOCK -> { |
| 65 | + // We didn't acquire the lock. Don't take a pooled segment. |
| 66 | + return Segment() |
| 67 | + } |
| 68 | + first == null -> { |
| 69 | + // We acquired the lock but the pool was empty. Unlock and return a new segment. |
| 70 | + firstRef.set(null) |
| 71 | + return Segment() |
| 72 | + } |
| 73 | + else -> { |
| 74 | + // We acquired the lock and the pool was not empty. Pop the first element and return it. |
| 75 | + firstRef.set(first.next) |
| 76 | + first.next = null |
| 77 | + atomicByteCount.addAndGet(-Segment.SIZE.toLong()) |
| 78 | + return first |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @JvmStatic |
| 84 | + actual fun recycle(segment: Segment) { |
| 85 | + require(segment.next == null && segment.prev == null) |
| 86 | + if (segment.shared) return // This segment cannot be recycled. |
| 87 | + if (atomicByteCount.get() >= MAX_SIZE) return // Pool is full. |
| 88 | + |
| 89 | + val first = firstRef.get() |
| 90 | + if (first === LOCK) return // A take() is currently in progress. |
| 91 | + |
| 92 | + segment.next = first |
| 93 | + segment.limit = 0 |
| 94 | + segment.pos = 0 |
| 95 | + |
| 96 | + if (firstRef.compareAndSet(first, segment)) { |
| 97 | + // We successfully recycled this segment. Adjust the pool size. |
| 98 | + atomicByteCount.addAndGet(Segment.SIZE.toLong()) |
| 99 | + } else { |
| 100 | + // We raced another operation. Don't recycle this segment. |
| 101 | + segment.next = null |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments