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 | 1d5be49 | 2022-08-18 13:49:09 +0000 | [diff] [blame] | 53 | // TODO(bugs.webrtc.org/14366): De-template this after millisec-based Wait is |
| 54 | // removed. |
| 55 | template <class T> |
| 56 | bool Wait(T give_up_after) { |
| 57 | webrtc::TimeDelta duration = ToTimeDelta(give_up_after); |
| 58 | return Wait(duration, duration.IsPlusInfinity() |
| 59 | ? webrtc::TimeDelta::Seconds(3) |
| 60 | : kForever); |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 61 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 62 | |
| 63 | private: |
Markus Handell | 1d5be49 | 2022-08-18 13:49:09 +0000 | [diff] [blame] | 64 | // TODO(bugs.webrtc.org/14366): Remove after millisec-based Wait is removed. |
| 65 | static webrtc::TimeDelta ToTimeDelta(int duration) { |
| 66 | // SocketServer users can get here with SocketServer::kForever which is |
| 67 | // -1. Mirror the definition here to avoid dependence. |
| 68 | constexpr int kForeverMs = -1; |
| 69 | return duration == kForeverMs ? kForever |
| 70 | : webrtc::TimeDelta::Millis(duration); |
| 71 | } |
| 72 | static webrtc::TimeDelta ToTimeDelta(webrtc::TimeDelta duration) { |
| 73 | return duration; |
| 74 | } |
| 75 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 76 | #if defined(WEBRTC_WIN) |
| 77 | HANDLE event_handle_; |
| 78 | #elif defined(WEBRTC_POSIX) |
| 79 | pthread_mutex_t event_mutex_; |
| 80 | pthread_cond_t event_cond_; |
| 81 | const bool is_manual_reset_; |
| 82 | bool event_status_; |
| 83 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 86 | // These classes are provided for compatibility with Chromium. |
Tommi | 39d93da | 2018-01-17 12:31:13 +0100 | [diff] [blame] | 87 | // The rtc::Event implementation is overriden inside of Chromium for the |
| 88 | // purposes of detecting when threads are blocked that shouldn't be as well as |
| 89 | // to use the more accurate event implementation that's there than is provided |
| 90 | // by default on some platforms (e.g. Windows). |
| 91 | // When building with standalone WebRTC, this class is a noop. |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 92 | // For further information, please see the |
| 93 | // ScopedAllowBaseSyncPrimitives(ForTesting) classes in Chromium. |
Tommi | 39d93da | 2018-01-17 12:31:13 +0100 | [diff] [blame] | 94 | class ScopedAllowBaseSyncPrimitives { |
| 95 | public: |
| 96 | ScopedAllowBaseSyncPrimitives() {} |
| 97 | ~ScopedAllowBaseSyncPrimitives() {} |
| 98 | }; |
| 99 | |
François Doray | 8ea977d | 2019-03-22 13:01:54 -0400 | [diff] [blame] | 100 | class ScopedAllowBaseSyncPrimitivesForTesting { |
| 101 | public: |
| 102 | ScopedAllowBaseSyncPrimitivesForTesting() {} |
| 103 | ~ScopedAllowBaseSyncPrimitivesForTesting() {} |
| 104 | }; |
| 105 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 106 | } // namespace rtc |
| 107 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 108 | #endif // RTC_BASE_EVENT_H_ |