1 /*
2 * Copyright (C) 2017 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_TAG "HalDeathHandler"
18 //#define LOG_NDEBUG 0
19
20 #include <utils/Log.h>
21
22 #include <media/audiohal/hidl/HalDeathHandler.h>
23
24 namespace android {
25
26 ANDROID_SINGLETON_STATIC_INSTANCE(HalDeathHandler);
27
28 // static
getInstance()29 sp<HalDeathHandler> HalDeathHandler::getInstance() {
30 return &Singleton<HalDeathHandler>::getInstance();
31 }
32
HalDeathHandler()33 HalDeathHandler::HalDeathHandler() : mSelf(this) {
34 }
35
~HalDeathHandler()36 HalDeathHandler::~HalDeathHandler() {
37 }
38
registerAtExitHandler(void * cookie,AtExitHandler handler)39 void HalDeathHandler::registerAtExitHandler(void* cookie, AtExitHandler handler) {
40 std::lock_guard<std::mutex> guard(mHandlersLock);
41 mHandlers.insert({cookie, handler});
42 }
43
unregisterAtExitHandler(void * cookie)44 void HalDeathHandler::unregisterAtExitHandler(void* cookie) {
45 std::lock_guard<std::mutex> guard(mHandlersLock);
46 mHandlers.erase(cookie);
47 }
48
serviceDied(uint64_t,const wp<IBase> &)49 void HalDeathHandler::serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) {
50 // No matter which of the service objects has died,
51 // we need to run all the registered handlers and exit.
52 std::lock_guard<std::mutex> guard(mHandlersLock);
53 for (const auto& handler : mHandlers) {
54 handler.second();
55 }
56 ALOGE("HAL server crashed, audio server is restarting");
57 _exit(1); // Avoid calling atexit handlers, as this code runs on a thread from RPC threadpool.
58 }
59
60 } // namespace android
61