OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_CODE_EVENTS_H_ | 5 #ifndef V8_CODE_EVENTS_H_ |
6 #define V8_CODE_EVENTS_H_ | 6 #define V8_CODE_EVENTS_H_ |
7 | 7 |
8 #include <unordered_set> | 8 #include <unordered_set> |
9 | 9 |
| 10 #include "src/base/platform/mutex.h" |
10 #include "src/globals.h" | 11 #include "src/globals.h" |
11 | 12 |
12 namespace v8 { | 13 namespace v8 { |
13 namespace internal { | 14 namespace internal { |
14 | 15 |
15 class AbstractCode; | 16 class AbstractCode; |
16 class Name; | 17 class Name; |
17 class SharedFunctionInfo; | 18 class SharedFunctionInfo; |
18 class String; | 19 class String; |
19 | 20 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 virtual void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) = 0; | 108 virtual void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) = 0; |
108 }; | 109 }; |
109 | 110 |
110 class CodeEventDispatcher { | 111 class CodeEventDispatcher { |
111 public: | 112 public: |
112 using LogEventsAndTags = CodeEventListener::LogEventsAndTags; | 113 using LogEventsAndTags = CodeEventListener::LogEventsAndTags; |
113 | 114 |
114 CodeEventDispatcher() {} | 115 CodeEventDispatcher() {} |
115 | 116 |
116 bool AddListener(CodeEventListener* listener) { | 117 bool AddListener(CodeEventListener* listener) { |
| 118 base::LockGuard<base::Mutex> guard(&mutex_); |
117 return listeners_.insert(listener).second; | 119 return listeners_.insert(listener).second; |
118 } | 120 } |
119 void RemoveListener(CodeEventListener* listener) { | 121 void RemoveListener(CodeEventListener* listener) { |
| 122 base::LockGuard<base::Mutex> guard(&mutex_); |
120 listeners_.erase(listener); | 123 listeners_.erase(listener); |
121 } | 124 } |
122 | 125 |
123 #define CODE_EVENT_DISPATCH(code) \ | 126 #define CODE_EVENT_DISPATCH(code) \ |
| 127 if (listeners_.empty()) return; \ |
| 128 base::LockGuard<base::Mutex> guard(&mutex_); \ |
124 for (auto it = listeners_.begin(); it != listeners_.end(); ++it) (*it)->code | 129 for (auto it = listeners_.begin(); it != listeners_.end(); ++it) (*it)->code |
125 | 130 |
126 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 131 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, |
127 const char* comment) { | 132 const char* comment) { |
128 CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, comment)); | 133 CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, comment)); |
129 } | 134 } |
130 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, Name* name) { | 135 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, Name* name) { |
131 CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, name)); | 136 CODE_EVENT_DISPATCH(CodeCreateEvent(tag, code, name)); |
132 } | 137 } |
133 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 138 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 void CodeDisableOptEvent(AbstractCode* code, SharedFunctionInfo* shared) { | 171 void CodeDisableOptEvent(AbstractCode* code, SharedFunctionInfo* shared) { |
167 CODE_EVENT_DISPATCH(CodeDisableOptEvent(code, shared)); | 172 CODE_EVENT_DISPATCH(CodeDisableOptEvent(code, shared)); |
168 } | 173 } |
169 void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) { | 174 void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) { |
170 CODE_EVENT_DISPATCH(CodeDeoptEvent(code, pc, fp_to_sp_delta)); | 175 CODE_EVENT_DISPATCH(CodeDeoptEvent(code, pc, fp_to_sp_delta)); |
171 } | 176 } |
172 #undef CODE_EVENT_DISPATCH | 177 #undef CODE_EVENT_DISPATCH |
173 | 178 |
174 private: | 179 private: |
175 std::unordered_set<CodeEventListener*> listeners_; | 180 std::unordered_set<CodeEventListener*> listeners_; |
| 181 base::Mutex mutex_; |
176 | 182 |
177 DISALLOW_COPY_AND_ASSIGN(CodeEventDispatcher); | 183 DISALLOW_COPY_AND_ASSIGN(CodeEventDispatcher); |
178 }; | 184 }; |
179 | 185 |
180 } // namespace internal | 186 } // namespace internal |
181 } // namespace v8 | 187 } // namespace v8 |
182 | 188 |
183 #endif // V8_CODE_EVENTS_H_ | 189 #endif // V8_CODE_EVENTS_H_ |
OLD | NEW |