diff options
author | Roman Kiryanov <rkir@google.com> | 2022-05-11 21:57:43 -0700 |
---|---|---|
committer | Roman Kiryanov <rkir@google.com> | 2022-05-11 22:37:44 -0700 |
commit | a1e0480b6400cf8c758f6035bcab351ddf7a67f4 (patch) | |
tree | 38492578b70f79024ce068b48c2becc926eabe2d | |
parent | 63596aaf5e975217593e56895adfa2b0ba59af12 (diff) | |
download | base-a1e0480b6400cf8c758f6035bcab351ddf7a67f4.tar.gz |
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
-rw-r--r-- | services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java b/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java index a4084ed332cf..44822a3f7429 100644 --- a/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java +++ b/services/core/java/com/android/server/clipboard/EmulatorClipboardMonitor.java @@ -60,11 +60,11 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> { return mPipe; } - private synchronized boolean openPipe() { - if (mPipe != null) { - return true; - } + private synchronized void setPipeFD(final FileDescriptor fd) { + mPipe = fd; + } + private static FileDescriptor openPipeImpl() { try { 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(); writeFully(fd, handshake, 0, handshake.length); - mPipe = fd; - return true; + return fd; } catch (ErrnoException | SocketException | InterruptedIOException e) { Os.close(fd); } } 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() { @@ -125,12 +142,7 @@ class EmulatorClipboardMonitor implements Consumer<ClipData> { this.mHostMonitorThread = new Thread(() -> { while (!Thread.interrupted()) { try { - // 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 (!openPipe()) { - Thread.sleep(100); - } + openPipe(); final byte[] receivedData = receiveMessage(); |