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