blob: 6e335230974dc6c5890e722667a2b032ee53730d [file] [log] [blame]
Mikhail Naganovd621ac82017-01-12 17:17:45 -08001/*
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
24namespace android {
25
26ANDROID_SINGLETON_STATIC_INSTANCE(HalDeathHandler);
27
28// static
29sp<HalDeathHandler> HalDeathHandler::getInstance() {
30 return &Singleton<HalDeathHandler>::getInstance();
31}
32
33HalDeathHandler::HalDeathHandler() : mSelf(this) {
34}
35
36HalDeathHandler::~HalDeathHandler() {
37}
38
39void HalDeathHandler::registerAtExitHandler(void* cookie, AtExitHandler handler) {
40 std::lock_guard<std::mutex> guard(mHandlersLock);
41 mHandlers.insert({cookie, handler});
42}
43
44void HalDeathHandler::unregisterAtExitHandler(void* cookie) {
45 std::lock_guard<std::mutex> guard(mHandlersLock);
46 mHandlers.erase(cookie);
47}
48
49void HalDeathHandler::serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) {
50 // No matter which of the service objects has died,
Mikhail Naganov7f16ea42017-11-16 08:29:49 -080051 // we need to run all the registered handlers and exit.
Mikhail Naganovd621ac82017-01-12 17:17:45 -080052 std::lock_guard<std::mutex> guard(mHandlersLock);
53 for (const auto& handler : mHandlers) {
54 handler.second();
55 }
Mikhail Naganov7f16ea42017-11-16 08:29:49 -080056 ALOGE("HAL server crashed, audio server is restarting");
Mikhail Naganov5beb4812018-12-14 10:48:17 -080057 _exit(1); // Avoid calling atexit handlers, as this code runs on a thread from RPC threadpool.
Mikhail Naganovd621ac82017-01-12 17:17:45 -080058}
59
60} // namespace android