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