OLD | NEW |
(Empty) | |
| 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/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread_checker.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 // thread 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 thread, |event_| will be watched along with them. |
| 33 void AllowWokenUpBySyncWatchOnSameThread(); |
| 34 |
| 35 // Waits on |event_| plus all other events and handles registered with this |
| 36 // thread's SyncHandleRegistry, running callbacks synchronously for any ready |
| 37 // events and handles. |
| 38 // This method: |
| 39 // - returns true when |should_stop| is set to true; |
| 40 // - return false when any error occurs, including this object being |
| 41 // destroyed during a callback. |
| 42 bool SyncWatch(const bool* should_stop); |
| 43 |
| 44 private: |
| 45 void IncrementRegisterCount(); |
| 46 void DecrementRegisterCount(); |
| 47 |
| 48 base::WaitableEvent* const event_; |
| 49 const base::Closure callback_; |
| 50 |
| 51 // Whether |event_| has been registered with SyncHandleRegistry. |
| 52 bool registered_ = false; |
| 53 |
| 54 // If non-zero, |event_| should be registered with SyncHandleRegistry. |
| 55 size_t register_request_count_ = 0; |
| 56 |
| 57 scoped_refptr<SyncHandleRegistry> registry_; |
| 58 |
| 59 scoped_refptr<base::RefCountedData<bool>> destroyed_; |
| 60 |
| 61 base::ThreadChecker thread_checker_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(SyncEventWatcher); |
| 64 }; |
| 65 |
| 66 } // namespace mojo |
| 67 |
| 68 #endif // MOJO_PUBLIC_CPP_BINDINGS_SYNC_EVENT_WATCHER_H_ |
OLD | NEW |