| 1 | // Copyright (c) 2024 Alexey Mayshev and contributors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // This is a port of lossy buffers from Caffeine. |
| 16 | // https://github.com/ben-manes/caffeine/blob/master/caffeine/src/main/java/com/github/benmanes/caffeine/cache/StripedBuffer.java |
| 17 | |
| 18 | package lossyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ |
| 19 | |
| 20 | import ( |
| 21 | "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/" |
| 22 | "sync/atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/" |
| 23 | |
| 24 | "github.com/maypok86/otter/v2/internal/generated/nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/" |
| 25 | "github.com/maypok86/otter/v2/internal/xruntimescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/" |
| 26 | ) |
| 27 | |
| 28 | const ( |
| 29 | attemptsscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/attempts. = 3 |
| 30 | ) |
| 31 | |
| 32 | // pool for P tokens. |
| 33 | var tokenPoolscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/tokenPool. syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.Poolscip-go gomod github.com/golang/go/src go1.24.0 sync/Pool# |
| 34 | |
| 35 | // a P token is used to point at the current OS thread (P) |
| 36 | // on which the goroutine is run; exact identity of the thread, |
| 37 | // as well as P migration tolerance, is not important since |
| 38 | // it's used to as a best effort mechanism for assigning |
| 39 | // concurrent operations (goroutines) to different stripes of |
| 40 | // the Adder. |
| 41 | type tokenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token# struct { |
| 42 | idxscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#idx. uint32 |
| 43 | paddingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#padding. [xruntimescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/.CacheLineSizescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/CacheLineSize. - 4]byte |
| 44 | } |
| 45 | |
| 46 | type stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#[K comparable, V any] struct { |
| 47 | buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers. []atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.Pointerscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#[ringscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ring#[K, V]] |
| 48 | lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len. int |
| 49 | } |
| 50 | |
| 51 | // Striped is a multiple-producer / single-consumer buffer that rejects new elements if it is full or |
| 52 | // fails spuriously due to contention. Unlike a queue and stack, a buffer does not guarantee an |
| 53 | // ordering of elements in either FIFO or LIFO order. |
| 54 | type Stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#[K comparable, V any] struct { |
| 55 | nodeManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#nodeManager. *nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Managerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Manager#[K, V] |
| 56 | maxLenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#maxLen. int |
| 57 | stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped. atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.Pointerscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#[stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#[K, V]] |
| 58 | busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy. atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.Uint32scip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32# |
| 59 | } |
| 60 | |
| 61 | func NewStripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/NewStriped().[K comparable, V any](maxLen int, nodeManager *nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Managerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Manager#[K, V]) *Stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#[K, V] { |
| 62 | return &Stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#[K, V]{ |
| 63 | nodeManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#nodeManager.: nodeManager, |
| 64 | maxLenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#maxLen.: maxLen, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Add inserts the specified element into this buffer if it is possible to do so immediately without |
| 69 | // violating capacity restrictions. The addition is allowed to fail spuriously if multiple |
| 70 | // goroutines insert concurrently. |
| 71 | func (s *Stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#[K, V]) Addscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#Add().(n nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Node#[K, V]) Statusscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Status# { |
| 72 | t, ok := tokenPoolscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/tokenPool..Getscip-go gomod github.com/golang/go/src go1.24.0 sync/Pool#Get().().(*tokenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#) |
| 73 | if !ok { |
| 74 | t = &tokenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#{ |
| 75 | idxscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#idx.: xruntimescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/.Fastrandscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/Fastrand().(), |
| 76 | } |
| 77 | } |
| 78 | defer tokenPoolscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/tokenPool..Putscip-go gomod github.com/golang/go/src go1.24.0 sync/Pool#Put().(t) |
| 79 | |
| 80 | bs := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 81 | if bs == nil { |
| 82 | return s.expandOrRetryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#expandOrRetry().(n, t, true) |
| 83 | } |
| 84 | |
| 85 | //nolint:gosec // len will never overflow uint32 |
| 86 | buffer := bs.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[t.idxscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#idx.&uint32(bs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.-1)].Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 87 | if buffer == nil { |
| 88 | return s.expandOrRetryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#expandOrRetry().(n, t, true) |
| 89 | } |
| 90 | |
| 91 | result := buffer.addscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ring#add().(n) |
| 92 | if result == Failedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Failed. { |
| 93 | return s.expandOrRetryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#expandOrRetry().(n, t, false) |
| 94 | } |
| 95 | |
| 96 | return result |
| 97 | } |
| 98 | |
| 99 | func (s *Stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#[K, V]) expandOrRetryscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#expandOrRetry().(n nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Node#[K, V], t *tokenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#, wasUncontended bool) Statusscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Status# { |
| 100 | result := Failedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Failed. |
| 101 | // True if last slot nonempty. |
| 102 | collide := true |
| 103 | |
| 104 | for attempt := 0; attempt < attemptsscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/attempts.; attempt++ { |
| 105 | bs := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 106 | if bs != nil && bs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len. > 0 { |
| 107 | //nolint:gosec // len will never overflow uint32 |
| 108 | buffer := bs.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[t.idxscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#idx.&uint32(bs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.-1)].Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 109 | //nolint:gocritic // the switch statement looks even worse here |
| 110 | if buffer == nil { |
| 111 | if s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#Load().() == 0 && s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..CompareAndSwapscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#CompareAndSwap().(0, 1) { |
| 112 | // Try to attach new buffer. |
| 113 | created := false |
| 114 | rs := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 115 | if rs != nil && rs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len. > 0 { |
| 116 | // Recheck under lock. |
| 117 | //nolint:gosec // len will never overflow uint32 |
| 118 | j := t.idxscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#idx. & uint32(rs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.-1) |
| 119 | if rs.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[j].Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() == nil { |
| 120 | rs.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[j].Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Store().(newRingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/newRing().(s.nodeManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#nodeManager., n)) |
| 121 | created = true |
| 122 | } |
| 123 | } |
| 124 | s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#Store().(0) |
| 125 | if created { |
| 126 | result = Successscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Success. |
| 127 | break |
| 128 | } |
| 129 | // Slot is now non-empty. |
| 130 | continue |
| 131 | } |
| 132 | collide = false |
| 133 | } else if !wasUncontended { |
| 134 | // CAS already known to fail. |
| 135 | // Continue after rehash. |
| 136 | wasUncontended = true |
| 137 | } else { |
| 138 | result = buffer.addscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ring#add().(n) |
| 139 | //nolint:gocritic // the switch statement looks even worse here |
| 140 | if result != Failedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Failed. { |
| 141 | break |
| 142 | } else if bs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len. >= s.maxLenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#maxLen. || s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() != bs { |
| 143 | // At max size or stale. |
| 144 | collide = false |
| 145 | } else if !collide { |
| 146 | collide = true |
| 147 | } else if s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#Load().() == 0 && s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..CompareAndSwapscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#CompareAndSwap().(0, 1) { |
| 148 | if s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() == bs { |
| 149 | length := bs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len. << 1 |
| 150 | striped := &stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#[K, V]{ |
| 151 | buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.: make([]atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.Pointerscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#[ringscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ring#[K, V]], length), |
| 152 | lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.: length, |
| 153 | } |
| 154 | for j := 0; j < bs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.; j++ { |
| 155 | striped.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[j].Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Store().(bs.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[j].Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().()) |
| 156 | } |
| 157 | s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Store().(striped) |
| 158 | } |
| 159 | s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#Store().(0) |
| 160 | collide = false |
| 161 | continue |
| 162 | } |
| 163 | } |
| 164 | t.idxscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/token#idx. = xruntimescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/.Fastrandscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/xruntime`/Fastrand().() |
| 165 | } else if s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#Load().() == 0 && s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() == bs && s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..CompareAndSwapscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#CompareAndSwap().(0, 1) { |
| 166 | init := false |
| 167 | if s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() == bs { |
| 168 | striped := &stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#[K, V]{ |
| 169 | buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.: make([]atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.Pointerscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#[ringscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ring#[K, V]], 1), |
| 170 | lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.: 1, |
| 171 | } |
| 172 | striped.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[0].Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Store().(newRingscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/newRing().(s.nodeManagerscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#nodeManager., n)) |
| 173 | s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Store().(striped) |
| 174 | init = true |
| 175 | } |
| 176 | s.busyscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#busy..Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Uint32#Store().(0) |
| 177 | if init { |
| 178 | result = Successscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Success. |
| 179 | break |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return result |
| 185 | } |
| 186 | |
| 187 | // DrainTo drains the buffer, sending each element to the consumer for processing. The caller must ensure |
| 188 | // that a consumer has exclusive read access to the buffer. |
| 189 | func (s *Stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#[K, V]) DrainToscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#DrainTo().(consumer func(n nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/.Nodescip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/generated/node`/Node#[K, V])) { |
| 190 | bs := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 191 | if bs == nil { |
| 192 | return |
| 193 | } |
| 194 | for i := 0; i < bs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.; i++ { |
| 195 | b := bs.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[i].Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 196 | if b != nil { |
| 197 | b.drainToscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ring#drainTo().(consumer) |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | func (s *Stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#[K, V]) Lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#Len().() int { |
| 203 | result := 0 |
| 204 | bs := s.stripedscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/Striped#striped..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 205 | if bs == nil { |
| 206 | return result |
| 207 | } |
| 208 | for i := 0; i < bs.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#len.; i++ { |
| 209 | b := bs.buffersscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/striped#buffers.[i].Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Pointer#Load().() |
| 210 | if b == nil { |
| 211 | continue |
| 212 | } |
| 213 | result += b.lenscip-go gomod github.com/maypok86/otter/v2 . `github.com/maypok86/otter/v2/internal/lossy`/ring#len().() |
| 214 | } |
| 215 | return result |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | func (s *Striped[K, V]) Clear() { |
| 220 | bs := s.striped.Load() |
| 221 | if bs == nil { |
| 222 | return |
| 223 | } |
| 224 | for s.busy.Load() != 0 || !s.busy.CompareAndSwap(0, 1) { |
| 225 | runtime.Gosched() |
| 226 | } |
| 227 | for i := 0; i < bs.len; i++ { |
| 228 | b := bs.buffers[i].Load() |
| 229 | if b != nil { |
| 230 | b.clear() |
| 231 | } |
| 232 | } |
| 233 | s.busy.Store(0) |
| 234 | } |
| 235 | */ |