1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ServiceSingleton"
19
20 #include <mediautils/ServiceSingleton.h>
21
22 namespace android::mediautils {
23
24 namespace details {
25
26 // To prevent multiple instances in different linkages,
27 // we anchor the singleton in a .cpp instead of inlining in the header.
28
29 template<typename T>
30 requires (std::is_same_v<T, const char*> || std::is_same_v<T, String16>)
getInstance(const T & name)31 std::shared_ptr<ServiceHandler> ServiceHandler::getInstance(const T& name) {
32 using Key = std::conditional_t<std::is_same_v<T, String16>, String16, std::string>;
33 [[clang::no_destroy]] static constinit std::mutex mutex;
34 [[clang::no_destroy]] static constinit std::shared_ptr<
35 std::map<Key, std::shared_ptr<ServiceHandler>>> map GUARDED_BY(mutex);
36 static constinit bool init GUARDED_BY(mutex) = false;
37
38 std::lock_guard l(mutex);
39 if (!init) {
40 map = std::make_shared<std::map<Key, std::shared_ptr<ServiceHandler>>>();
41 init = true;
42 }
43
44 auto& handler = (*map)[name];
45 if (!handler) {
46 handler = std::make_shared<ServiceHandler>();
47 if constexpr (std::is_same_v<T, String16>) {
48 handler->init_cpp();
49 } else /* constexpr */ {
50 handler->init_ndk();
51 }
52 }
53 return handler;
54 }
55
56 // Explicit template function instantiation.
57 template
58 std::shared_ptr<ServiceHandler> ServiceHandler::getInstance<const char*>(const char* const& name);
59
60 template
61 std::shared_ptr<ServiceHandler> ServiceHandler::getInstance<String16>(const String16& name);
62
63 } // details
64
65 } // namespace android::mediautils
66
67