2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
AIDL による高速メッセージ キュー
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Android 12 以降、NDK バックエンドを使用して AIDL で高速メッセージ キューを使用できます。高速メッセージ キューを使用すると、簡単なセットアップの後、バインダー トランザクションのオーバーヘッドや制限なく、プロセス間で通信を行えるようになります。安定版 AIDL を使用すると、システム プロセスとベンダー プロセス間の通信が可能になります。
サポートされているペイロードの型
共有メモリ メッセージ キュー内のプロセス間で送信されるメッセージの場合、プロセスの境界にかかわらずメモリ レイアウトを同一にする必要があり、ポインタを設定することはできません。サポートされていない型で AidlMessageQueue
を作成しようとすると、コンパイル エラーとなります。
サポートされているキューの型
AIDL でサポートされているキューの型(フレーバー)は、HIDL と同じです。これらのキューの型は、キューと記述子のテンプレート引数として使用されます。
HIDL の型 |
AIDL の型 |
android::hardware::kSynchronizedReadWrite |
android.hardware.common.fmq.SynchronizedReadWrite |
android::hardware::kUnsynchronizedWrite |
android.hardware.common.fmq.UnsynchronizedWrite |
使用方法
MQDescriptor
を他のプロセスに渡す AIDL インターフェースを定義します。MQDescriptor
は、Parcelable を使用できる場所であればどこでも使用できます。
MQDescriptor
に必要なテンプレート引数は、ペイロードの型とキューのフレーバーです。
import android.hardware.common.fmq.MQDescriptor
import android.hardware.common.fmq.SynchronizedReadWrite
void getQueue(out MQDescriptor<int, SynchronizedReadWrite> mqDesc);
メッセージ キューの各サイドを設定するプロセスは、HIDL を使用するプロセスとほぼ同じで、AIDL の型を使用するだけです。
#include <fmq/AidlMessageQueue.h>
...
using ::android::AidlMessageQueue;
using ::aidl::android::hardware::common::fmq::MQDescriptor;
using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
...
ndk::ScopedAStatus MyInterface::getQueue(MQDescriptor<int32_t, SynchronizedReadWrite>* mqDesc) {
*mqDesc = mFmqSynchronized->dupeDesc();
return ndk::ScopedAStatus::ok();
}
...
// Create the first side of the queue before servicing getQueue() in this example
mFmqSynchronized =
new AidlMessageQueue<int32_t, SynchronizedReadWrite>(kNumElementsInQueue);
受信側のプロセスが、AIDL インターフェースから受け取った記述子を使ってキューのもう一方のサイドを作成します。
MQDescriptor<int32_t, SynchronizedReadWrite> desc;
auto ret = service->getQueue(true, &desc);
if (!ret.isOk()) {
...
}
// By default the constructor will reset the read and write pointers of the queue.
// Add a second `false` argument to avoid resetting the pointers.
mQueue = new (std::nothrow) AidlMessageQueue<int32_t, SynchronizedReadWrite>(desc);
if (!mQueue->isValid()) {
...
}
設定後の AidlMessageQueue
の使い方は、HIDL MessageQueue
の場合と同じです。MessageQueue の使用に記載されているすべての API は、AidlMessageQueue
で完全にサポートされますが、下記の例外が 1 つあります。
const MQDescriptor<T, flavor>* getDesc()
は、AIDL MQDescriptor
を返す MQDescriptor<T, U> dupeDesc()
に置き換えられます。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-03-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-03-26 UTC。"],[],[],null,["# Fast Message Queue with AIDL\n\nAs of Android 12, [Fast Message Queue](/docs/core/architecture/hidl/fmq) can be\nused with AIDL interfaces using the NDK backend. This allows processes to\ncommunicate without the overhead and restrictions of binder transactions after\nsome short setup. Using [Stable AIDL](/docs/core/architecture/aidl/stable-aidl) allows communication between system and\nvendor processes.\n\nSupported payload types\n-----------------------\n\n- [@FixedSize](/docs/core/architecture/aidl/aidl-annotations#fixedsize) AIDL `parcelable` types\n- AIDL `enum` types\n- [AIDL integral types](/docs/core/architecture/aidl/aidl-backends#types)\n\nThe messages sent between processes in the shared memory message queue must have the same\nmemory layout across process boundaries and cannot contain pointers. Attempting to create an\n`AidlMessageQueue` with a type that isn't supported will cause a compilation error.\n\nSupported queue types\n---------------------\n\nThe same [queue types](/docs/core/architecture/hidl/fmq#flavors) from HIDL, often\ncalled flavors, are supported with AIDL. These are used as template arguments for\nthe queues and descriptors.\n\n| HIDL Types | AIDL Types |\n|---------------------------------------------|-----------------------------------------------------|\n| `android::hardware::kSynchronizedReadWrite` | `android.hardware.common.fmq.SynchronizedReadWrite` |\n| `android::hardware::kUnsynchronizedWrite` | `android.hardware.common.fmq.UnsynchronizedWrite` |\n\nHow to use\n----------\n\nDefine the AIDL interface that will pass the `MQDescriptor` to the other process.\n`MQDescriptor` can be used anywhere a parcelable can be.\n\nThe required template arguments for `MQDescriptor` are payload type and queue flavor. \n\n import android.hardware.common.fmq.MQDescriptor\n import android.hardware.common.fmq.SynchronizedReadWrite\n\n void getQueue(out MQDescriptor\u003cint, SynchronizedReadWrite\u003e mqDesc);\n\nThe process of setting up each side of the message queue is nearly identical to\nthe [process using HIDL](/docs/core/architecture/hidl/fmq#setup), just using the AIDL types. \n\n #include \u003cfmq/AidlMessageQueue.h\u003e\n ...\n using ::android::AidlMessageQueue;\n using ::aidl::android::hardware::common::fmq::MQDescriptor;\n using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;\n ...\n ndk::ScopedAStatus MyInterface::getQueue(MQDescriptor\u003cint32_t, SynchronizedReadWrite\u003e* mqDesc) {\n *mqDesc = mFmqSynchronized-\u003edupeDesc();\n return ndk::ScopedAStatus::ok();\n }\n ...\n // Create the first side of the queue before servicing getQueue() in this example\n mFmqSynchronized =\n new AidlMessageQueue\u003cint32_t, SynchronizedReadWrite\u003e(kNumElementsInQueue);\n\nThe receiving process will create the other side of the queue with the descriptor received from the AIDL interface. \n\n MQDescriptor\u003cint32_t, SynchronizedReadWrite\u003e desc;\n auto ret = service-\u003egetQueue(true, &desc);\n if (!ret.isOk()) {\n ...\n }\n // By default the constructor will reset the read and write pointers of the queue.\n // Add a second `false` argument to avoid resetting the pointers.\n mQueue = new (std::nothrow) AidlMessageQueue\u003cint32_t, SynchronizedReadWrite\u003e(desc);\n if (!mQueue-\u003eisValid()) {\n ...\n }\n\nUsing the `AidlMessageQueue` after setup is the same as the HIDL `MessageQueue`.\nAll of the APIs described at [Using the MessageQueue](/docs/core/architecture/hidl/fmq#using)\nare fully supported with `AidlMessageQueue` with one exception:\n\n`const MQDescriptor\u003cT, flavor\u003e* getDesc()` is replaced by `MQDescriptor\u003cT, U\u003e dupeDesc()`\nwhich returns the AIDL `MQDescriptor`."]]