blob: fa671224e4d7315ac0855eb3d24067f89eb82510 [file] [log] [blame]
Josh Gao0c442562016-09-13 14:50:57 -07001/*
2 * Copyright (C) 2016 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#include "android-base/quick_exit.h"
18
19#if !defined(__linux__)
20
21#include <mutex>
22#include <vector>
23
24#include "android-base/mutex.h"
25
26namespace android {
27namespace base {
28
Josh Gaoc95afaa2016-09-14 13:54:16 -070029static auto& quick_exit_mutex = *new std::mutex();
30static auto& quick_exit_handlers = *new std::vector<void (*)()>();
Josh Gao0c442562016-09-13 14:50:57 -070031
32void quick_exit(int exit_code) {
33 std::lock_guard<std::mutex> lock(quick_exit_mutex);
34 for (auto it = quick_exit_handlers.rbegin(); it != quick_exit_handlers.rend(); ++it) {
35 (*it)();
36 }
37 _Exit(exit_code);
38}
39
40int at_quick_exit(void (*func)()) {
41 std::lock_guard<std::mutex> lock(quick_exit_mutex);
42 quick_exit_handlers.push_back(func);
43 return 0;
44}
45
46} // namespace base
47} // namespace android
48#endif