Add synchronization between IRadio callbacks and service creation.

Radio indications are sent by vendor ril on a separate thread and
can be received even before radio service is created and registered.
This is to avoid that race and make sure indication callbacks are
accessed only after service is created.

Test: Basic telephony sanity
Bug: 34363807
Change-Id: Iab11add5ca458476575c8b52d5e5878ff2b1d175
diff --git a/libril/ril.cpp b/libril/ril.cpp
index 865c3f9..96ca84c 100644
--- a/libril/ril.cpp
+++ b/libril/ril.cpp
@@ -4888,7 +4888,16 @@
             case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION:
             case RIL_REQUEST_GET_CURRENT_CALLS:
             case RIL_REQUEST_DIAL:
+                pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock(
+                        (int) socket_id);
+                int rwlockRet = pthread_rwlock_rdlock(radioServiceRwlockPtr);
+                assert(rwlockRet == 0);
+
                 radio::acknowledgeRequest((int) socket_id, pRI->token);
+
+                rwlockRet = pthread_rwlock_unlock(radioServiceRwlockPtr);
+                assert(rwlockRet == 0);
+
                 return;
         }
 
@@ -4969,9 +4978,17 @@
         if (response != NULL || hidlized) {
             // there is a response payload, no matter success or not.
             RLOGE ("Calling responseFunction() for token %d", pRI->token);
+
+            pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock((int) socket_id);
+            int rwlockRet = pthread_rwlock_rdlock(radioServiceRwlockPtr);
+            assert(rwlockRet == 0);
+
             ret = pRI->pCI->responseFunction(p, (int) socket_id, pRI->pCI->requestNumber,
                     responseType, pRI->token, e, response, responselen);
 
+            rwlockRet = pthread_rwlock_unlock(radioServiceRwlockPtr);
+            assert(rwlockRet == 0);
+
             if (hidlized)  {
                 free(pRI);
                 return;
@@ -5135,9 +5152,17 @@
         responseType = RESPONSE_UNSOLICITED;
     }
 
+    pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock((int) soc_id);
+    int rwlockRet = pthread_rwlock_rdlock(radioServiceRwlockPtr);
+    assert(rwlockRet == 0);
+
     ret = s_unsolResponses[unsolResponseIndex].responseFunction(
             p, (int) soc_id, unsolResponse, responseType, 0, RIL_E_SUCCESS, const_cast<void*>(data),
             datalen);
+
+    rwlockRet = pthread_rwlock_unlock(radioServiceRwlockPtr);
+    assert(rwlockRet == 0);
+
     if (ret != 0) {
         // Problem with the response. Don't continue;
         goto error_exit;
@@ -5175,7 +5200,13 @@
     ret = 0;
     switch (unsolResponse) {
         case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
+            rwlockRet = pthread_rwlock_rdlock(radioServiceRwlockPtr);
+            assert(rwlockRet == 0);
+
             radio::radioStateChangedInd(soc_id, responseType, newState);
+
+            rwlockRet = pthread_rwlock_unlock(radioServiceRwlockPtr);
+            assert(rwlockRet == 0);
             break;
     }