Do not lock the handler calls in AsyncFile - see #3140

This commit is contained in:
Julien Viet
2019-10-14 10:03:55 +02:00
parent 62efc9835d
commit 1018724a96
2 changed files with 31 additions and 4 deletions

View File

@@ -400,22 +400,30 @@ public class AsyncFileImpl implements AsyncFile {
});
}
private synchronized void handleBuffer(Buffer buff) {
private void handleBuffer(Buffer buff) {
Handler<Buffer> handler;
synchronized (this) {
handler = this.handler;
}
if (handler != null) {
checkContext();
handler.handle(buff);
}
}
private synchronized void handleEnd() {
handler = null;
private void handleEnd() {
Handler<Void> endHandler;
synchronized (this) {
handler = null;
endHandler = this.endHandler;
}
if (endHandler != null) {
checkContext();
endHandler.handle(null);
}
}
private synchronized void doFlush(Handler<AsyncResult<Void>> handler) {
checkClosed();
context.executeBlockingInternal((Promise<Void> fut) -> {

View File

@@ -1564,6 +1564,25 @@ public class FileSystemTest extends VertxTestBase {
await();
}
@Test
public void testReadStreamNoLock() throws Exception {
String fileName = "some-file.dat";
int chunkSize = 16384;
int chunks = 1;
byte[] content = TestUtils.randomByteArray(chunkSize * chunks);
createFile(fileName, content);
vertx.fileSystem().open(testDir + pathSep + fileName, new OpenOptions(), onSuccess(rs -> {
rs.handler(buff -> {
assertFalse(Thread.holdsLock(rs));
});
rs.endHandler(v -> {
assertFalse(Thread.holdsLock(rs));
testComplete();
});
}));
await();
}
@Test
@SuppressWarnings("unchecked")
public void testPumpFileStreams() throws Exception {