1 #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 
10 namespace android {
11 namespace hardware {
12 namespace tests {
13 namespace multithread {
14 namespace V1_0 {
15 namespace implementation {
16 
17 // Methods from ::android::hardware::tests::multithread::V1_0::IMultithread follow.
setNumThreads(int32_t maxThreads,int32_t numThreads)18 Return<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 
runNewThread()32 Return<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 
HIDL_FETCH_IMultithread(const char *)52 IMultithread* HIDL_FETCH_IMultithread(const char* /* name */) {
53     return new Multithread();
54 }
55 
56 decltype(Multithread::kTimeoutDuration) Multithread::kTimeoutDuration;
57 
58 }  // namespace implementation
59 }  // namespace V1_0
60 }  // namespace multithread
61 }  // namespace tests
62 }  // namespace hardware
63 }  // namespace android
64