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