blob: 1060d853d81e3a9dd59747ceb49680c6fc51f50e [file] [log] [blame]
Andreas Gampe04bbb5b2017-01-19 17:49:03 +00001/*
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"
24
25namespace art {
26
27class Thread;
28class ThreadLifecycleCallback;
29
30// Note: RuntimeCallbacks uses the mutator lock to synchronize the callback lists. A thread must
31// hold the exclusive lock to add or remove a listener. A thread must hold the shared lock
32// to dispatch an event. This setup is chosen as some clients may want to suspend the
33// dispatching thread or all threads.
34//
35// To make this safe, the following restrictions apply:
36// * Only the owner of a listener may ever add or remove said listener.
37// * A listener must never add or remove itself or any other listener while running.
38// * It is the responsibility of the owner to not remove the listener while it is running
39// (and suspended).
40//
41// The simplest way to satisfy these restrictions is to never remove a listener, and to do
42// any state checking (is the listener enabled) in the listener itself. For an example, see
43// Dbg.
44
45class RuntimeCallbacks {
46 public:
47 void AddThreadLifecycleCallback(ThreadLifecycleCallback* cb)
48 REQUIRES(Locks::mutator_lock_);
49 void RemoveThreadLifecycleCallback(ThreadLifecycleCallback* cb)
50 REQUIRES(Locks::mutator_lock_);
51
52 void ThreadStart(Thread* self)
53 REQUIRES_SHARED(Locks::mutator_lock_);
54 void ThreadDeath(Thread* self)
55 REQUIRES_SHARED(Locks::mutator_lock_);
56
57 private:
58 std::vector<ThreadLifecycleCallback*> thread_callbacks_
59 GUARDED_BY(Locks::mutator_lock_);
60};
61
62} // namespace art
63
64#endif // ART_RUNTIME_RUNTIME_CALLBACKS_H_