Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.io.internal |
| 2 | |
Roman Elizarov | 8376203 | 2017-08-08 19:58:40 +0300 | [diff] [blame] | 3 | internal class RingBufferCapacity(private val totalCapacity: Int) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 4 | @Volatile @JvmField |
| 5 | var availableForRead = 0 |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 6 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 7 | @Volatile @JvmField |
| 8 | var availableForWrite = totalCapacity |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 9 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 10 | @Volatile @JvmField |
| 11 | var pendingToFlush = 0 |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 12 | |
| 13 | // concurrent unsafe! |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 14 | fun resetForWrite() { |
| 15 | availableForRead = 0 |
| 16 | availableForWrite = totalCapacity |
| 17 | pendingToFlush = 0 |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 18 | } |
| 19 | |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 20 | fun resetForRead() { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 21 | availableForRead = totalCapacity |
| 22 | availableForWrite = 0 |
| 23 | pendingToFlush = 0 |
Sergey Mashkov | 569b542 | 2017-07-25 12:17:11 +0300 | [diff] [blame] | 24 | } |
| 25 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 26 | fun tryReadExact(n: Int): Boolean { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 27 | val AvailableForRead = AvailableForRead |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 28 | while (true) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 29 | val remaining = availableForRead |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 30 | if (remaining < n) return false |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 31 | if (AvailableForRead.compareAndSet(this, remaining, remaining - n)) return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | fun tryReadAtMost(n: Int): Int { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 36 | val AvailableForRead = AvailableForRead |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 37 | while (true) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 38 | val remaining = availableForRead |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 39 | val delta = minOf(n, remaining) |
| 40 | if (delta == 0) return 0 |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 41 | if (AvailableForRead.compareAndSet(this, remaining, remaining - delta)) return delta |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
Sergey Mashkov | 7bf4fa9 | 2017-11-17 18:39:03 +0300 | [diff] [blame] | 45 | fun tryWriteAtLeast(n: Int): Int { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 46 | val AvailableForWrite = AvailableForWrite |
Sergey Mashkov | 7bf4fa9 | 2017-11-17 18:39:03 +0300 | [diff] [blame] | 47 | while (true) { |
| 48 | val remaining = availableForWrite |
| 49 | if (remaining < n) return 0 |
| 50 | if (AvailableForWrite.compareAndSet(this, remaining, 0)) return remaining |
| 51 | } |
| 52 | } |
| 53 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 54 | fun tryWriteExact(n: Int): Boolean { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 55 | val AvailableForWrite = AvailableForWrite |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 56 | while (true) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 57 | val remaining = availableForWrite |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 58 | if (remaining < n) return false |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 59 | if (AvailableForWrite.compareAndSet(this, remaining, remaining - n)) return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | fun tryWriteAtMost(n: Int): Int { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 64 | val AvailableForWrite = AvailableForWrite |
| 65 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 66 | while (true) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 67 | val remaining = availableForWrite |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 68 | val delta = minOf(n, remaining) |
| 69 | if (delta == 0) return 0 |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 70 | if (AvailableForWrite.compareAndSet(this, remaining, remaining - delta)) return delta |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | fun completeRead(n: Int) { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 75 | val totalCapacity = totalCapacity |
| 76 | val AvailableForWrite = AvailableForWrite |
| 77 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 78 | while (true) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 79 | val remaining = availableForWrite |
| 80 | val update = remaining + n |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 81 | if (update > totalCapacity) completeReadOverflow(remaining, update, n) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 82 | if (AvailableForWrite.compareAndSet(this, remaining, update)) break |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 86 | private fun completeReadOverflow(remaining: Int, update: Int, n: Int): Nothing { |
| 87 | throw IllegalArgumentException("Completed read overflow: $remaining + $n = $update > $totalCapacity") |
| 88 | } |
| 89 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 90 | fun completeWrite(n: Int) { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 91 | val totalCapacity = totalCapacity |
| 92 | val PendingToFlush = PendingToFlush |
| 93 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 94 | while (true) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 95 | val pending = pendingToFlush |
| 96 | val update = pending + n |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 97 | if (update > totalCapacity) completeReadOverflow(pending, n) |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 98 | if (PendingToFlush.compareAndSet(this, pending, update)) break |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 102 | private fun completeReadOverflow(pending: Int, n: Int): Nothing { |
| 103 | throw IllegalArgumentException("Complete write overflow: $pending + $n > $totalCapacity") |
| 104 | } |
| 105 | |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 106 | /** |
| 107 | * @return true if there are bytes available for read after flush |
| 108 | */ |
| 109 | fun flush(): Boolean { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 110 | val AvailableForRead = AvailableForRead |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 111 | val pending = PendingToFlush.getAndSet(this, 0) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 112 | while (true) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 113 | val remaining = availableForRead |
| 114 | val update = remaining + pending |
| 115 | if (remaining == update || AvailableForRead.compareAndSet(this, remaining, update)) { |
| 116 | return update > 0 |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fun tryLockForRelease(): Boolean { |
Sergey Mashkov | ffc7134 | 2017-12-18 19:31:08 +0300 | [diff] [blame] | 122 | val AvailableForWrite = AvailableForWrite |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 123 | while (true) { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 124 | val remaining = availableForWrite |
| 125 | if (pendingToFlush > 0 || availableForRead > 0 || remaining != totalCapacity) return false |
| 126 | if (AvailableForWrite.compareAndSet(this, remaining, 0)) return true |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Sergey Mashkov | 98c8739 | 2017-11-20 17:58:28 +0300 | [diff] [blame] | 130 | /** |
| 131 | * Make all writers to fail to write any more bytes |
| 132 | * Use only during failure termination |
| 133 | */ |
| 134 | fun forceLockForRelease() { |
| 135 | AvailableForWrite.getAndSet(this, 0) |
| 136 | } |
| 137 | |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 138 | fun isEmpty(): Boolean = availableForWrite == totalCapacity |
| 139 | fun isFull(): Boolean = availableForWrite == 0 |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 140 | |
| 141 | companion object { |
Roman Elizarov | 469cad3 | 2017-08-15 15:54:56 +0300 | [diff] [blame] | 142 | // todo: replace with atomicfu, remove companion object |
| 143 | private val AvailableForRead = intUpdater(RingBufferCapacity::availableForRead) |
| 144 | private val AvailableForWrite = intUpdater(RingBufferCapacity::availableForWrite) |
| 145 | private val PendingToFlush = intUpdater(RingBufferCapacity::pendingToFlush) |
Sergey Mashkov | 86f7061 | 2017-07-25 10:59:44 +0300 | [diff] [blame] | 146 | } |
| 147 | } |