• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ben-manes / caffeine / #5173

29 Dec 2025 05:27AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5173

push

github

ben-manes
speed up development ci build

0 of 3838 branches covered (0.0%)

0 of 7869 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedBuffer.java
1
/*
2
 * Copyright 2015 Ben Manes. All Rights Reserved.
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
package com.github.benmanes.caffeine.cache;
17

18
import java.lang.invoke.MethodHandles;
19
import java.lang.invoke.VarHandle;
20
import java.util.function.Consumer;
21

22
import com.google.errorprone.annotations.Var;
23

24
import org.jspecify.annotations.Nullable;
25

26
/**
27
 * A striped, non-blocking, bounded buffer.
28
 *
29
 * @author ben.manes@gmail.com (Ben Manes)
30
 * @param <E> the type of elements maintained by this buffer
31
 */
32
final class BoundedBuffer<E> extends StripedBuffer<E> {
×
33
  /*
34
   * A circular ring buffer stores the elements being transferred by the producers to the consumer.
35
   * The monotonically increasing count of reads and writes allow indexing sequentially to the next
36
   * element location based upon a power-of-two sizing.
37
   *
38
   * The producers race to read the counts, check if there is available capacity, and if so then try
39
   * once to CAS to the next write count. If the increment is successful then the producer lazily
40
   * publishes the element. The producer does not retry or block when unsuccessful due to a failed
41
   * CAS or the buffer being full.
42
   *
43
   * The consumer reads the counts and takes the available elements. The clearing of the elements
44
   * and the next read count are lazily set.
45
   *
46
   * This implementation is striped to further increase concurrency by rehashing and dynamically
47
   * adding new buffers when contention is detected, up to an internal maximum. When rehashing in
48
   * order to discover an available buffer, the producer may retry adding its element to determine
49
   * whether it found a satisfactory buffer or if resizing is necessary.
50
   */
51

52
  /** The maximum number of elements per buffer. */
53
  static final int BUFFER_SIZE = 16;
54
  static final int MASK = BUFFER_SIZE - 1;
55

56
  @Override
57
  protected Buffer<E> create(E e) {
58
    return new RingBuffer<>(e);
×
59
  }
60

61
  static final class RingBuffer<E> extends BBHeader.ReadAndWriteCounterRef implements Buffer<E> {
62
    static final VarHandle BUFFER = MethodHandles.arrayElementVarHandle(Object[].class);
×
63

64
    final @Nullable Object[] buffer;
65

66
    public RingBuffer(E e) {
×
67
      buffer = new Object[BUFFER_SIZE];
×
68
      BUFFER.set(buffer, 0, e);
×
69
      WRITE.set(this, 1);
×
70
    }
×
71

72
    @Override
73
    @SuppressWarnings("Varifier")
74
    public int offer(E e) {
75
      long head = readCounter;
×
76
      long tail = writeCounterOpaque();
×
77
      long size = (tail - head);
×
78
      if (size >= BUFFER_SIZE) {
×
79
        return Buffer.FULL;
×
80
      }
81
      if (casWriteCounter(tail, tail + 1)) {
×
82
        int index = (int) (tail & MASK);
×
83
        BUFFER.setRelease(buffer, index, e);
×
84
        return Buffer.SUCCESS;
×
85
      }
86
      return Buffer.FAILED;
×
87
    }
88

89
    @Override
90
    @SuppressWarnings("Varifier")
91
    public void drainTo(Consumer<E> consumer) {
92
      @Var long head = readCounter;
×
93
      long tail = writeCounterOpaque();
×
94
      long size = (tail - head);
×
95
      if (size == 0) {
×
96
        return;
×
97
      }
98
      do {
99
        int index = (int) (head & MASK);
×
100
        @SuppressWarnings("unchecked")
101
        var e = (E) BUFFER.getAcquire(buffer, index);
×
102
        if (e == null) {
×
103
          // not published yet
104
          break;
×
105
        }
106
        BUFFER.setRelease(buffer, index, null);
×
107
        consumer.accept(e);
×
108
        head++;
×
109
      } while (head != tail);
×
110
      setReadCounterOpaque(head);
×
111
    }
×
112

113
    @Override
114
    public long reads() {
115
      return readCounter;
×
116
    }
117

118
    @Override
119
    public long writes() {
120
      return writeCounter;
×
121
    }
122
  }
123
}
124

125
/** The namespace for field padding through inheritance. */
126
@SuppressWarnings({"MemberName", "MultiVariableDeclaration"})
127
final class BBHeader {
128

129
  private BBHeader() {}
130

131
  @SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
132
  abstract static class PadReadCounter {
×
133
    byte p000, p001, p002, p003, p004, p005, p006, p007;
134
    byte p008, p009, p010, p011, p012, p013, p014, p015;
135
    byte p016, p017, p018, p019, p020, p021, p022, p023;
136
    byte p024, p025, p026, p027, p028, p029, p030, p031;
137
    byte p032, p033, p034, p035, p036, p037, p038, p039;
138
    byte p040, p041, p042, p043, p044, p045, p046, p047;
139
    byte p048, p049, p050, p051, p052, p053, p054, p055;
140
    byte p056, p057, p058, p059, p060, p061, p062, p063;
141
    byte p064, p065, p066, p067, p068, p069, p070, p071;
142
    byte p072, p073, p074, p075, p076, p077, p078, p079;
143
    byte p080, p081, p082, p083, p084, p085, p086, p087;
144
    byte p088, p089, p090, p091, p092, p093, p094, p095;
145
    byte p096, p097, p098, p099, p100, p101, p102, p103;
146
    byte p104, p105, p106, p107, p108, p109, p110, p111;
147
    byte p112, p113, p114, p115, p116, p117, p118, p119;
148
  }
149

150
  /** Enforces a memory layout to avoid false sharing by padding the read count. */
151
  abstract static class ReadCounterRef extends PadReadCounter {
×
152
    volatile long readCounter;
153
  }
154

155
  abstract static class PadWriteCounter extends ReadCounterRef {
×
156
    byte p120, p121, p122, p123, p124, p125, p126, p127;
157
    byte p128, p129, p130, p131, p132, p133, p134, p135;
158
    byte p136, p137, p138, p139, p140, p141, p142, p143;
159
    byte p144, p145, p146, p147, p148, p149, p150, p151;
160
    byte p152, p153, p154, p155, p156, p157, p158, p159;
161
    byte p160, p161, p162, p163, p164, p165, p166, p167;
162
    byte p168, p169, p170, p171, p172, p173, p174, p175;
163
    byte p176, p177, p178, p179, p180, p181, p182, p183;
164
    byte p184, p185, p186, p187, p188, p189, p190, p191;
165
    byte p192, p193, p194, p195, p196, p197, p198, p199;
166
    byte p200, p201, p202, p203, p204, p205, p206, p207;
167
    byte p208, p209, p210, p211, p212, p213, p214, p215;
168
    byte p216, p217, p218, p219, p220, p221, p222, p223;
169
    byte p224, p225, p226, p227, p228, p229, p230, p231;
170
    byte p232, p233, p234, p235, p236, p237, p238, p239;
171
  }
172

173
  /** Enforces a memory layout to avoid false sharing by padding the write counter. */
174
  abstract static class ReadAndWriteCounterRef extends PadWriteCounter {
×
175
    static final VarHandle READ = findVarHandle(
×
176
        ReadCounterRef.class, "readCounter", long.class);
177
    static final VarHandle WRITE = findVarHandle(
×
178
        ReadAndWriteCounterRef.class, "writeCounter", long.class);
179

180
    volatile long writeCounter;
181

182
    void setReadCounterOpaque(long count) {
183
      READ.setOpaque(this, count);
×
184
    }
×
185

186
    long writeCounterOpaque() {
187
      return (long) WRITE.getOpaque(this);
×
188
    }
189

190
    boolean casWriteCounter(long expect, long update) {
191
      return WRITE.weakCompareAndSet(this, expect, update);
×
192
    }
193

194
    static VarHandle findVarHandle(Class<?> recv, String name, Class<?> type) {
195
      try {
196
        return MethodHandles.lookup().findVarHandle(recv, name, type);
×
197
      } catch (ReflectiveOperationException e) {
×
198
        throw new ExceptionInInitializerError(e);
×
199
      }
200
    }
201
  }
202
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc