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 | #ifndef ART_RUNTIME_RUNTIME_CALLBACKS_H_ |
| 18 | #define ART_RUNTIME_RUNTIME_CALLBACKS_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "base/macros.h" |
| 23 | #include "base/mutex.h" |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 24 | #include "handle.h" |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 28 | namespace mirror { |
| 29 | class Class; |
| 30 | } // namespace mirror |
| 31 | |
| 32 | class ClassLoadCallback; |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 33 | class Thread; |
| 34 | class ThreadLifecycleCallback; |
| 35 | |
| 36 | // Note: RuntimeCallbacks uses the mutator lock to synchronize the callback lists. A thread must |
| 37 | // hold the exclusive lock to add or remove a listener. A thread must hold the shared lock |
| 38 | // to dispatch an event. This setup is chosen as some clients may want to suspend the |
| 39 | // dispatching thread or all threads. |
| 40 | // |
| 41 | // To make this safe, the following restrictions apply: |
| 42 | // * Only the owner of a listener may ever add or remove said listener. |
| 43 | // * A listener must never add or remove itself or any other listener while running. |
| 44 | // * It is the responsibility of the owner to not remove the listener while it is running |
| 45 | // (and suspended). |
| 46 | // |
| 47 | // The simplest way to satisfy these restrictions is to never remove a listener, and to do |
| 48 | // any state checking (is the listener enabled) in the listener itself. For an example, see |
| 49 | // Dbg. |
| 50 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 51 | class RuntimeSigQuitCallback { |
| 52 | public: |
| 53 | virtual ~RuntimeSigQuitCallback() {} |
| 54 | |
| 55 | virtual void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 56 | }; |
| 57 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame^] | 58 | class RuntimePhaseCallback { |
| 59 | public: |
| 60 | enum RuntimePhase { |
| 61 | kStart, // The runtime is started. |
| 62 | kInit, // The runtime is initialized (and will run user code soon). |
| 63 | kDeath, // The runtime just died. |
| 64 | }; |
| 65 | |
| 66 | virtual ~RuntimePhaseCallback() {} |
| 67 | |
| 68 | virtual void NextRuntimePhase(RuntimePhase phase) REQUIRES_SHARED(Locks::mutator_lock_) = 0; |
| 69 | }; |
| 70 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 71 | class RuntimeCallbacks { |
| 72 | public: |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 73 | void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 74 | void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb) REQUIRES(Locks::mutator_lock_); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 75 | |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 76 | void ThreadStart(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); |
| 77 | void ThreadDeath(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_); |
| 78 | |
| 79 | void AddClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 80 | void RemoveClassLoadCallback(ClassLoadCallback* cb) REQUIRES(Locks::mutator_lock_); |
| 81 | |
| 82 | void ClassLoad(Handle<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); |
| 83 | void ClassPrepare(Handle<mirror::Class> temp_klass, Handle<mirror::Class> klass) |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 84 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 85 | |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 86 | void AddRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) |
| 87 | REQUIRES(Locks::mutator_lock_); |
| 88 | void RemoveRuntimeSigQuitCallback(RuntimeSigQuitCallback* cb) |
| 89 | REQUIRES(Locks::mutator_lock_); |
| 90 | |
| 91 | void SigQuit() REQUIRES_SHARED(Locks::mutator_lock_); |
| 92 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame^] | 93 | void AddRuntimePhaseCallback(RuntimePhaseCallback* cb) |
| 94 | REQUIRES(Locks::mutator_lock_); |
| 95 | void RemoveRuntimePhaseCallback(RuntimePhaseCallback* cb) |
| 96 | REQUIRES(Locks::mutator_lock_); |
| 97 | |
| 98 | void NextRuntimePhase(RuntimePhaseCallback::RuntimePhase phase) |
| 99 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 100 | |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 101 | private: |
| 102 | std::vector<ThreadLifecycleCallback*> thread_callbacks_ |
| 103 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 104 | std::vector<ClassLoadCallback*> class_callbacks_ |
| 105 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | a5814f9 | 2017-01-18 21:43:16 -0800 | [diff] [blame] | 106 | std::vector<RuntimeSigQuitCallback*> sigquit_callbacks_ |
| 107 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame^] | 108 | std::vector<RuntimePhaseCallback*> phase_callbacks_ |
| 109 | GUARDED_BY(Locks::mutator_lock_); |
Andreas Gampe | 04bbb5b | 2017-01-19 17:49:03 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace art |
| 113 | |
| 114 | #endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_ |