| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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_LIB_SYNC_HANDLE_WATCHER_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/threading/thread_checker.h" | |
| 11 #include "mojo/public/cpp/bindings/sync_handle_registry.h" | |
| 12 #include "mojo/public/cpp/system/core.h" | |
| 13 | |
| 14 namespace mojo { | |
| 15 namespace internal { | |
| 16 | |
| 17 // SyncHandleWatcher supports watching a handle synchronously. It also supports | |
| 18 // registering the handle with a thread-local storage (SyncHandleRegistry), so | |
| 19 // that when other SyncHandleWatcher instances on the same thread perform sync | |
| 20 // handle watching, this handle will be watched together. | |
| 21 // | |
| 22 // SyncHandleWatcher is used for sync methods. While a sync call is waiting for | |
| 23 // response, we would like to block the thread. On the other hand, we need | |
| 24 // incoming sync method requests on the same thread to be able to reenter. We | |
| 25 // also need master interface endpoints to continue dispatching messages for | |
| 26 // associated endpoints on different threads. | |
| 27 // | |
| 28 // This class is not thread safe. | |
| 29 class SyncHandleWatcher { | |
| 30 public: | |
| 31 // Note: |handle| must outlive this object. | |
| 32 SyncHandleWatcher(const Handle& handle, | |
| 33 MojoHandleSignals handle_signals, | |
| 34 const SyncHandleRegistry::HandleCallback& callback); | |
| 35 | |
| 36 ~SyncHandleWatcher(); | |
| 37 | |
| 38 // Registers |handle_| with SyncHandleRegistry, so that when others perform | |
| 39 // sync handle watching on the same thread, |handle_| will be watched | |
| 40 // together. | |
| 41 void AllowWokenUpBySyncWatchOnSameThread(); | |
| 42 | |
| 43 // Waits on |handle_| plus all handles registered with SyncHandleRegistry and | |
| 44 // runs callbacks synchronously for those ready handles. | |
| 45 // This method: | |
| 46 // - returns true when |should_stop| is set to true; | |
| 47 // - return false when any error occurs, including this object being | |
| 48 // destroyed during a callback. | |
| 49 bool SyncWatch(const bool* should_stop); | |
| 50 | |
| 51 private: | |
| 52 void IncrementRegisterCount(); | |
| 53 void DecrementRegisterCount(); | |
| 54 | |
| 55 const Handle handle_; | |
| 56 const MojoHandleSignals handle_signals_; | |
| 57 SyncHandleRegistry::HandleCallback callback_; | |
| 58 | |
| 59 // Whether |handle_| has been registered with SyncHandleRegistry. | |
| 60 bool registered_; | |
| 61 // If non-zero, |handle_| should be registered with SyncHandleRegistry. | |
| 62 size_t register_request_count_; | |
| 63 | |
| 64 scoped_refptr<SyncHandleRegistry> registry_; | |
| 65 | |
| 66 scoped_refptr<base::RefCountedData<bool>> destroyed_; | |
| 67 | |
| 68 base::ThreadChecker thread_checker_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(SyncHandleWatcher); | |
| 71 }; | |
| 72 | |
| 73 } // namespace internal | |
| 74 } // namespace mojo | |
| 75 | |
| 76 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ | |
| OLD | NEW |