blob: e4dd62bfb531f67ab041bcfd27f83a21784c6c22 [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
Josh Gao0c442562016-09-13 14:50:57 -070024namespace android {
25namespace base {
26
Josh Gaoc95afaa2016-09-14 13:54:16 -070027static auto& quick_exit_mutex = *new std::mutex();
28static auto& quick_exit_handlers = *new std::vector<void (*)()>();
Josh Gao0c442562016-09-13 14:50:57 -070029
30void quick_exit(int exit_code) {
31 std::lock_guard<std::mutex> lock(quick_exit_mutex);
32 for (auto it = quick_exit_handlers.rbegin(); it != quick_exit_handlers.rend(); ++it) {
33 (*it)();
34 }
35 _Exit(exit_code);
36}
37
38int at_quick_exit(void (*func)()) {
39 std::lock_guard<std::mutex> lock(quick_exit_mutex);
40 quick_exit_handlers.push_back(func);
41 return 0;
42}
43
44} // namespace base
45} // namespace android
46#endif