OSDN Git Service

NativeDaemonConnector: add waitForCallbacks method
authorRebecca Silberstein <silberst@google.com>
Thu, 21 Apr 2016 19:14:41 +0000 (12:14 -0700)
committerThe Android Automerger <android-build@google.com>
Fri, 22 Apr 2016 22:14:32 +0000 (15:14 -0700)
Add a method that allows callers to wait until all unsolicited
responses received from the native daemon during a command are
processed.

When commands are issued to a native daemon (such as netd) through the
NativeDaemonConnector we block until the command response is received.
Any responses or events that are a side-effect (considered
"unsolicited") of the command are placed in a Message and handled as
callbacks. The order of their processing is not guaranteed and, as we
have seen from bugreports, can be handled several seconds
later - causing the SoftAP that was just set up to be torn down
because a late interface down/removed is indistinguishable from a
new interface down/removed.

This CL adds a method that first checks to make sure callback thread
is not the same thread as used for the blocking call.  The new
waitForCallbacks method uses a CountDownLatch to force the calling
thread to wait until all unsolicited responses received from the
native daemon during the execution of the command are handled.

The wifiFirmwareReload method is also updated to use the new
waitForCallbacks method.

BUG: 27857665
Change-Id: I3e22978f720b1cbf57fbb64ad4fea73f8c2d408a

services/core/java/com/android/server/NativeDaemonConnector.java
services/core/java/com/android/server/NetworkManagementService.java

index 6009984..f5f7732 100644 (file)
@@ -41,6 +41,7 @@ import java.util.ArrayList;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.LinkedList;
 
@@ -344,6 +345,30 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
     }
 
     /**
+     * Method that waits until all asychronous notifications sent by the native daemon have
+     * been processed. This method must not be called on the notification thread or an
+     * exception will be thrown.
+     */
+    public void waitForCallbacks() {
+        if (Thread.currentThread() == mLooper.getThread()) {
+            throw new IllegalStateException("Must not call this method on callback thread");
+        }
+
+        final CountDownLatch latch = new CountDownLatch(1);
+        mCallbackHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                latch.countDown();
+            }
+        });
+        try {
+            latch.await();
+        } catch (InterruptedException e) {
+            Slog.wtf(TAG, "Interrupted while waiting for unsolicited response handling", e);
+        }
+    }
+
+    /**
      * Issue the given command to the native daemon and return a single expected
      * response.
      *
index fffd850..7458898 100644 (file)
@@ -1507,6 +1507,11 @@ public class NetworkManagementService extends INetworkManagementService.Stub
         } catch (NativeDaemonConnectorException e) {
             throw e.rethrowAsParcelableException();
         }
+
+        // Ensure that before we return from this command, any asynchronous
+        // notifications generated before the command completed have been
+        // processed by all NetworkManagementEventObservers.
+        mConnector.waitForCallbacks();
     }
 
     @Override