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 | #ifndef RTC_BASE_EVENT_H_ |
| 12 | #define RTC_BASE_EVENT_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Markus Handell | 1d5be49 | 2022-08-18 13:49:09 +0000 | [diff] [blame] | 14 | #include "api/units/time_delta.h" |
Tommi | 2fc3757 | 2022-10-24 09:22:16 +0200 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #if defined(WEBRTC_WIN) |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 17 | #include <windows.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | #elif defined(WEBRTC_POSIX) |
| 19 | #include <pthread.h> |
| 20 | #else |
| 21 | #error "Must define either WEBRTC_WIN or WEBRTC_POSIX." |
| 22 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | |
Tommi | 2fc3757 | 2022-10-24 09:22:16 +0200 | [diff] [blame] | 24 | #include "rtc_base/synchronization/yield_policy.h" |
| 25 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | namespace rtc { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | |
Tommi | 2fc3757 | 2022-10-24 09:22:16 +0200 | [diff] [blame] | 28 | // RTC_DISALLOW_WAIT() utility |
| 29 | // |
| 30 | // Sets a stack-scoped flag that disallows use of `rtc::Event::Wait` by means |
| 31 | // of raising a DCHECK when a call to `rtc::Event::Wait()` is made.. |
| 32 | // This is useful to guard synchronization-free scopes against regressions. |
| 33 | // |
| 34 | // Example of what this would catch (`ScopeToProtect` calls `Foo`): |
| 35 | // |
| 36 | // void Foo(TaskQueue* tq) { |
| 37 | // Event event; |
| 38 | // tq->PostTask([&event]() { |
| 39 | // event.Set(); |
| 40 | // }); |
| 41 | // event.Wait(Event::kForever); // <- Will trigger a DCHECK. |
| 42 | // } |
| 43 | // |
| 44 | // void ScopeToProtect() { |
| 45 | // TaskQueue* tq = GetSomeTaskQueue(); |
| 46 | // RTC_DISALLOW_WAIT(); // Policy takes effect. |
| 47 | // Foo(tq); |
| 48 | // } |
| 49 | // |
| 50 | #if RTC_DCHECK_IS_ON |
| 51 | #define RTC_DISALLOW_WAIT() ScopedDisallowWait disallow_wait_##__LINE__ |
| 52 | #else |
| 53 | #define RTC_DISALLOW_WAIT() |
| 54 | #endif |
| 55 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 56 | class Event { |
| 57 | public: |
Markus Handell | 1d5be49 | 2022-08-18 13:49:09 +0000 | [diff] [blame] | 58 | // TODO(bugs.webrtc.org/14366): Consider removing this redundant alias. |
| 59 | static constexpr webrtc::TimeDelta kForever = |
| 60 | webrtc::TimeDelta::PlusInfinity(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 61 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 62 | Event(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 63 | Event(bool manual_reset, bool initially_signaled); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 64 | Event(const Event&) = delete; |
| 65 | Event& operator=(const Event&) = delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 66 | ~Event(); |
| 67 | |
| 68 | void Set(); |
| 69 | void Reset(); |
| 70 | |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 71 | // Waits for the event to become signaled, but logs a warning if it takes more |
Markus Handell | 1d5be49 | 2022-08-18 13:49:09 +0000 | [diff] [blame] | 72 | // than `warn_after`, and gives up completely if it takes more than |
| 73 | // `give_up_after`. (If `warn_after >= give_up_after`, no warning will be |
| 74 | // logged.) Either or both may be `kForever`, which means wait indefinitely. |
| 75 | // |
| 76 | // Care is taken so that the underlying OS wait call isn't requested to sleep |
| 77 | // shorter than `give_up_after`. |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 78 | // |
| 79 | // Returns true if the event was signaled, false if there was a timeout or |
| 80 | // some other error. |
Markus Handell | 1d5be49 | 2022-08-18 13:49:09 +0000 | [diff] [blame] | 81 | bool Wait(webrtc::TimeDelta give_up_after, webrtc::TimeDelta warn_after); |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 82 | |
| 83 | // Waits with the given timeout and a reasonable default warning timeout. |
Markus Handell | 0cd0dd3 | 2022-08-19 12:42:31 +0000 | [diff] [blame] | 84 | bool Wait(webrtc::TimeDelta give_up_after) { |
| 85 | return Wait(give_up_after, give_up_after.IsPlusInfinity() |
| 86 | ? webrtc::TimeDelta::Seconds(3) |
| 87 | : kForever); |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 88 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | #if defined(WEBRTC_WIN) |
| 92 | HANDLE event_handle_; |
| 93 | #elif defined(WEBRTC_POSIX) |
| 94 | pthread_mutex_t event_mutex_; |
| 95 | pthread_cond_t event_cond_; |
| 96 | const bool is_manual_reset_; |
| 97 | bool event_status_; |
| 98 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 101 | // These classes are provided for compatibility with Chromium. |
Tommi | 39d93da | 2018-01-17 12:31:13 +0100 | [diff] [blame] | 102 | // The rtc::Event implementation is overriden inside of Chromium for the |
| 103 | // purposes of detecting when threads are blocked that shouldn't be as well as |
| 104 | // to use the more accurate event implementation that's there than is provided |
| 105 | // by default on some platforms (e.g. Windows). |
| 106 | // When building with standalone WebRTC, this class is a noop. |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 107 | // For further information, please see the |
| 108 | // ScopedAllowBaseSyncPrimitives(ForTesting) classes in Chromium. |
Tommi | 39d93da | 2018-01-17 12:31:13 +0100 | [diff] [blame] | 109 | class ScopedAllowBaseSyncPrimitives { |
| 110 | public: |
| 111 | ScopedAllowBaseSyncPrimitives() {} |
| 112 | ~ScopedAllowBaseSyncPrimitives() {} |
| 113 | }; |
| 114 | |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 115 | class ScopedAllowBaseSyncPrimitivesForTesting { |
| 116 | public: |
| 117 | ScopedAllowBaseSyncPrimitivesForTesting() {} |
| 118 | ~ScopedAllowBaseSyncPrimitivesForTesting() {} |
| 119 | }; |
| 120 | |
Tommi | 2fc3757 | 2022-10-24 09:22:16 +0200 | [diff] [blame] | 121 | #if RTC_DCHECK_IS_ON |
| 122 | class ScopedDisallowWait { |
| 123 | public: |
| 124 | ScopedDisallowWait() = default; |
| 125 | |
| 126 | private: |
| 127 | class DisallowYieldHandler : public YieldInterface { |
| 128 | public: |
| 129 | void YieldExecution() override { RTC_DCHECK_NOTREACHED(); } |
| 130 | } handler_; |
| 131 | rtc::ScopedYieldPolicy policy{&handler_}; |
| 132 | }; |
| 133 | #endif |
| 134 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 135 | } // namespace rtc |
| 136 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 137 | #endif // RTC_BASE_EVENT_H_ |