Josh Gao | f6d181b | 2016-09-13 14:50:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Gao | f6d181b | 2016-09-13 14:50:57 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace base { |
| 26 | |
Josh Gao | fa761c4 | 2016-09-14 13:54:16 -0700 | [diff] [blame] | 27 | static auto& quick_exit_mutex = *new std::mutex(); |
| 28 | static auto& quick_exit_handlers = *new std::vector<void (*)()>(); |
Josh Gao | f6d181b | 2016-09-13 14:50:57 -0700 | [diff] [blame] | 29 | |
| 30 | void 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 | |
| 38 | int 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 |