• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_SEQUENCE_LOCAL_SYNC_EVENT_WATCHER_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_SEQUENCE_LOCAL_SYNC_EVENT_WATCHER_H_
7 
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/weak_ptr.h"
11 #include "mojo/public/cpp/bindings/bindings_export.h"
12 
13 namespace mojo {
14 
15 // This encapsulates a SyncEventWatcher watching an event shared by all
16 // |SequenceLocalSyncEventWatcher| on the same sequence. This class is NOT
17 // sequence-safe in general, but |SignalEvent()| is safe to call from any
18 // sequence.
19 //
20 // Interfaces which support sync messages use a WaitableEvent to block and
21 // be signaled when messages are available, but having a WaitableEvent for every
22 // such interface endpoint would cause the number of WaitableEvents to grow
23 // arbitrarily large.
24 //
25 // Some platform constraints may limit the number of WaitableEvents the bindings
26 // layer can wait upon concurrently, so this type is used to keep the number
27 // of such events fixed at a small constant value per sequence regardless of the
28 // number of active interface endpoints supporting sync messages on that
29 // sequence.
30 class MOJO_CPP_BINDINGS_EXPORT SequenceLocalSyncEventWatcher {
31  public:
32   explicit SequenceLocalSyncEventWatcher(
33       const base::RepeatingClosure& callback);
34   ~SequenceLocalSyncEventWatcher();
35 
36   // Signals the shared event on behalf of this specific watcher. Safe to call
37   // from any sequence.
38   void SignalEvent();
39 
40   // Resets the shared event on behalf of this specific watcher.
41   void ResetEvent();
42 
43   // Allows this watcher to be notified during sync wait operations invoked by
44   // other watchers (for example, other SequenceLocalSyncEventWatchers calling
45   // |SyncWatch()|) on the same sequence.
46   void AllowWokenUpBySyncWatchOnSameSequence();
47 
48   // Blocks the calling sequence until the shared event is signaled on behalf of
49   // this specific watcher (i.e. until someone calls |SignalEvent()| on |this|).
50   // Behaves similarly to SyncEventWatcher and SyncHandleWatcher, returning
51   // |true| when |*should_stop| is set to |true|, or |false| if some other
52   // (e.g. error) event interrupts the wait.
53   bool SyncWatch(const bool* should_stop);
54 
55  private:
56   class Registration;
57   class SequenceLocalState;
58   friend class SequenceLocalState;
59 
60   const std::unique_ptr<Registration> registration_;
61   const base::RepeatingClosure callback_;
62   bool can_wake_up_during_any_watch_ = false;
63 
64   DISALLOW_COPY_AND_ASSIGN(SequenceLocalSyncEventWatcher);
65 };
66 
67 }  // namespace mojo
68 
69 #endif  // MOJO_PUBLIC_CPP_BINDINGS_SEQUENCE_LOCAL_SYNC_EVENT_WATCHER_H_
70