Skip to content

Commit a3633a9

Browse files
committed
document expected count values
also remove BARRIER as that was only necessary for previous implementation where cancel always deprecated the count we now use a magic value of MAX_INT/2 to represent a hold, to distinguish it from an individual activation
1 parent 5c60de4 commit a3633a9

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/core/scopemanager/ScopeContinuation.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,32 @@ final class ScopeContinuation implements AgentScope.Continuation {
2020

2121
// these boundaries were selected to allow for speculative counting and fuzzy checks
2222
private static final int CANCELLED = Integer.MIN_VALUE >> 1;
23-
private static final int BARRIER = Integer.MIN_VALUE >> 2;
2423
private static final int HELD = (Integer.MAX_VALUE >> 1) + 1;
2524

2625
final ContinuableScopeManager scopeManager;
2726
final AgentSpan spanUnderScope;
2827
final byte source;
2928
final AgentTraceCollector traceCollector;
3029

30+
/**
31+
* When positive this reflects the number of outstanding activations as well as whether there is
32+
* an active hold on the continuation:
33+
*
34+
* <table>
35+
* <tr><th>Value</th> <th>Meaning</th></tr>
36+
* <tr><td>0</td><td>Not held or activated</td></tr>
37+
* <tr><td>1..HELD-1</td><td>Activated, not held</td></tr>
38+
* <tr><td>HELD</td><td>Held, not yet activated</td></tr>
39+
* <tr><td>HELD..MAX_INT</td><td>Activated and held</td></tr>
40+
* </table>
41+
*
42+
* where HELD is at the mid-point between 1 and MAX_INT.
43+
*
44+
* <p>A negative value of CANCELLED reflects that the continuation has either been activated and
45+
* all associated scopes are now closed, or it has been explicitly cancelled. This value was
46+
* chosen to be half the size of MIN_INT to avoid speculative additions in {@link #activate()}
47+
* from overflowing to a positive count.
48+
*/
3149
private volatile int count = 0;
3250

3351
ScopeContinuation(
@@ -73,7 +91,7 @@ public void cancel() {
7391
COUNT.compareAndSet(this, current, current - HELD);
7492
current = count;
7593
}
76-
while (current <= 0 && current > BARRIER) {
94+
while (current == 0) {
7795
// no outstanding activations and hold has been removed
7896
if (COUNT.compareAndSet(this, current, CANCELLED)) {
7997
traceCollector.cancelContinuation(this);

0 commit comments

Comments
 (0)