blob: ab66d6afce038b0862529f4225b612538db13f1e [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
Markus Handell1d5be492022-08-18 13:49:09 +000014#include "api/units/time_delta.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#if defined(WEBRTC_WIN)
Patrik Höglunda8005cf2017-12-13 16:05:42 +010016#include <windows.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#elif defined(WEBRTC_POSIX)
18#include <pthread.h>
19#else
20#error "Must define either WEBRTC_WIN or WEBRTC_POSIX."
21#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023namespace rtc {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025class Event {
26 public:
Markus Handell1d5be492022-08-18 13:49:09 +000027 // TODO(bugs.webrtc.org/14366): Consider removing this redundant alias.
28 static constexpr webrtc::TimeDelta kForever =
29 webrtc::TimeDelta::PlusInfinity();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020030
Niels Möllerc572ff32018-11-07 08:43:50 +010031 Event();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032 Event(bool manual_reset, bool initially_signaled);
Niels Möllerc572ff32018-11-07 08:43:50 +010033 Event(const Event&) = delete;
34 Event& operator=(const Event&) = delete;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020035 ~Event();
36
37 void Set();
38 void Reset();
39
Karl Wibergfc47c862019-04-11 10:31:24 +020040 // Waits for the event to become signaled, but logs a warning if it takes more
Markus Handell1d5be492022-08-18 13:49:09 +000041 // 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 Wibergfc47c862019-04-11 10:31:24 +020047 //
48 // Returns true if the event was signaled, false if there was a timeout or
49 // some other error.
Markus Handell1d5be492022-08-18 13:49:09 +000050 bool Wait(webrtc::TimeDelta give_up_after, webrtc::TimeDelta warn_after);
Karl Wibergfc47c862019-04-11 10:31:24 +020051
52 // Waits with the given timeout and a reasonable default warning timeout.
Markus Handell1d5be492022-08-18 13:49:09 +000053 // 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 Wibergfc47c862019-04-11 10:31:24 +020061 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
63 private:
Markus Handell1d5be492022-08-18 13:49:09 +000064 // 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 Kjellanderec78f1c2017-06-29 07:52:50 +020076#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 Kjellanderec78f1c2017-06-29 07:52:50 +020084};
85
François Doray8ea977d2019-03-22 13:01:54 -040086// These classes are provided for compatibility with Chromium.
Tommi39d93da2018-01-17 12:31:13 +010087// 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 Doray8ea977d2019-03-22 13:01:54 -040092// For further information, please see the
93// ScopedAllowBaseSyncPrimitives(ForTesting) classes in Chromium.
Tommi39d93da2018-01-17 12:31:13 +010094class ScopedAllowBaseSyncPrimitives {
95 public:
96 ScopedAllowBaseSyncPrimitives() {}
97 ~ScopedAllowBaseSyncPrimitives() {}
98};
99
François Doray8ea977d2019-03-22 13:01:54 -0400100class ScopedAllowBaseSyncPrimitivesForTesting {
101 public:
102 ScopedAllowBaseSyncPrimitivesForTesting() {}
103 ~ScopedAllowBaseSyncPrimitivesForTesting() {}
104};
105
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200106} // namespace rtc
107
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200108#endif // RTC_BASE_EVENT_H_