• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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_SYNC_EVENT_WATCHER_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_SYNC_EVENT_WATCHER_H_
7 
8 #include <stddef.h>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/sequence_checker.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "mojo/public/cpp/bindings/bindings_export.h"
16 #include "mojo/public/cpp/bindings/sync_handle_registry.h"
17 
18 namespace mojo {
19 
20 // SyncEventWatcher supports waiting on a base::WaitableEvent to signal while
21 // also allowing other SyncEventWatchers and SyncHandleWatchers on the same
22 // sequence to wake up as needed.
23 //
24 // This class is not thread safe.
25 class MOJO_CPP_BINDINGS_EXPORT SyncEventWatcher {
26  public:
27   SyncEventWatcher(base::WaitableEvent* event, const base::Closure& callback);
28 
29   ~SyncEventWatcher();
30 
31   // Registers |event_| with SyncHandleRegistry, so that when others perform
32   // sync watching on the same sequence, |event_| will be watched along with
33   // them.
34   void AllowWokenUpBySyncWatchOnSameThread();
35 
36   // Waits on |event_| plus all other events and handles registered with this
37   // sequence's SyncHandleRegistry, running callbacks synchronously for any
38   // ready events and handles.
39   //
40   // |stop_flags| is treated as an array of |const bool*| with |num_stop_flags|
41   // entries.
42   //
43   // This method:
44   //   - returns true when any flag in |stop_flags| is set to |true|.
45   //   - return false when any error occurs, including this object being
46   //     destroyed during a callback.
47   bool SyncWatch(const bool** stop_flags, size_t num_stop_flags);
48 
49  private:
50   void IncrementRegisterCount();
51   void DecrementRegisterCount();
52 
53   base::WaitableEvent* const event_;
54   const base::Closure callback_;
55 
56   // Whether |event_| has been registered with SyncHandleRegistry.
57   bool registered_ = false;
58 
59   // If non-zero, |event_| should be registered with SyncHandleRegistry.
60   size_t register_request_count_ = 0;
61 
62   scoped_refptr<SyncHandleRegistry> registry_;
63 
64   scoped_refptr<base::RefCountedData<bool>> destroyed_;
65 
66   SEQUENCE_CHECKER(sequence_checker_);
67 
68   DISALLOW_COPY_AND_ASSIGN(SyncEventWatcher);
69 };
70 
71 }  // namespace mojo
72 
73 #endif  // MOJO_PUBLIC_CPP_BINDINGS_SYNC_EVENT_WATCHER_H_
74