blob: c9b708468f0d80ac9efb85fe53255e15c9e3df55 [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#ifndef ANDROID_HARDWARE_HIDL_HAL_DEATH_HANDLER_H
18#define ANDROID_HARDWARE_HIDL_HAL_DEATH_HANDLER_H
19
20#include <functional>
21#include <mutex>
22#include <unordered_map>
23
24#include <hidl/HidlSupport.h>
25#include <utils/Singleton.h>
26
27using android::hardware::hidl_death_recipient;
28using android::hidl::base::V1_0::IBase;
29
30namespace android {
31
32class HalDeathHandler : public hidl_death_recipient, private Singleton<HalDeathHandler> {
33 public:
34 typedef std::function<void()> AtExitHandler;
35
36 // Note that the exit handler gets called using a thread from
37 // RPC threadpool, thus it needs to be thread-safe.
38 void registerAtExitHandler(void* cookie, AtExitHandler handler);
39 void unregisterAtExitHandler(void* cookie);
40
41 // hidl_death_recipient
42 virtual void serviceDied(uint64_t cookie, const wp<IBase>& who);
43
44 // Used both for (un)registering handlers, and for passing to
45 // '(un)linkToDeath'.
46 static sp<HalDeathHandler> getInstance();
47
48 private:
49 friend class Singleton<HalDeathHandler>;
50 typedef std::unordered_map<void*, AtExitHandler> Handlers;
51
52 HalDeathHandler();
53 virtual ~HalDeathHandler();
54
55 sp<HalDeathHandler> mSelf; // Allows the singleton instance to live forever.
56 std::mutex mHandlersLock;
57 Handlers mHandlers;
58};
59
60} // namespace android
61
62#endif // ANDROID_HARDWARE_HIDL_HAL_DEATH_HANDLER_H