Skip to content

Commit

Permalink
All ArrayChannel.size accesses should be under the channel lock
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkoval authored and elizarov committed Oct 2, 2019
1 parent c5a42da commit cfc08ee
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions kotlinx-coroutines-core/common/src/channels/ArrayChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,12 @@ internal open class ArrayChannel<E>(
*/
private var buffer: Array<Any?> = arrayOfNulls<Any?>(min(capacity, 8))
private var head: Int = 0

private val _size = atomic(0)
private var size: Int // Invariant: size <= capacity
get() = _size.value
set(value) { _size.value = value }
private var size = 0 // Invariant: size <= capacity

protected final override val isBufferAlwaysEmpty: Boolean get() = false
protected final override val isBufferEmpty: Boolean get() = lock.withLock { size == 0 }
protected final override val isBufferAlwaysFull: Boolean get() = false
protected final override val isBufferFull: Boolean get() = size == capacity
protected final override val isBufferFull: Boolean get() = lock.withLock { size == capacity }

// result is `OFFER_SUCCESS | OFFER_FAILED | Closed`
protected override fun offerInternal(element: E): Any {
Expand Down

0 comments on commit cfc08ee

Please sign in to comment.