blob: 240cf14c5842392b1a5f4c9dd6668ff62b220388 [file] [log] [blame]
skvlad98bb6642016-04-07 15:36:45 -07001/*
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_BASE_ONETIMEEVENT_H_
12#define WEBRTC_BASE_ONETIMEEVENT_H_
13
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include "webrtc/base/criticalsection.h"
15#include "webrtc/typedefs.h"
skvlad98bb6642016-04-07 15:36:45 -070016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017namespace 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
skvlad98bb6642016-04-07 15:36:45 -070060
61#endif // WEBRTC_BASE_ONETIMEEVENT_H_