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 RTC_BASE_ONE_TIME_EVENT_H_ 12 #define RTC_BASE_ONE_TIME_EVENT_H_ 13 14 #include "rtc_base/synchronization/mutex.h" 15 16 namespace webrtc { 17 // Provides a simple way to perform an operation (such as logging) one 18 // time in a certain scope. 19 // Example: 20 // OneTimeEvent firstFrame; 21 // ... 22 // if (firstFrame()) { 23 // RTC_LOG(LS_INFO) << "This is the first frame". 24 // } 25 class OneTimeEvent { 26 public: OneTimeEvent()27 OneTimeEvent() {} operator()28 bool operator()() { 29 MutexLock lock(&mutex_); 30 if (happened_) { 31 return false; 32 } 33 happened_ = true; 34 return true; 35 } 36 37 private: 38 bool happened_ = false; 39 Mutex mutex_; 40 }; 41 42 // A non-thread-safe, ligher-weight version of the OneTimeEvent class. 43 class ThreadUnsafeOneTimeEvent { 44 public: ThreadUnsafeOneTimeEvent()45 ThreadUnsafeOneTimeEvent() {} operator()46 bool operator()() { 47 if (happened_) { 48 return false; 49 } 50 happened_ = true; 51 return true; 52 } 53 54 private: 55 bool happened_ = false; 56 }; 57 58 } // namespace webrtc 59 60 #endif // RTC_BASE_ONE_TIME_EVENT_H_ 61