| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 RunPingPongTest("1_Task_Threads_With_Observer", 1); | 173 RunPingPongTest("1_Task_Threads_With_Observer", 1); |
| 174 RunPingPongTest("4_Task_Threads_With_Observer", 4); | 174 RunPingPongTest("4_Task_Threads_With_Observer", 4); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // Class to test our WaitableEvent performance by signaling back and fort. | 177 // Class to test our WaitableEvent performance by signaling back and fort. |
| 178 // WaitableEvent is templated so we can also compare with other versions. | 178 // WaitableEvent is templated so we can also compare with other versions. |
| 179 template <typename WaitableEventType> | 179 template <typename WaitableEventType> |
| 180 class EventPerfTest : public ThreadPerfTest { | 180 class EventPerfTest : public ThreadPerfTest { |
| 181 public: | 181 public: |
| 182 void Init() override { | 182 void Init() override { |
| 183 for (size_t i = 0; i < threads_.size(); i++) | 183 for (size_t i = 0; i < threads_.size(); i++) { |
| 184 events_.push_back(new WaitableEventType(false, false)); | 184 events_.push_back( |
| 185 new WaitableEventType(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 186 WaitableEvent::InitialState::NOT_SIGNALED)); |
| 187 } |
| 185 } | 188 } |
| 186 | 189 |
| 187 void Reset() override { events_.clear(); } | 190 void Reset() override { events_.clear(); } |
| 188 | 191 |
| 189 void WaitAndSignalOnThread(size_t event) { | 192 void WaitAndSignalOnThread(size_t event) { |
| 190 size_t next_event = (event + 1) % events_.size(); | 193 size_t next_event = (event + 1) % events_.size(); |
| 191 int my_hops = 0; | 194 int my_hops = 0; |
| 192 do { | 195 do { |
| 193 events_[event]->Wait(); | 196 events_[event]->Wait(); |
| 194 my_hops = --remaining_hops_; // We own 'hops' between Wait and Signal. | 197 my_hops = --remaining_hops_; // We own 'hops' between Wait and Signal. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 221 // might want to craft a way to test the best-case (where the thread doesn't | 224 // might want to craft a way to test the best-case (where the thread doesn't |
| 222 // end up blocking because the event is already signalled). | 225 // end up blocking because the event is already signalled). |
| 223 typedef EventPerfTest<base::WaitableEvent> WaitableEventPerfTest; | 226 typedef EventPerfTest<base::WaitableEvent> WaitableEventPerfTest; |
| 224 TEST_F(WaitableEventPerfTest, EventPingPong) { | 227 TEST_F(WaitableEventPerfTest, EventPingPong) { |
| 225 RunPingPongTest("4_WaitableEvent_Threads", 4); | 228 RunPingPongTest("4_WaitableEvent_Threads", 4); |
| 226 } | 229 } |
| 227 | 230 |
| 228 // Build a minimal event using ConditionVariable. | 231 // Build a minimal event using ConditionVariable. |
| 229 class ConditionVariableEvent { | 232 class ConditionVariableEvent { |
| 230 public: | 233 public: |
| 231 ConditionVariableEvent(bool manual_reset, bool initially_signaled) | 234 ConditionVariableEvent(WaitableEvent::ResetPolicy reset_policy, |
| 235 WaitableEvent::InitialState initial_state) |
| 232 : cond_(&lock_), signaled_(false) { | 236 : cond_(&lock_), signaled_(false) { |
| 233 DCHECK(!manual_reset); | 237 DCHECK_EQ(WaitableEvent::ResetPolicy::AUTOMATIC, reset_policy); |
| 234 DCHECK(!initially_signaled); | 238 DCHECK_EQ(WaitableEvent::InitialState::NOT_SIGNALED, initial_state); |
| 235 } | 239 } |
| 236 | 240 |
| 237 void Signal() { | 241 void Signal() { |
| 238 { | 242 { |
| 239 base::AutoLock scoped_lock(lock_); | 243 base::AutoLock scoped_lock(lock_); |
| 240 signaled_ = true; | 244 signaled_ = true; |
| 241 } | 245 } |
| 242 cond_.Signal(); | 246 cond_.Signal(); |
| 243 } | 247 } |
| 244 | 248 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 260 typedef EventPerfTest<ConditionVariableEvent> ConditionVariablePerfTest; | 264 typedef EventPerfTest<ConditionVariableEvent> ConditionVariablePerfTest; |
| 261 TEST_F(ConditionVariablePerfTest, EventPingPong) { | 265 TEST_F(ConditionVariablePerfTest, EventPingPong) { |
| 262 RunPingPongTest("4_ConditionVariable_Threads", 4); | 266 RunPingPongTest("4_ConditionVariable_Threads", 4); |
| 263 } | 267 } |
| 264 #if defined(OS_POSIX) | 268 #if defined(OS_POSIX) |
| 265 | 269 |
| 266 // Absolutely 100% minimal posix waitable event. If there is a better/faster | 270 // Absolutely 100% minimal posix waitable event. If there is a better/faster |
| 267 // way to force a context switch, we should use that instead. | 271 // way to force a context switch, we should use that instead. |
| 268 class PthreadEvent { | 272 class PthreadEvent { |
| 269 public: | 273 public: |
| 270 PthreadEvent(bool manual_reset, bool initially_signaled) { | 274 PthreadEvent(WaitableEvent::ResetPolicy reset_policy, |
| 271 DCHECK(!manual_reset); | 275 WaitableEvent::InitialState initial_state) { |
| 272 DCHECK(!initially_signaled); | 276 DCHECK_EQ(WaitableEvent::ResetPolicy::AUTOMATIC, reset_policy); |
| 277 DCHECK_EQ(WaitableEvent::InitialState::NOT_SIGNALED, initial_state); |
| 273 pthread_mutex_init(&mutex_, 0); | 278 pthread_mutex_init(&mutex_, 0); |
| 274 pthread_cond_init(&cond_, 0); | 279 pthread_cond_init(&cond_, 0); |
| 275 signaled_ = false; | 280 signaled_ = false; |
| 276 } | 281 } |
| 277 | 282 |
| 278 ~PthreadEvent() { | 283 ~PthreadEvent() { |
| 279 pthread_cond_destroy(&cond_); | 284 pthread_cond_destroy(&cond_); |
| 280 pthread_mutex_destroy(&mutex_); | 285 pthread_mutex_destroy(&mutex_); |
| 281 } | 286 } |
| 282 | 287 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 306 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; | 311 typedef EventPerfTest<PthreadEvent> PthreadEventPerfTest; |
| 307 TEST_F(PthreadEventPerfTest, EventPingPong) { | 312 TEST_F(PthreadEventPerfTest, EventPingPong) { |
| 308 RunPingPongTest("4_PthreadCondVar_Threads", 4); | 313 RunPingPongTest("4_PthreadCondVar_Threads", 4); |
| 309 } | 314 } |
| 310 | 315 |
| 311 #endif | 316 #endif |
| 312 | 317 |
| 313 } // namespace | 318 } // namespace |
| 314 | 319 |
| 315 } // namespace base | 320 } // namespace base |
| OLD | NEW |