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 11 #ifndef RTC_BASE_EVENT_H_ 12 #define RTC_BASE_EVENT_H_ 13 14 #if defined(WEBRTC_WIN) 15 #include <windows.h> 16 #elif defined(WEBRTC_POSIX) 17 #include <pthread.h> 18 #else 19 #error "Must define either WEBRTC_WIN or WEBRTC_POSIX." 20 #endif 21 22 namespace rtc { 23 24 class Event { 25 public: 26 static const int kForever = -1; 27 28 Event(); 29 Event(bool manual_reset, bool initially_signaled); 30 Event(const Event&) = delete; 31 Event& operator=(const Event&) = delete; 32 ~Event(); 33 34 void Set(); 35 void Reset(); 36 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. Wait(int give_up_after_ms)48 bool Wait(int give_up_after_ms) { 49 return Wait(give_up_after_ms, 50 give_up_after_ms == kForever ? 3000 : kForever); 51 } 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 62 }; 63 64 // These classes are provided for compatibility with Chromium. 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. 70 // For further information, please see the 71 // ScopedAllowBaseSyncPrimitives(ForTesting) classes in Chromium. 72 class ScopedAllowBaseSyncPrimitives { 73 public: ScopedAllowBaseSyncPrimitives()74 ScopedAllowBaseSyncPrimitives() {} ~ScopedAllowBaseSyncPrimitives()75 ~ScopedAllowBaseSyncPrimitives() {} 76 }; 77 78 class ScopedAllowBaseSyncPrimitivesForTesting { 79 public: ScopedAllowBaseSyncPrimitivesForTesting()80 ScopedAllowBaseSyncPrimitivesForTesting() {} ~ScopedAllowBaseSyncPrimitivesForTesting()81 ~ScopedAllowBaseSyncPrimitivesForTesting() {} 82 }; 83 84 } // namespace rtc 85 86 #endif // RTC_BASE_EVENT_H_ 87