I have Android java service which will interact with HAL
service using HIDL
calls.
I have below scenario, I'm not sure to treat it as critical.
+----------+ (AIDL) +--------------+
|App thread|-------->|Java Service | (HIDL) +-----------+
+----------+ |(SendFunction)|------->|CPP service|
+--------------+ +-----------+
^
+--------------+ |
|AnotherThread |-----|
+--------------+
Definition of SendFunction
is as below.
private void SendFunction(int status, DiagCommandDesc response) {
try {
server.executeCommandResponse(status, response);
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Response sent to HAL.");
}
} catch (Exception e) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "HAL Server error.");
}
}
}
SendFunction
is being called from two different threads.
Where server
is an instance to CPP Server
using HIDL
.
MY question.
server.executeCommandResponse(status, response);
Do I need to treat above call
as critical and synchronize it? as server
object will be accessed from two different threads.
No, you do not have to guard the call to server.executeCommandResponse(status, response)
in your Java service.
The Binder communication is already thread-safe. That concurrent calls to executeCommandResponse
are safe inside the HAL service has to be ensured by the HAL itself. There is an easy way to make it thread-safe on the HAL side: Using a thread-pool with only a single thread. This will make all other threads wait on the first one to finish, though.
int main()
{
::android::hardware::configureRpcThreadpool(1, true);
::android::sp<MyHal> service = new MyHal;
if (::android::OK != service->registerAsService())
return EXIT_FAILURE;
::android::hardware::joinRpcThreadpool();
return EXIT_SUCCESS;
}
You can find more information here: https://source.android.com/devices/architecture/hidl/threading