blob: 1fb267c03c55ab5bbbd44d654526dad4bfbb7c54 [file] [log] [blame]
Timur Iskhakov16a04372017-07-07 13:28:56 -07001#define LOG_TAG "hidl_test"
2
3#include <android-base/logging.h>
4#include "Multithread.h"
5#include <inttypes.h>
6#include <thread>
7
8#include <hidl/HidlTransportSupport.h>
9
10namespace android {
11namespace hardware {
12namespace tests {
13namespace multithread {
14namespace V1_0 {
15namespace implementation {
16
17// Methods from ::android::hardware::tests::multithread::V1_0::IMultithread follow.
18Return<void> Multithread::setNumThreads(int32_t maxThreads, int32_t numThreads) {
19 LOG(INFO) << "SERVER(Multithread) setNumThreads("
20 << maxThreads << ", " << numThreads << ")";
21
22 LOG(INFO) << "SERVER(Multithread) call configureRpcThreadpool("
23 << maxThreads << ")";
24 ::android::hardware::configureRpcThreadpool(maxThreads, /*willjoin*/ false);
25
26 mNumThreads = numThreads;
27 mNoTimeout = true;
28
29 return Void();
30}
31
32Return<bool> Multithread::runNewThread() {
33 LOG(INFO) << "SERVER(Multithread) runNewThread()";
34
35 std::unique_lock<std::mutex> lk(mCvMutex);
36 --mNumThreads;
37
38 LOG(INFO) << "SERVER(Multithread) runNewThread()";
39 LOG(INFO) << mNumThreads << "threads left";
40
41 mCv.notify_all();
42 bool noTimeout = mCv.wait_for(lk, kTimeoutDuration,
43 [&] { return mNumThreads <= 0 || !mNoTimeout; });
44
45 if (!noTimeout) {
46 mNoTimeout = false;
47 mCv.notify_all();
48 }
49 return mNoTimeout;
50}
51
52IMultithread* HIDL_FETCH_IMultithread(const char* /* name */) {
53 return new Multithread();
54}
55
56decltype(Multithread::kTimeoutDuration) Multithread::kTimeoutDuration;
57
58} // namespace implementation
59} // namespace V1_0
60} // namespace multithread
61} // namespace tests
62} // namespace hardware
63} // namespace android