Try fetching hwservice before waiting for it.

This reduces thrashing in the common case.

This also has the side effect of registering less service
notification listeners with hwservicemanager. This means even
though death notifications aren't being received (b/32837397 #10),
hwservicemanager will no longer be leaking callback interfaces
because after hwservices start, no more will be registered.

Caveat: waitForHwService requires the current process to have
a hwbinder thread available. This can cause problems if:
- a process only has one hwbinder thread
- this process calls getService on that hwbinder thread
- deadlock occurs since another hwbinder thread won't
  be automatically started.
After this change, this deadlock will no longer occur 100% of the
time.

Test: boot internal marlin
Test: hidl_test
Test: repeatedly killing and restarting camera app no longer leaks
      memory in hwservicemanager.
Bug: 62319810

Merged-In: If005575b097313a1014c0cb77fc078df098a24da
Change-Id: If005575b097313a1014c0cb77fc078df098a24da
diff --git a/generateCpp.cpp b/generateCpp.cpp
index 2ff1c51..2f33217 100644
--- a/generateCpp.cpp
+++ b/generateCpp.cpp
@@ -243,7 +243,8 @@
         // } else {
         //     if (vintfHwbinder) {
         //         while (no alive service) {
-        //             waitForHwService
+        //             if (have already tried to get service)
+        //                 waitForHwService
         //             defaultServiceManager()->get
         //         }
         //     } else if (vintfLegacy) {
@@ -254,20 +255,16 @@
         //     }
         // }
 
-        out << "bool tried = false;\n";
-        out.sWhile("!getStub && (vintfHwbinder || (vintfLegacy && !tried))", [&] {
-
-            out.sIf("tried", [&] {
-                // sleep only after the first trial.
-                out << "ALOGI(\"" << functionName << ": retrying in 1s...\");\n"
-                    << "sleep(1);\n";
-            }).endl();
-
-            out << "tried = true;\n";
-
-
+        out.sFor("int tries = 0; !getStub && (vintfHwbinder || (vintfLegacy && tries == 0)); tries++", [&] {
             if (!isTry) {
-                out.sIf("vintfHwbinder", [&] {
+                out.sIf("tries > 1", [&] {
+                    // sleep only after the first time we've called waitForHwService.
+                    out << "ALOGI(\"" << functionName << ": Will do try %d for %s/%s in 1s...\", tries, "
+                        << interfaceName << "::descriptor, serviceName.c_str());\n"
+                        << "sleep(1);\n";
+                }).endl();
+
+                out.sIf("vintfHwbinder && tries > 0", [&] {
                     out << "waitForHwService("
                         << interfaceName << "::descriptor, serviceName);\n";
                 }).endl();
@@ -287,10 +284,12 @@
 
             out << "sp<" << gIBaseFqName.cppName() << "> base = ret;\n";
             out.sIf("base == nullptr", [&] {
-                // race condition. hwservicemanager drops the service
+                // if tries > 0: race condition. hwservicemanager drops the service
                 // from waitForHwService to here
-                out << "ALOGW(\"" << interfaceName << ": found null hwbinder interface\");\n"
-                    << (isTry ? "break" : "continue")
+                out.sIf("tries > 0", [&] {
+                    out << "ALOGW(\"" << interfaceName << ": found null hwbinder interface\");\n";
+                });
+                out << (isTry ? "break" : "continue")
                     << ";\n";
             }).endl();
             out << "Return<sp<" << interfaceName