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 | |
| 24 | #include "android-base/mutex.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace base { |
| 28 | |
| 29 | static std::mutex quick_exit_mutex; |
| 30 | static std::vector<void(*)()> quick_exit_handlers; |
| 31 | |
| 32 | void 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 | |
| 40 | int 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 |