diff --git a/tests/Android.bp b/tests/Android.bp index ddf300b8a8..9583bfda91 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -20,6 +20,8 @@ subdirs = [ "memory/1.0/default", "msgq/1.0", "msgq/1.0/default", + "multithread/1.0", + "multithread/1.0/default", "pointer/1.0", "pointer/1.0/default", "pointer/1.0/default/lib", diff --git a/tests/multithread/1.0/Android.bp b/tests/multithread/1.0/Android.bp new file mode 100644 index 0000000000..c3782a0d6d --- /dev/null +++ b/tests/multithread/1.0/Android.bp @@ -0,0 +1,59 @@ +// This file is autogenerated by hidl-gen. Do not edit manually. + +filegroup { + name: "android.hardware.tests.multithread@1.0_hal", + srcs: [ + "IMultithread.hal", + ], +} + +genrule { + name: "android.hardware.tests.multithread@1.0_genc++", + tools: ["hidl-gen"], + cmd: "$(location hidl-gen) -o $(genDir) -Lc++-sources -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport android.hardware.tests.multithread@1.0", + srcs: [ + ":android.hardware.tests.multithread@1.0_hal", + ], + out: [ + "android/hardware/tests/multithread/1.0/MultithreadAll.cpp", + ], +} + +genrule { + name: "android.hardware.tests.multithread@1.0_genc++_headers", + tools: ["hidl-gen"], + cmd: "$(location hidl-gen) -o $(genDir) -Lc++-headers -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport android.hardware.tests.multithread@1.0", + srcs: [ + ":android.hardware.tests.multithread@1.0_hal", + ], + out: [ + "android/hardware/tests/multithread/1.0/IMultithread.h", + "android/hardware/tests/multithread/1.0/IHwMultithread.h", + "android/hardware/tests/multithread/1.0/BnHwMultithread.h", + "android/hardware/tests/multithread/1.0/BpHwMultithread.h", + "android/hardware/tests/multithread/1.0/BsMultithread.h", + ], +} + +cc_library_shared { + name: "android.hardware.tests.multithread@1.0", + defaults: ["hidl-module-defaults"], + generated_sources: ["android.hardware.tests.multithread@1.0_genc++"], + generated_headers: ["android.hardware.tests.multithread@1.0_genc++_headers"], + export_generated_headers: ["android.hardware.tests.multithread@1.0_genc++_headers"], + vendor_available: true, + shared_libs: [ + "libhidlbase", + "libhidltransport", + "libhwbinder", + "liblog", + "libutils", + "libcutils", + ], + export_shared_lib_headers: [ + "libhidlbase", + "libhidltransport", + "libhwbinder", + "libutils", + ], +} diff --git a/tests/multithread/1.0/Android.mk b/tests/multithread/1.0/Android.mk new file mode 100644 index 0000000000..f63381d24c --- /dev/null +++ b/tests/multithread/1.0/Android.mk @@ -0,0 +1,76 @@ +# This file is autogenerated by hidl-gen. Do not edit manually. + +LOCAL_PATH := $(call my-dir) + +################################################################################ + +include $(CLEAR_VARS) +LOCAL_MODULE := android.hardware.tests.multithread-V1.0-java +LOCAL_MODULE_CLASS := JAVA_LIBRARIES + +intermediates := $(call local-generated-sources-dir, COMMON) + +HIDL := $(HOST_OUT_EXECUTABLES)/hidl-gen$(HOST_EXECUTABLE_SUFFIX) + +LOCAL_JAVA_LIBRARIES := \ + android.hidl.base-V1.0-java \ + + +# +# Build IMultithread.hal +# +GEN := $(intermediates)/android/hardware/tests/multithread/V1_0/IMultithread.java +$(GEN): $(HIDL) +$(GEN): PRIVATE_HIDL := $(HIDL) +$(GEN): PRIVATE_DEPS := $(LOCAL_PATH)/IMultithread.hal +$(GEN): PRIVATE_OUTPUT_DIR := $(intermediates) +$(GEN): PRIVATE_CUSTOM_TOOL = \ + $(PRIVATE_HIDL) -o $(PRIVATE_OUTPUT_DIR) \ + -Ljava \ + -randroid.hardware:hardware/interfaces \ + -randroid.hidl:system/libhidl/transport \ + android.hardware.tests.multithread@1.0::IMultithread + +$(GEN): $(LOCAL_PATH)/IMultithread.hal + $(transform-generated-source) +LOCAL_GENERATED_SOURCES += $(GEN) +include $(BUILD_JAVA_LIBRARY) + + +################################################################################ + +include $(CLEAR_VARS) +LOCAL_MODULE := android.hardware.tests.multithread-V1.0-java-static +LOCAL_MODULE_CLASS := JAVA_LIBRARIES + +intermediates := $(call local-generated-sources-dir, COMMON) + +HIDL := $(HOST_OUT_EXECUTABLES)/hidl-gen$(HOST_EXECUTABLE_SUFFIX) + +LOCAL_STATIC_JAVA_LIBRARIES := \ + android.hidl.base-V1.0-java-static \ + + +# +# Build IMultithread.hal +# +GEN := $(intermediates)/android/hardware/tests/multithread/V1_0/IMultithread.java +$(GEN): $(HIDL) +$(GEN): PRIVATE_HIDL := $(HIDL) +$(GEN): PRIVATE_DEPS := $(LOCAL_PATH)/IMultithread.hal +$(GEN): PRIVATE_OUTPUT_DIR := $(intermediates) +$(GEN): PRIVATE_CUSTOM_TOOL = \ + $(PRIVATE_HIDL) -o $(PRIVATE_OUTPUT_DIR) \ + -Ljava \ + -randroid.hardware:hardware/interfaces \ + -randroid.hidl:system/libhidl/transport \ + android.hardware.tests.multithread@1.0::IMultithread + +$(GEN): $(LOCAL_PATH)/IMultithread.hal + $(transform-generated-source) +LOCAL_GENERATED_SOURCES += $(GEN) +include $(BUILD_STATIC_JAVA_LIBRARY) + + + +include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/tests/multithread/1.0/IMultithread.hal b/tests/multithread/1.0/IMultithread.hal new file mode 100644 index 0000000000..19dc4f6810 --- /dev/null +++ b/tests/multithread/1.0/IMultithread.hal @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.tests.multithread@1.0; + +/** + * IMultithread tests configureRpcThreadpool + * + * Makes calls finish only when specific number of them are running in parallel + */ +interface IMultithread { + /** + * Sets up controller + * Must be called for each test + * @param maxThreads arg passed to configureRpcThreadpool + * Could not be decremented by future calls + * @param numThreads number of calls in parallel to finish + */ + setNumThreads(int32_t maxThreads, int32_t numThreads); + + /** + * Makes a new call + * @return noTimeout numThreads appeared in parallel before timeout + * Shall return the same value for one setNumThreads call + */ + runNewThread() generates (bool noTimeout); +}; diff --git a/tests/multithread/1.0/default/Android.bp b/tests/multithread/1.0/default/Android.bp new file mode 100644 index 0000000000..72b9e91da6 --- /dev/null +++ b/tests/multithread/1.0/default/Android.bp @@ -0,0 +1,17 @@ +cc_library_shared { + name: "android.hardware.tests.multithread@1.0-impl", + defaults: ["hidl_defaults"], + relative_install_path: "hw", + proprietary: true, + srcs: [ + "Multithread.cpp", + ], + shared_libs: [ + "libbase", + "libhidlbase", + "libhidltransport", + "liblog", + "libutils", + "android.hardware.tests.multithread@1.0", + ], +} diff --git a/tests/multithread/1.0/default/Multithread.cpp b/tests/multithread/1.0/default/Multithread.cpp new file mode 100644 index 0000000000..1fb267c03c --- /dev/null +++ b/tests/multithread/1.0/default/Multithread.cpp @@ -0,0 +1,63 @@ +#define LOG_TAG "hidl_test" + +#include +#include "Multithread.h" +#include +#include + +#include + +namespace android { +namespace hardware { +namespace tests { +namespace multithread { +namespace V1_0 { +namespace implementation { + +// Methods from ::android::hardware::tests::multithread::V1_0::IMultithread follow. +Return Multithread::setNumThreads(int32_t maxThreads, int32_t numThreads) { + LOG(INFO) << "SERVER(Multithread) setNumThreads(" + << maxThreads << ", " << numThreads << ")"; + + LOG(INFO) << "SERVER(Multithread) call configureRpcThreadpool(" + << maxThreads << ")"; + ::android::hardware::configureRpcThreadpool(maxThreads, /*willjoin*/ false); + + mNumThreads = numThreads; + mNoTimeout = true; + + return Void(); +} + +Return Multithread::runNewThread() { + LOG(INFO) << "SERVER(Multithread) runNewThread()"; + + std::unique_lock lk(mCvMutex); + --mNumThreads; + + LOG(INFO) << "SERVER(Multithread) runNewThread()"; + LOG(INFO) << mNumThreads << "threads left"; + + mCv.notify_all(); + bool noTimeout = mCv.wait_for(lk, kTimeoutDuration, + [&] { return mNumThreads <= 0 || !mNoTimeout; }); + + if (!noTimeout) { + mNoTimeout = false; + mCv.notify_all(); + } + return mNoTimeout; +} + +IMultithread* HIDL_FETCH_IMultithread(const char* /* name */) { + return new Multithread(); +} + +decltype(Multithread::kTimeoutDuration) Multithread::kTimeoutDuration; + +} // namespace implementation +} // namespace V1_0 +} // namespace multithread +} // namespace tests +} // namespace hardware +} // namespace android diff --git a/tests/multithread/1.0/default/Multithread.h b/tests/multithread/1.0/default/Multithread.h new file mode 100644 index 0000000000..0d4a007833 --- /dev/null +++ b/tests/multithread/1.0/default/Multithread.h @@ -0,0 +1,48 @@ +#ifndef ANDROID_HARDWARE_TESTS_MULTITHREAD_V1_0_MULTITHREAD_H +#define ANDROID_HARDWARE_TESTS_MULTITHREAD_V1_0_MULTITHREAD_H + +#include +#include + +#include +#include +#include + +namespace android { +namespace hardware { +namespace tests { +namespace multithread { +namespace V1_0 { +namespace implementation { + +using ::android::hardware::tests::multithread::V1_0::IMultithread; +using ::android::hardware::Return; +using ::android::hardware::Void; + +using namespace std::chrono_literals; + +struct Multithread : public IMultithread { + // Methods from ::android::hardware::tests::multithread::V1_0::IMultithread follow. + virtual Return setNumThreads(int32_t maxThreads, int32_t numThreads) override; + virtual Return runNewThread() override; + + private: + int32_t mNumThreads; + bool mNoTimeout; + + std::condition_variable mCv; + std::mutex mCvMutex; + + static constexpr auto kTimeoutDuration = 100ms; +}; + +extern "C" IMultithread* HIDL_FETCH_IMultithread(const char* name); + +} // namespace implementation +} // namespace V1_0 +} // namespace multithread +} // namespace tests +} // namespace hardware +} // namespace android + +#endif // ANDROID_HARDWARE_TESTS_MULTITHREAD_V1_0_MULTITHREAD_H