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" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #if defined(WEBRTC_WIN) |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 16 | #include <windows.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | #elif defined(WEBRTC_POSIX) |
| 18 | #include <pthread.h> |
| 19 | #else |
| 20 | #error "Must define either WEBRTC_WIN or WEBRTC_POSIX." |
| 21 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | namespace rtc { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 25 | class Event { |
| 26 | public: |
Markus Handell | 1d5be49 | 2022-08-18 13:49:09 +0000 | [diff] [blame] | 27 | // TODO(bugs.webrtc.org/14366): Consider removing this redundant alias. |
| 28 | static constexpr webrtc::TimeDelta kForever = |
| 29 | webrtc::TimeDelta::PlusInfinity(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 30 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 31 | Event(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 32 | Event(bool manual_reset, bool initially_signaled); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 33 | Event(const Event&) = delete; |
| 34 | Event& operator=(const Event&) = delete; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 35 | ~Event(); |
| 36 | |
| 37 | void Set(); |
| 38 | void Reset(); |
| 39 | |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 40 | // 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] | 41 | // than `warn_after`, and gives up completely if it takes more than |
| 42 | // `give_up_after`. (If `warn_after >= give_up_after`, no warning will be |
| 43 | // logged.) Either or both may be `kForever`, which means wait indefinitely. |
| 44 | // |
| 45 | // Care is taken so that the underlying OS wait call isn't requested to sleep |
| 46 | // shorter than `give_up_after`. |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 47 | // |
| 48 | // Returns true if the event was signaled, false if there was a timeout or |
| 49 | // some other error. |
Markus Handell | 1d5be49 | 2022-08-18 13:49:09 +0000 | [diff] [blame] | 50 | bool Wait(webrtc::TimeDelta give_up_after, webrtc::TimeDelta warn_after); |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 51 | |
| 52 | // Waits with the given timeout and a reasonable default warning timeout. |
Markus Handell | 0cd0dd3 | 2022-08-19 12:42:31 +0000 | [diff] [blame] | 53 | bool Wait(webrtc::TimeDelta give_up_after) { |
| 54 | return Wait(give_up_after, give_up_after.IsPlusInfinity() |
| 55 | ? webrtc::TimeDelta::Seconds(3) |
| 56 | : kForever); |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 57 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | #if defined(WEBRTC_WIN) |
| 61 | HANDLE event_handle_; |
| 62 | #elif defined(WEBRTC_POSIX) |
| 63 | pthread_mutex_t event_mutex_; |
| 64 | pthread_cond_t event_cond_; |
| 65 | const bool is_manual_reset_; |
| 66 | bool event_status_; |
| 67 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 70 | // These classes are provided for compatibility with Chromium. |
Tommi | 39d93da | 2018-01-17 12:31:13 +0100 | [diff] [blame] | 71 | // The rtc::Event implementation is overriden inside of Chromium for the |
| 72 | // purposes of detecting when threads are blocked that shouldn't be as well as |
| 73 | // to use the more accurate event implementation that's there than is provided |
| 74 | // by default on some platforms (e.g. Windows). |
| 75 | // When building with standalone WebRTC, this class is a noop. |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 76 | // For further information, please see the |
| 77 | // ScopedAllowBaseSyncPrimitives(ForTesting) classes in Chromium. |
Tommi | 39d93da | 2018-01-17 12:31:13 +0100 | [diff] [blame] | 78 | class ScopedAllowBaseSyncPrimitives { |
| 79 | public: |
| 80 | ScopedAllowBaseSyncPrimitives() {} |
| 81 | ~ScopedAllowBaseSyncPrimitives() {} |
| 82 | }; |
| 83 | |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 84 | class ScopedAllowBaseSyncPrimitivesForTesting { |
| 85 | public: |
| 86 | ScopedAllowBaseSyncPrimitivesForTesting() {} |
| 87 | ~ScopedAllowBaseSyncPrimitivesForTesting() {} |
| 88 | }; |
| 89 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 90 | } // namespace rtc |
| 91 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 92 | #endif // RTC_BASE_EVENT_H_ |