2

我有 Android java 服务,它将HAL使用HIDL调用与服务交互。

我有以下情况,我不确定将其视为关键。

+----------+  (AIDL) +--------------+
|App thread|-------->|Java Service  | (HIDL) +-----------+
+----------+         |(SendFunction)|------->|CPP service|  
                     +--------------+        +-----------+
                     ^
+--------------+     |
|AnotherThread |-----|
+--------------+

的定义SendFunction如下。

 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正在从两个不同的线程调用。使用server的实例在哪里。CPP ServerHIDL

我的问题。

      server.executeCommandResponse(status, response);

我是否需要将上述call视为关键并同步它?asserver对象将从两个不同的线程访问。

4

1 回答 1

2

不,您不必保护server.executeCommandResponse(status, response)Java 服务中的调用。

Binder 通信已经是线程安全的。HAL 服务内部的并发调用executeCommandResponse是安全的,必须由 HAL 本身来确保。有一种简单的方法可以使其在 HAL 端成为线程安全的:使用只有一个线程的线程池。不过,这将使所有其他线程等待第一个线程完成。

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;
}

您可以在此处找到更多信息:https ://source.android.com/devices/architecture/hidl/threading

于 2020-01-14T10:07:48.387 回答