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