Line data Source code
1 : /*
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 :
14 : #include "webrtc/base/criticalsection.h"
15 : #include "webrtc/typedefs.h"
16 :
17 : namespace 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 : // }
26 0 : class OneTimeEvent {
27 : public:
28 0 : OneTimeEvent() {}
29 0 : bool operator()() {
30 0 : rtc::CritScope cs(&critsect_);
31 0 : if (happened_) {
32 0 : return false;
33 : }
34 0 : happened_ = true;
35 0 : 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.
44 : class ThreadUnsafeOneTimeEvent {
45 : public:
46 0 : ThreadUnsafeOneTimeEvent() {}
47 0 : bool operator()() {
48 0 : if (happened_) {
49 0 : return false;
50 : }
51 0 : happened_ = true;
52 0 : return true;
53 : }
54 :
55 : private:
56 : bool happened_ = false;
57 : };
58 :
59 : } // namespace webrtc
60 :
61 : #endif // WEBRTC_BASE_ONETIMEEVENT_H_
|