Skip to content

Commit 5911705

Browse files
daschlMichael Nitschinger
authored andcommitted
SPY-174: Make sure MultiOperationCallback is threadsafe
First, special thanks goes to @exortech for reporting and fixing the issue here: #9 Motivation ---------- Certain operations like the multi get callback rely on the multi operation callback which has a non-thread safe component to it. Modification ------------ Make the callback atomic and thread safe. Result ------ No race conditions with bulk get operation callbacks. Change-Id: I511a8e5ec6e8fe50168337a4b9bbddf2360bd365 Reviewed-on: http://review.couchbase.org/38358 Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com> Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
1 parent 1afbf56 commit 5911705

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/main/java/net/spy/memcached/ops/MultiOperationCallback.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
package net.spy.memcached.ops;
2424

25+
import java.util.concurrent.atomic.AtomicInteger;
26+
2527
/**
2628
* An operation callback that will capture receivedStatus and complete
2729
* invocations and dispatch to a single callback.
@@ -34,7 +36,7 @@
3436
public abstract class MultiOperationCallback implements OperationCallback {
3537

3638
private OperationStatus mostRecentStatus = null;
37-
private int remaining = 0;
39+
private final AtomicInteger remaining;
3840
protected final OperationCallback originalCallback;
3941

4042
/**
@@ -50,11 +52,11 @@ public MultiOperationCallback(OperationCallback original, int todo) {
5052
}
5153

5254
originalCallback = original;
53-
remaining = todo;
55+
remaining = new AtomicInteger(todo);
5456
}
5557

5658
public void complete() {
57-
if (--remaining == 0) {
59+
if (remaining.decrementAndGet() == 0) {
5860
originalCallback.receivedStatus(mostRecentStatus);
5961
originalCallback.complete();
6062
}

0 commit comments

Comments
 (0)