Skip to content

Instantly share code, notes, and snippets.

@purplefox
Last active June 8, 2017 15:42
Show Gist options
  • Save purplefox/723fa69a1330fb4f4f74d4a5dd5e6658 to your computer and use it in GitHub Desktop.
Save purplefox/723fa69a1330fb4f4f74d4a5dd5e6658 to your computer and use it in GitHub Desktop.
diff --git a/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java b/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java
index c9f8992..301bde9 100644
--- a/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java
+++ b/src/main/java/io/vertx/core/file/impl/AsyncFileImpl.java
@@ -43,6 +43,7 @@ import java.util.HashSet;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
/**
*
@@ -65,7 +66,7 @@ public class AsyncFileImpl implements AsyncFile {
private final ContextImpl context;
private boolean closed;
private Runnable closedDeferred;
- private long writesOutstanding;
+ private AtomicLong writesOutstanding = new AtomicLong();
private Handler<Throwable> exceptionHandler;
private Handler<Void> drainHandler;
private long writePos;
@@ -148,7 +149,7 @@ public class AsyncFileImpl implements AsyncFile {
Handler<AsyncResult<Void>> wrapped = ar -> {
if (ar.succeeded()) {
checkContext();
- if (writesOutstanding == 0 && closedDeferred != null) {
+ if (writesOutstanding.get() == 0 && closedDeferred != null) {
closedDeferred.run();
} else {
checkDrained();
@@ -200,7 +201,7 @@ public class AsyncFileImpl implements AsyncFile {
@Override
public synchronized boolean writeQueueFull() {
check();
- return writesOutstanding >= maxWrites;
+ return writesOutstanding.get() >= maxWrites;
}
@Override
@@ -280,7 +281,7 @@ public class AsyncFileImpl implements AsyncFile {
}
private synchronized void checkDrained() {
- if (drainHandler != null && writesOutstanding <= lwm) {
+ if (drainHandler != null && writesOutstanding.get() <= lwm) {
Handler<Void> handler = drainHandler;
drainHandler = null;
handler.handle(null);
@@ -371,7 +372,7 @@ public class AsyncFileImpl implements AsyncFile {
if (toWrite == 0) {
throw new IllegalStateException("Cannot save zero bytes");
}
- writesOutstanding += toWrite;
+ writesOutstanding.addAndGet(toWrite);
writeInternal(buff, position, handler);
}
@@ -391,7 +392,7 @@ public class AsyncFileImpl implements AsyncFile {
} else {
// It's been fully written
context.runOnContext((v) -> {
- writesOutstanding -= buff.limit();
+ writesOutstanding.addAndGet(-buff.limit());
handler.handle(Future.succeededFuture());
});
}
@@ -476,7 +477,7 @@ public class AsyncFileImpl implements AsyncFile {
closed = true;
- if (writesOutstanding == 0) {
+ if (writesOutstanding.get() == 0) {
doClose(handler);
} else {
closedDeferred = () -> doClose(handler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment