Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.channels |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 6 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.internal.* |
| 8 | import kotlinx.coroutines.selects.* |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Channel with array buffer of a fixed [capacity]. |
| 12 | * Sender suspends only when buffer is fully and receiver suspends only when buffer is empty. |
| 13 | * |
Roman Elizarov | f2a710a | 2017-07-21 18:33:59 +0300 | [diff] [blame] | 14 | * This channel is created by `Channel(capacity)` factory function invocation. |
| 15 | * |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 16 | * This implementation uses lock to protect the buffer, which is held only during very short buffer-update operations. |
| 17 | * The lists of suspended senders or receivers are lock-free. |
Vsevolod Tolstopyatov | 1f7b2d8 | 2018-10-09 15:57:51 +0300 | [diff] [blame^] | 18 | **/ |
| 19 | internal open class ArrayChannel<E>( |
Roman Elizarov | f138bbc | 2017-02-09 19:13:08 +0300 | [diff] [blame] | 20 | /** |
| 21 | * Buffer capacity. |
| 22 | */ |
| 23 | val capacity: Int |
| 24 | ) : AbstractChannel<E>() { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 25 | init { |
Roman Elizarov | 8385ec9 | 2017-05-11 18:32:52 +0300 | [diff] [blame] | 26 | require(capacity >= 1) { "ArrayChannel capacity must be at least 1, but $capacity was specified" } |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | private val lock = ReentrantLock() |
| 30 | private val buffer: Array<Any?> = arrayOfNulls<Any?>(capacity) |
| 31 | private var head: Int = 0 |
| 32 | @Volatile |
| 33 | private var size: Int = 0 |
| 34 | |
Roman Elizarov | 2ad0e94 | 2017-02-28 19:14:08 +0300 | [diff] [blame] | 35 | protected final override val isBufferAlwaysEmpty: Boolean get() = false |
| 36 | protected final override val isBufferEmpty: Boolean get() = size == 0 |
| 37 | protected final override val isBufferAlwaysFull: Boolean get() = false |
| 38 | protected final override val isBufferFull: Boolean get() = size == capacity |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 39 | |
Roman Elizarov | f6fed2a | 2017-02-03 19:12:09 +0300 | [diff] [blame] | 40 | // result is `OFFER_SUCCESS | OFFER_FAILED | Closed` |
Roman Elizarov | 2ad0e94 | 2017-02-28 19:14:08 +0300 | [diff] [blame] | 41 | protected override fun offerInternal(element: E): Any { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 42 | var receive: ReceiveOrClosed<E>? = null |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 43 | var token: Any? = null |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 44 | lock.withLock { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 45 | val size = this.size |
Roman Elizarov | f6fed2a | 2017-02-03 19:12:09 +0300 | [diff] [blame] | 46 | closedForSend?.let { return it } |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 47 | if (size < capacity) { |
| 48 | // tentatively put element to buffer |
| 49 | this.size = size + 1 // update size before checking queue (!!!) |
| 50 | // check for receivers that were waiting on empty queue |
| 51 | if (size == 0) { |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 52 | loop@ while (true) { |
| 53 | receive = takeFirstReceiveOrPeekClosed() ?: break@loop // break when no receivers queued |
| 54 | if (receive is Closed) { |
| 55 | this.size = size // restore size |
| 56 | return receive!! |
| 57 | } |
| 58 | token = receive!!.tryResumeReceive(element, idempotent = null) |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 59 | if (token != null) { |
| 60 | this.size = size // restore size |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 61 | return@withLock |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
| 65 | buffer[(head + size) % capacity] = element // actually queue element |
| 66 | return OFFER_SUCCESS |
| 67 | } |
| 68 | // size == capacity: full |
| 69 | return OFFER_FAILED |
| 70 | } |
| 71 | // breaks here if offer meets receiver |
| 72 | receive!!.completeResumeReceive(token!!) |
| 73 | return receive!!.offerResult |
| 74 | } |
| 75 | |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 76 | // result is `ALREADY_SELECTED | OFFER_SUCCESS | OFFER_FAILED | Closed` |
Roman Elizarov | 2ad0e94 | 2017-02-28 19:14:08 +0300 | [diff] [blame] | 77 | protected override fun offerSelectInternal(element: E, select: SelectInstance<*>): Any { |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 78 | var receive: ReceiveOrClosed<E>? = null |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 79 | var token: Any? = null |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 80 | lock.withLock { |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 81 | val size = this.size |
| 82 | closedForSend?.let { return it } |
| 83 | if (size < capacity) { |
| 84 | // tentatively put element to buffer |
| 85 | this.size = size + 1 // update size before checking queue (!!!) |
| 86 | // check for receivers that were waiting on empty queue |
| 87 | if (size == 0) { |
| 88 | loop@ while (true) { |
| 89 | val offerOp = describeTryOffer(element) |
| 90 | val failure = select.performAtomicTrySelect(offerOp) |
| 91 | when { |
| 92 | failure == null -> { // offered successfully |
| 93 | this.size = size // restore size |
| 94 | receive = offerOp.result |
| 95 | token = offerOp.resumeToken |
| 96 | check(token != null) |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 97 | return@withLock |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 98 | } |
| 99 | failure === OFFER_FAILED -> break@loop // cannot offer -> Ok to queue to buffer |
| 100 | failure === ALREADY_SELECTED || failure is Closed<*> -> { |
| 101 | this.size = size // restore size |
| 102 | return failure |
| 103 | } |
| 104 | else -> error("performAtomicTrySelect(describeTryOffer) returned $failure") |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | // let's try to select sending this element to buffer |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 109 | if (!select.trySelect(null)) { // :todo: move trySelect completion outside of lock |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 110 | this.size = size // restore size |
| 111 | return ALREADY_SELECTED |
| 112 | } |
| 113 | buffer[(head + size) % capacity] = element // actually queue element |
| 114 | return OFFER_SUCCESS |
| 115 | } |
| 116 | // size == capacity: full |
| 117 | return OFFER_FAILED |
| 118 | } |
| 119 | // breaks here if offer meets receiver |
| 120 | receive!!.completeResumeReceive(token!!) |
| 121 | return receive!!.offerResult |
| 122 | } |
| 123 | |
| 124 | // result is `E | POLL_FAILED | Closed` |
Roman Elizarov | 2ad0e94 | 2017-02-28 19:14:08 +0300 | [diff] [blame] | 125 | protected override fun pollInternal(): Any? { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 126 | var send: Send? = null |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 127 | var token: Any? = null |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 128 | var result: Any? = null |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 129 | lock.withLock { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 130 | val size = this.size |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 131 | if (size == 0) return closedForSend ?: POLL_FAILED // when nothing can be read from buffer |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 132 | // size > 0: not empty -- retrieve element |
| 133 | result = buffer[head] |
| 134 | buffer[head] = null |
| 135 | this.size = size - 1 // update size before checking queue (!!!) |
| 136 | // check for senders that were waiting on full queue |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 137 | var replacement: Any? = POLL_FAILED |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 138 | if (size == capacity) { |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 139 | loop@ while (true) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 140 | send = takeFirstSendOrPeekClosed() ?: break |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 141 | token = send!!.tryResumeSend(idempotent = null) |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 142 | if (token != null) { |
| 143 | replacement = send!!.pollResult |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 144 | break@loop |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 148 | if (replacement !== POLL_FAILED && replacement !is Closed<*>) { |
Roman Elizarov | 7b2d8b0 | 2017-02-02 20:09:14 +0300 | [diff] [blame] | 149 | this.size = size // restore size |
| 150 | buffer[(head + size) % capacity] = replacement |
| 151 | } |
| 152 | head = (head + 1) % capacity |
| 153 | } |
| 154 | // complete send the we're taken replacement from |
| 155 | if (token != null) |
| 156 | send!!.completeResumeSend(token!!) |
| 157 | return result |
| 158 | } |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 159 | |
| 160 | // result is `ALREADY_SELECTED | E | POLL_FAILED | Closed` |
Roman Elizarov | 2ad0e94 | 2017-02-28 19:14:08 +0300 | [diff] [blame] | 161 | protected override fun pollSelectInternal(select: SelectInstance<*>): Any? { |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 162 | var send: Send? = null |
| 163 | var token: Any? = null |
| 164 | var result: Any? = null |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 165 | lock.withLock { |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 166 | val size = this.size |
| 167 | if (size == 0) return closedForSend ?: POLL_FAILED |
| 168 | // size > 0: not empty -- retrieve element |
| 169 | result = buffer[head] |
| 170 | buffer[head] = null |
| 171 | this.size = size - 1 // update size before checking queue (!!!) |
| 172 | // check for senders that were waiting on full queue |
| 173 | var replacement: Any? = POLL_FAILED |
| 174 | if (size == capacity) { |
| 175 | loop@ while (true) { |
| 176 | val pollOp = describeTryPoll() |
| 177 | val failure = select.performAtomicTrySelect(pollOp) |
| 178 | when { |
| 179 | failure == null -> { // polled successfully |
| 180 | send = pollOp.result |
| 181 | token = pollOp.resumeToken |
| 182 | check(token != null) |
| 183 | replacement = send!!.pollResult |
| 184 | break@loop |
| 185 | } |
| 186 | failure === POLL_FAILED -> break@loop // cannot poll -> Ok to take from buffer |
| 187 | failure === ALREADY_SELECTED -> { |
| 188 | this.size = size // restore size |
| 189 | buffer[head] = result // restore head |
| 190 | return failure |
| 191 | } |
| 192 | failure is Closed<*> -> { |
| 193 | send = failure |
| 194 | token = failure.tryResumeSend(idempotent = null) |
| 195 | replacement = failure |
| 196 | break@loop |
| 197 | } |
| 198 | else -> error("performAtomicTrySelect(describeTryOffer) returned $failure") |
| 199 | } |
| 200 | } |
| 201 | } |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 202 | if (replacement !== POLL_FAILED && replacement !is Closed<*>) { |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 203 | this.size = size // restore size |
| 204 | buffer[(head + size) % capacity] = replacement |
| 205 | } else { |
| 206 | // failed to poll or is already closed --> let's try to select receiving this element from buffer |
Roman Elizarov | e3aa8ff | 2017-04-27 19:16:40 +0300 | [diff] [blame] | 207 | if (!select.trySelect(null)) { // :todo: move trySelect completion outside of lock |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 208 | this.size = size // restore size |
| 209 | buffer[head] = result // restore head |
| 210 | return ALREADY_SELECTED |
| 211 | } |
| 212 | } |
| 213 | head = (head + 1) % capacity |
| 214 | } |
| 215 | // complete send the we're taken replacement from |
| 216 | if (token != null) |
| 217 | send!!.completeResumeSend(token!!) |
| 218 | return result |
| 219 | } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 220 | |
| 221 | // Note: this function is invoked when channel is already closed |
| 222 | override fun cleanupSendQueueOnCancel() { |
| 223 | // clear buffer first |
| 224 | lock.withLock { |
| 225 | repeat(size) { |
| 226 | buffer[head] = 0 |
| 227 | head = (head + 1) % capacity |
| 228 | } |
| 229 | size = 0 |
| 230 | } |
| 231 | // then clean all queued senders |
| 232 | super.cleanupSendQueueOnCancel() |
| 233 | } |
Roman Elizarov | e4b6f09 | 2018-03-06 13:37:42 +0300 | [diff] [blame] | 234 | |
| 235 | // ------ debug ------ |
| 236 | |
| 237 | override val bufferDebugString: String |
| 238 | get() = "(buffer:capacity=${buffer.size},size=$size)" |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 239 | } |