blob: 584ad5d35a47534d95a4abfc2eaba914edfe75f9 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_EVENT_H_
12#define RTC_BASE_EVENT_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#if defined(WEBRTC_WIN)
Patrik Höglunda8005cf2017-12-13 16:05:42 +010015#include <windows.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#elif defined(WEBRTC_POSIX)
17#include <pthread.h>
18#else
19#error "Must define either WEBRTC_WIN or WEBRTC_POSIX."
20#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024class Event {
25 public:
26 static const int kForever = -1;
27
Niels Möllerc572ff32018-11-07 08:43:50 +010028 Event();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029 Event(bool manual_reset, bool initially_signaled);
Niels Möllerc572ff32018-11-07 08:43:50 +010030 Event(const Event&) = delete;
31 Event& operator=(const Event&) = delete;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032 ~Event();
33
34 void Set();
35 void Reset();
36
Karl Wibergfc47c862019-04-11 10:31:24 +020037 // 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 Kjellanderec78f1c2017-06-29 07:52:50 +020052
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 Kjellanderec78f1c2017-06-29 07:52:50 +020062};
63
François Doray8ea977d2019-03-22 13:01:54 -040064// These classes are provided for compatibility with Chromium.
Tommi39d93da2018-01-17 12:31:13 +010065// 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 Doray8ea977d2019-03-22 13:01:54 -040070// For further information, please see the
71// ScopedAllowBaseSyncPrimitives(ForTesting) classes in Chromium.
Tommi39d93da2018-01-17 12:31:13 +010072class ScopedAllowBaseSyncPrimitives {
73 public:
74 ScopedAllowBaseSyncPrimitives() {}
75 ~ScopedAllowBaseSyncPrimitives() {}
76};
77
François Doray8ea977d2019-03-22 13:01:54 -040078class ScopedAllowBaseSyncPrimitivesForTesting {
79 public:
80 ScopedAllowBaseSyncPrimitivesForTesting() {}
81 ~ScopedAllowBaseSyncPrimitivesForTesting() {}
82};
83
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020084} // namespace rtc
85
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020086#endif // RTC_BASE_EVENT_H_