xref: /aosp_15_r20/external/perfetto/src/base/waitable_event.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "perfetto/ext/base/waitable_event.h"
18 
19 namespace perfetto {
20 namespace base {
21 
22 WaitableEvent::WaitableEvent() = default;
23 WaitableEvent::~WaitableEvent() = default;
24 
Wait(uint64_t notifications)25 void WaitableEvent::Wait(uint64_t notifications)
26     PERFETTO_NO_THREAD_SAFETY_ANALYSIS {
27   // 'std::unique_lock' lock doesn't work well with thread annotations
28   // (see https://github.com/llvm/llvm-project/issues/63239),
29   // so we suppress thread safety static analysis for this method.
30   std::unique_lock<std::mutex> lock(mutex_);
31   return event_.wait(lock, [&]() PERFETTO_EXCLUSIVE_LOCKS_REQUIRED(mutex_) {
32     return notifications_ >= notifications;
33   });
34 }
35 
Notify()36 void WaitableEvent::Notify() {
37   std::lock_guard<std::mutex> lock(mutex_);
38   ++notifications_;
39   event_.notify_all();
40 }
41 
42 }  // namespace base
43 }  // namespace perfetto
44