Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 1 | /* |
| 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 | #include "runtime_callbacks.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 21 | #include "base/macros.h" |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 22 | #include "class_linker.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 23 | #include "thread.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | void RuntimeCallbacks::AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) { |
| 28 | thread_callbacks_.push_back(cb); |
| 29 | } |
| 30 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 31 | template <typename T> |
| 32 | ALWAYS_INLINE |
| 33 | static inline void Remove(T* cb, std::vector<T*>* data) { |
| 34 | auto it = std::find(data->begin(), data->end(), cb); |
| 35 | if (it != data->end()) { |
| 36 | data->erase(it); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 40 | void RuntimeCallbacks::RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) { |
| 41 | Remove(cb, &thread_callbacks_); |
| 42 | } |
| 43 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 44 | void RuntimeCallbacks::ThreadStart(Thread* self) { |
| 45 | for (ThreadLifecycleCallback* cb : thread_callbacks_) { |
| 46 | cb->ThreadStart(self); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void RuntimeCallbacks::ThreadDeath(Thread* self) { |
| 51 | for (ThreadLifecycleCallback* cb : thread_callbacks_) { |
| 52 | cb->ThreadDeath(self); |
| 53 | } |
| 54 | } |
| 55 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 56 | void RuntimeCallbacks::AddClassLoadCallback(ClassLoadCallback* cb) { |
| 57 | class_callbacks_.push_back(cb); |
| 58 | } |
| 59 | |
| 60 | void RuntimeCallbacks::RemoveClassLoadCallback(ClassLoadCallback* cb) { |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 61 | Remove(cb, &class_callbacks_); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void RuntimeCallbacks::ClassLoad(Handle<mirror::Class> klass) { |
| 65 | for (ClassLoadCallback* cb : class_callbacks_) { |
| 66 | cb->ClassLoad(klass); |
| 67 | } |
| 68 | } |
| 69 | |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 70 | void RuntimeCallbacks::ClassPreDefine(const char* descriptor, |
| 71 | Handle<mirror::Class> temp_class, |
| 72 | Handle<mirror::ClassLoader> loader, |
| 73 | const DexFile& initial_dex_file, |
| 74 | const DexFile::ClassDef& initial_class_def, |
| 75 | /*out*/DexFile const** final_dex_file, |
| 76 | /*out*/DexFile::ClassDef const** final_class_def) { |
| 77 | DexFile const* current_dex_file = &initial_dex_file; |
| 78 | DexFile::ClassDef const* current_class_def = &initial_class_def; |
| 79 | for (ClassLoadCallback* cb : class_callbacks_) { |
| 80 | DexFile const* new_dex_file = nullptr; |
| 81 | DexFile::ClassDef const* new_class_def = nullptr; |
| 82 | cb->ClassPreDefine(descriptor, |
| 83 | temp_class, |
| 84 | loader, |
| 85 | *current_dex_file, |
| 86 | *current_class_def, |
| 87 | &new_dex_file, |
| 88 | &new_class_def); |
| 89 | if ((new_dex_file != nullptr && new_dex_file != current_dex_file) || |
| 90 | (new_class_def != nullptr && new_class_def != current_class_def)) { |
| 91 | DCHECK(new_dex_file != nullptr && new_class_def != nullptr); |
| 92 | current_dex_file = new_dex_file; |
| 93 | current_class_def = new_class_def; |
| 94 | } |
| 95 | } |
| 96 | *final_dex_file = current_dex_file; |
| 97 | *final_class_def = current_class_def; |
| 98 | } |
| 99 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 100 | void RuntimeCallbacks::ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass) { |
| 101 | for (ClassLoadCallback* cb : class_callbacks_) { |
| 102 | cb->ClassPrepare(temp_klass, klass); |
| 103 | } |
| 104 | } |
| 105 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 106 | void RuntimeCallbacks::AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) { |
| 107 | sigquit_callbacks_.push_back(cb); |
| 108 | } |
| 109 | |
| 110 | void RuntimeCallbacks::RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) { |
| 111 | Remove(cb, &sigquit_callbacks_); |
| 112 | } |
| 113 | |
| 114 | void RuntimeCallbacks::SigQuit() { |
| 115 | for (RuntimeSigQuitCallback* cb : sigquit_callbacks_) { |
| 116 | cb->SigQuit(); |
| 117 | } |
| 118 | } |
| 119 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 120 | void RuntimeCallbacks::AddRuntimePhaseCallback(RuntimePhaseCallback* cb) { |
| 121 | phase_callbacks_.push_back(cb); |
| 122 | } |
| 123 | |
| 124 | void RuntimeCallbacks::RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) { |
| 125 | Remove(cb, &phase_callbacks_); |
| 126 | } |
| 127 | |
| 128 | void RuntimeCallbacks::NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) { |
| 129 | for (RuntimePhaseCallback* cb : phase_callbacks_) { |
| 130 | cb->NextRuntimePhase(phase); |
| 131 | } |
| 132 | } |
| 133 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 134 | } // namespace art |