1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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
16 #include "object_collector.h"
17 #include <iremote_object.h>
18 #include <thread>
19 #include "hdf_base.h"
20 #include "hdf_log.h"
21 #include "osal_time.h"
22
23 using namespace OHOS::HDI;
24
25 ObjectCollector *ObjectCollector::instance_ = new ObjectCollector();
26
GetInstance()27 ObjectCollector &ObjectCollector::GetInstance()
28 {
29 return *instance_;
30 }
31
ConstructorRegister(const std::u16string & interfaceName,const Constructor & constructor)32 bool ObjectCollector::ConstructorRegister(const std::u16string &interfaceName, const Constructor &constructor)
33 {
34 if (interfaceName.empty()) {
35 return false;
36 }
37 std::lock_guard<std::mutex> lock(mutex_);
38 constructorMapper_.emplace(interfaceName, std::move(constructor));
39 return true;
40 }
41
ConstructorUnRegister(const std::u16string & interfaceName)42 void ObjectCollector::ConstructorUnRegister(const std::u16string &interfaceName)
43 {
44 std::lock_guard<std::mutex> lock(mutex_);
45 constructorMapper_.erase(interfaceName);
46 }
47
NewObjectLocked(const OHOS::sptr<HdiBase> & interface,const std::u16string & interfaceName)48 OHOS::sptr<OHOS::IRemoteObject> ObjectCollector::NewObjectLocked(
49 const OHOS::sptr<HdiBase> &interface, const std::u16string &interfaceName)
50 {
51 if (interface == nullptr) {
52 return nullptr;
53 }
54 auto constructor = constructorMapper_.find(interfaceName);
55 if (constructor != constructorMapper_.end()) {
56 return constructor->second(interface);
57 }
58
59 return nullptr;
60 }
61
NewObject(const OHOS::sptr<HdiBase> & interface,const std::u16string & interfaceName)62 OHOS::sptr<OHOS::IRemoteObject> ObjectCollector::NewObject(
63 const OHOS::sptr<HdiBase> &interface, const std::u16string &interfaceName)
64 {
65 std::lock_guard<std::mutex> lock(mutex_);
66 return NewObjectLocked(interface, interfaceName);
67 }
68
GetOrNewObject(const OHOS::sptr<HdiBase> & interface,const std::u16string & interfaceName)69 OHOS::sptr<OHOS::IRemoteObject> ObjectCollector::GetOrNewObject(
70 const OHOS::sptr<HdiBase> &interface, const std::u16string &interfaceName)
71 {
72 if (interface == nullptr) {
73 return nullptr;
74 }
75
76 RETRY:
77 std::unique_lock<std::mutex> lock(mutex_);
78 auto it = interfaceObjectCollector_.find(interface.GetRefPtr());
79 if (it != interfaceObjectCollector_.end()) {
80 if (it->second->GetSptrRefCount() == 0) {
81 // may object is releasing, yield to sync
82 lock.unlock();
83 std::this_thread::yield();
84 goto RETRY;
85 }
86 return it->second.GetRefPtr();
87 }
88 sptr<IRemoteObject> object = NewObjectLocked(interface, interfaceName);
89 interfaceObjectCollector_[interface.GetRefPtr()] = object;
90 return object;
91 }
92
RemoveObject(const OHOS::sptr<HdiBase> & interface)93 bool ObjectCollector::RemoveObject(const OHOS::sptr<HdiBase> &interface)
94 {
95 std::lock_guard<std::mutex> lock(mutex_);
96 auto it = interfaceObjectCollector_.find(interface.GetRefPtr());
97 if (it == interfaceObjectCollector_.end()) {
98 return false;
99 }
100 interfaceObjectCollector_.erase(it);
101 return true;
102 }
103