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