henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/event.h" |
| 12 | #include "rtc_base/gunit.h" |
Kári Tristan Helgason | c5975bf | 2017-11-20 15:31:38 +0100 | [diff] [blame] | 13 | #include "rtc_base/platform_thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | |
| 15 | namespace rtc { |
| 16 | |
| 17 | TEST(EventTest, InitiallySignaled) { |
| 18 | Event event(false, true); |
| 19 | ASSERT_TRUE(event.Wait(0)); |
| 20 | } |
| 21 | |
| 22 | TEST(EventTest, ManualReset) { |
| 23 | Event event(true, false); |
| 24 | ASSERT_FALSE(event.Wait(0)); |
| 25 | |
| 26 | event.Set(); |
| 27 | ASSERT_TRUE(event.Wait(0)); |
| 28 | ASSERT_TRUE(event.Wait(0)); |
| 29 | |
| 30 | event.Reset(); |
| 31 | ASSERT_FALSE(event.Wait(0)); |
| 32 | } |
| 33 | |
| 34 | TEST(EventTest, AutoReset) { |
| 35 | Event event(false, false); |
| 36 | ASSERT_FALSE(event.Wait(0)); |
| 37 | |
| 38 | event.Set(); |
| 39 | ASSERT_TRUE(event.Wait(0)); |
| 40 | ASSERT_FALSE(event.Wait(0)); |
| 41 | } |
| 42 | |
Kári Tristan Helgason | c5975bf | 2017-11-20 15:31:38 +0100 | [diff] [blame] | 43 | class SignalerThread { |
| 44 | public: |
| 45 | SignalerThread() : thread_(&ThreadFn, this, "EventPerf") {} |
| 46 | void Start(Event* writer, Event* reader) { |
| 47 | writer_ = writer; |
| 48 | reader_ = reader; |
| 49 | thread_.Start(); |
| 50 | } |
| 51 | void Stop() { |
| 52 | stop_event_.Set(); |
| 53 | thread_.Stop(); |
| 54 | } |
| 55 | static void ThreadFn(void *param) { |
| 56 | auto* me = static_cast<SignalerThread*>(param); |
| 57 | while(!me->stop_event_.Wait(0)) { |
| 58 | me->writer_->Set(); |
| 59 | me->reader_->Wait(Event::kForever); |
| 60 | } |
| 61 | } |
| 62 | Event stop_event_{false, false}; |
| 63 | Event* writer_; |
| 64 | Event* reader_; |
| 65 | PlatformThread thread_; |
| 66 | }; |
| 67 | |
| 68 | // These tests are disabled by default and only intended to be run manually. |
Kári Tristan Helgason | ce15cd3 | 2017-11-27 13:18:18 +0100 | [diff] [blame] | 69 | TEST(EventTest, DISABLED_PerformanceSingleThread) { |
Kári Tristan Helgason | c5975bf | 2017-11-20 15:31:38 +0100 | [diff] [blame] | 70 | static const int kNumIterations = 10000000; |
| 71 | Event event(false, false); |
| 72 | for (int i = 0; i < kNumIterations; ++i) { |
| 73 | event.Set(); |
| 74 | event.Wait(0); |
| 75 | } |
| 76 | } |
| 77 | |
Kári Tristan Helgason | ce15cd3 | 2017-11-27 13:18:18 +0100 | [diff] [blame] | 78 | TEST(EventTest, DISABLED_PerformanceMultiThread) { |
Kári Tristan Helgason | c5975bf | 2017-11-20 15:31:38 +0100 | [diff] [blame] | 79 | static const int kNumIterations = 10000; |
| 80 | Event read(false, false); |
| 81 | Event write(false, false); |
| 82 | SignalerThread thread; |
| 83 | thread.Start(&read, &write); |
| 84 | |
| 85 | for (int i = 0; i < kNumIterations; ++i) { |
| 86 | write.Set(); |
| 87 | read.Wait(Event::kForever); |
| 88 | } |
| 89 | write.Set(); |
| 90 | |
| 91 | thread.Stop(); |
| 92 | } |
| 93 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | } // namespace rtc |