event.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef RTC_BASE_EVENT_H_
  11. #define RTC_BASE_EVENT_H_
  12. #if defined(WEBRTC_WIN)
  13. #include <windows.h>
  14. #elif defined(WEBRTC_POSIX)
  15. #include <pthread.h>
  16. #else
  17. #error "Must define either WEBRTC_WIN or WEBRTC_POSIX."
  18. #endif
  19. namespace rtc {
  20. class Event {
  21. public:
  22. static const int kForever = -1;
  23. Event();
  24. Event(bool manual_reset, bool initially_signaled);
  25. Event(const Event&) = delete;
  26. Event& operator=(const Event&) = delete;
  27. ~Event();
  28. void Set();
  29. void Reset();
  30. // Waits for the event to become signaled, but logs a warning if it takes more
  31. // than `warn_after_ms` milliseconds, and gives up completely if it takes more
  32. // than `give_up_after_ms` milliseconds. (If `warn_after_ms >=
  33. // give_up_after_ms`, no warning will be logged.) Either or both may be
  34. // `kForever`, which means wait indefinitely.
  35. //
  36. // Returns true if the event was signaled, false if there was a timeout or
  37. // some other error.
  38. bool Wait(int give_up_after_ms, int warn_after_ms);
  39. // Waits with the given timeout and a reasonable default warning timeout.
  40. bool Wait(int give_up_after_ms) {
  41. return Wait(give_up_after_ms,
  42. give_up_after_ms == kForever ? 3000 : kForever);
  43. }
  44. private:
  45. #if defined(WEBRTC_WIN)
  46. HANDLE event_handle_;
  47. #elif defined(WEBRTC_POSIX)
  48. pthread_mutex_t event_mutex_;
  49. pthread_cond_t event_cond_;
  50. const bool is_manual_reset_;
  51. bool event_status_;
  52. #endif
  53. };
  54. // These classes are provided for compatibility with Chromium.
  55. // The rtc::Event implementation is overriden inside of Chromium for the
  56. // purposes of detecting when threads are blocked that shouldn't be as well as
  57. // to use the more accurate event implementation that's there than is provided
  58. // by default on some platforms (e.g. Windows).
  59. // When building with standalone WebRTC, this class is a noop.
  60. // For further information, please see the
  61. // ScopedAllowBaseSyncPrimitives(ForTesting) classes in Chromium.
  62. class ScopedAllowBaseSyncPrimitives {
  63. public:
  64. ScopedAllowBaseSyncPrimitives() {}
  65. ~ScopedAllowBaseSyncPrimitives() {}
  66. };
  67. class ScopedAllowBaseSyncPrimitivesForTesting {
  68. public:
  69. ScopedAllowBaseSyncPrimitivesForTesting() {}
  70. ~ScopedAllowBaseSyncPrimitivesForTesting() {}
  71. };
  72. } // namespace rtc
  73. #endif // RTC_BASE_EVENT_H_