xref: /aosp_15_r20/test/dittosuite/src/binder_service.cpp (revision 6fa2df46f119dce7527f5beb2814eca0e6f886ac)
1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #if __ANDROID__
16 
17 #include <ditto/binder_service.h>
18 
19 #include <ditto/binder.h>
20 #include <ditto/logger.h>
21 
22 #include <binder/IPCThreadState.h>
23 #include <binder/IServiceManager.h>
24 #include <binder/ProcessState.h>
25 
26 
27 using android::IPCThreadState;
28 using android::defaultServiceManager;
29 
30 namespace dittosuite {
31 
BinderService(const Params & params,const std::string & name,int64_t threads)32 BinderService::BinderService(const Params& params, const std::string& name, int64_t threads)
33     : Instruction(kName, params), threads_(threads), name_(name) {
34   pthread_mutex_init(&s_work_lock, nullptr);
35   pthread_cond_init(&s_work_cond, nullptr);
36 }
37 
RunSingle()38 void BinderService::RunSingle() {
39   LOGD("Joining thread pool");
40 
41   pthread_mutex_lock(&s_work_lock);
42   pthread_cond_wait(&s_work_cond, &s_work_lock);
43   pthread_mutex_unlock(&s_work_lock);
44 
45   LOGD("Exiting thread pool");
46 }
47 
SetUp()48 void BinderService::SetUp() {
49   Instruction::SetUp();
50   LOGD("Creating Binder service: " + name_);
51 
52   defaultServiceManager()->addService(String16(name_.c_str()), new DittoBinder(&s_work_cond));
53 
54   android::ProcessState::self()->setThreadPoolMaxThreadCount(threads_);
55   android::ProcessState::self()->startThreadPool();
56 
57   LOGD("Demo service is now ready");
58 }
59 
TearDown()60 void BinderService::TearDown() {
61   LOGD("Demo service finished");
62 
63   pthread_cond_destroy(&s_work_cond);
64   pthread_mutex_destroy(&s_work_lock);
65 
66   Instruction::TearDown();
67 }
68 
69 }  // namespace dittosuite
70 
71 #endif
72