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

ben-manes / caffeine / #5155

03 Dec 2025 02:00AM UTC coverage: 0.0% (-100.0%) from 100.0%
#5155

push

github

ben-manes
add loading type to parameterized test dimensions to reduce task size

0 of 3834 branches covered (0.0%)

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

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

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

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

62
    final Object[] buffer;
63

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

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

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

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

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

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

127
  private BBHeader() {}
128

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

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

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

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

178
    volatile long writeCounter;
179

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

184
    long writeCounterOpaque() {
185
      return (long) WRITE.getOpaque(this);
×
186
    }
187

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

192
    static VarHandle findVarHandle(Class<?> recv, String name, Class<?> type) {
193
      try {
194
        return MethodHandles.lookup().findVarHandle(recv, name, type);
×
195
      } catch (ReflectiveOperationException e) {
×
196
        throw new ExceptionInInitializerError(e);
×
197
      }
198
    }
199
  }
200
}
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