blob: f22a6dc28220954687deed0188a0c35e630b2c50 [file] [log] [blame]
Henrik Kjellander67765182017-06-28 20:58:07 +02001/*
2 * Copyright (c) 2016 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 WEBRTC_RTC_BASE_ONETIMEEVENT_H_
12#define WEBRTC_RTC_BASE_ONETIMEEVENT_H_
13
14#include "webrtc/base/criticalsection.h"
15#include "webrtc/typedefs.h"
16
17namespace webrtc {
18// Provides a simple way to perform an operation (such as logging) one
19// time in a certain scope.
20// Example:
21// OneTimeEvent firstFrame;
22// ...
23// if (firstFrame()) {
24// LOG(LS_INFO) << "This is the first frame".
25// }
26class OneTimeEvent {
27 public:
28 OneTimeEvent() {}
29 bool operator()() {
30 rtc::CritScope cs(&critsect_);
31 if (happened_) {
32 return false;
33 }
34 happened_ = true;
35 return true;
36 }
37
38 private:
39 bool happened_ = false;
40 rtc::CriticalSection critsect_;
41};
42
43// A non-thread-safe, ligher-weight version of the OneTimeEvent class.
44class ThreadUnsafeOneTimeEvent {
45 public:
46 ThreadUnsafeOneTimeEvent() {}
47 bool operator()() {
48 if (happened_) {
49 return false;
50 }
51 happened_ = true;
52 return true;
53 }
54
55 private:
56 bool happened_ = false;
57};
58
59} // namespace webrtc
60
61#endif // WEBRTC_RTC_BASE_ONETIMEEVENT_H_