• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #include "base/synchronization/cancelable_event.h"
5 
6 #include "base/notreached.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "base/threading/scoped_blocking_call.h"
9 #include "base/trace_event/base_tracing.h"
10 #include "build/build_config.h"
11 
12 namespace base {
13 
Signal()14 void CancelableEvent::Signal() {
15 #if BUILDFLAG(ENABLE_BASE_TRACING)
16   // Must be ordered before SignalImpl() to match the `TerminatingFlow` in
17   // TimedWait() and Cancel().
18   if (!only_used_while_idle_) {
19     TRACE_EVENT_INSTANT("wakeup.flow,toplevel.flow", "CancelableEvent::Signal",
20                         perfetto::Flow::FromPointer(this));
21   }
22 #endif
23   SignalImpl();
24 }
25 
Cancel()26 bool CancelableEvent::Cancel() {
27 #if BUILDFLAG(ENABLE_BASE_TRACING)
28   if (!only_used_while_idle_) {
29     TRACE_EVENT_INSTANT("wakeup.flow,toplevel.flow", "CancelableEvent::Cancel",
30                         perfetto::TerminatingFlow::FromPointer(this));
31   }
32 #endif
33   return CancelImpl();
34 }
35 
TimedWait(TimeDelta timeout)36 bool CancelableEvent::TimedWait(TimeDelta timeout) {
37   // Consider this thread blocked for scheduling purposes. Ignore this for
38   // non-blocking CancelableEvents.
39   std::optional<internal::ScopedBlockingCallWithBaseSyncPrimitives>
40       scoped_blocking_call;
41   if (!only_used_while_idle_) {
42     scoped_blocking_call.emplace(FROM_HERE, BlockingType::MAY_BLOCK);
43   }
44 
45   const bool result = TimedWaitImpl(timeout);
46 
47 #if BUILDFLAG(ENABLE_BASE_TRACING)
48   if (result && !only_used_while_idle_) {
49     TRACE_EVENT_INSTANT("wakeup.flow,toplevel.flow",
50                         "CancelableEvent::Wait Complete",
51                         perfetto::TerminatingFlow::FromPointer(this));
52   }
53 #endif
54 
55   return result;
56 }
57 
58 }  // namespace base
59