Cleanup in EmulatorClipboardMonitor (2)
The whole `openPipe` function does not have to be synchronized. Bug: 231345789 Test: presubmit Signed-off-by: Roman Kiryanov <rkir@google.com> Change-Id: I5241086c947a4227fb32648f0693303be66f4d46 Merged-In: I5241086c947a4227fb32648f0693303be66f4d46
This commit is contained in:
@@ -60,11 +60,11 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
return mPipe;
|
return mPipe;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized boolean openPipe() {
|
private synchronized void setPipeFD(final FileDescriptor fd) {
|
||||||
if (mPipe != null) {
|
mPipe = fd;
|
||||||
return true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
private static FileDescriptor openPipeImpl() {
|
||||||
try {
|
try {
|
||||||
final FileDescriptor fd = Os.socket(OsConstants.AF_VSOCK, OsConstants.SOCK_STREAM, 0);
|
final FileDescriptor fd = Os.socket(OsConstants.AF_VSOCK, OsConstants.SOCK_STREAM, 0);
|
||||||
|
|
||||||
@@ -73,15 +73,32 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
|
|
||||||
final byte[] handshake = createOpenHandshake();
|
final byte[] handshake = createOpenHandshake();
|
||||||
writeFully(fd, handshake, 0, handshake.length);
|
writeFully(fd, handshake, 0, handshake.length);
|
||||||
mPipe = fd;
|
return fd;
|
||||||
return true;
|
|
||||||
} catch (ErrnoException | SocketException | InterruptedIOException e) {
|
} catch (ErrnoException | SocketException | InterruptedIOException e) {
|
||||||
Os.close(fd);
|
Os.close(fd);
|
||||||
}
|
}
|
||||||
} catch (ErrnoException e) {
|
} catch (ErrnoException e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void openPipe() throws InterruptedException {
|
||||||
|
FileDescriptor fd = getPipeFD();
|
||||||
|
|
||||||
|
if (fd == null) {
|
||||||
|
fd = openPipeImpl();
|
||||||
|
|
||||||
|
// There's no guarantee that QEMU pipes will be ready at the moment
|
||||||
|
// this method is invoked. We simply try to get the pipe open and
|
||||||
|
// retry on failure indefinitely.
|
||||||
|
while (fd == null) {
|
||||||
|
Thread.sleep(100);
|
||||||
|
fd = openPipeImpl();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setPipeFD(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void closePipe() {
|
private synchronized void closePipe() {
|
||||||
@@ -125,12 +142,7 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> {
|
|||||||
this.mHostMonitorThread = new Thread(() -> {
|
this.mHostMonitorThread = new Thread(() -> {
|
||||||
while (!Thread.interrupted()) {
|
while (!Thread.interrupted()) {
|
||||||
try {
|
try {
|
||||||
// There's no guarantee that QEMU pipes will be ready at the moment
|
openPipe();
|
||||||
// this method is invoked. We simply try to get the pipe open and
|
|
||||||
// retry on failure indefinitely.
|
|
||||||
while (!openPipe()) {
|
|
||||||
Thread.sleep(100);
|
|
||||||
}
|
|
||||||
|
|
||||||
final byte[] receivedData = receiveMessage();
|
final byte[] receivedData = receiveMessage();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user