OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ |
7 | 7 |
8 #include <unordered_map> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/macros.h" | 8 #include "base/macros.h" |
12 #include "base/message_loop/message_loop.h" | 9 #include "base/memory/ref_counted.h" |
13 #include "base/threading/thread_checker.h" | 10 #include "base/threading/thread_checker.h" |
| 11 #include "mojo/public/cpp/bindings/lib/sync_handle_registry.h" |
14 #include "mojo/public/cpp/system/core.h" | 12 #include "mojo/public/cpp/system/core.h" |
15 | 13 |
16 namespace mojo { | 14 namespace mojo { |
17 namespace internal { | 15 namespace internal { |
18 | 16 |
19 // SyncHandleWatcher is used to support sync methods. While a sync call is | 17 // SyncHandleWatcher supports watching a handle synchronously. It also supports |
20 // waiting for response, we would like incoming sync method requests on the same | 18 // registering the handle with a thread-local storage (SyncHandleRegistry), so |
21 // thread to be able to reenter. We also would like master endpoints to continue | 19 // that when other SyncHandleWatcher instances on the same thread perform sync |
22 // dispatching messages for associated endpoints on different threads. | 20 // handle watching, this handle will be watched together. |
23 // Therefore, SyncHandleWatcher is used as thread-local storage to register all | 21 // |
24 // handles that need to be watched while waiting for sync call responses. | 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. |
25 // | 27 // |
26 // This class is not thread safe. | 28 // This class is not thread safe. |
27 class SyncHandleWatcher : public base::MessageLoop::DestructionObserver { | 29 class SyncHandleWatcher { |
28 public: | 30 public: |
29 // Returns a thread-local object. | 31 // Note: |handle| must outlive this object. |
30 static SyncHandleWatcher* current(); | 32 SyncHandleWatcher(const Handle& handle, |
| 33 MojoHandleSignals handle_signals, |
| 34 const SyncHandleRegistry::HandleCallback& callback); |
31 | 35 |
32 using HandleCallback = base::Callback<void(MojoResult)>; | 36 ~SyncHandleWatcher(); |
33 bool RegisterHandle(const Handle& handle, | |
34 MojoHandleSignals handle_signals, | |
35 const HandleCallback& callback); | |
36 | 37 |
37 void UnregisterHandle(const Handle& handle); | 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(); |
38 | 42 |
39 // Waits on all the registered handles and runs callbacks synchronously for | 43 // Waits on |handle_| plus all handles registered with SyncHandleRegistry and |
40 // those ready handles. | 44 // runs callbacks synchronously for those ready handles. |
41 // The method: | 45 // This method: |
42 // - returns true when any element of |should_stop| is set to true; | 46 // - returns true when |should_stop| is set to true; |
43 // - returns false when any error occurs. | 47 // - return false when any error occurs, including this object being |
44 bool WatchAllHandles(const bool* should_stop[], size_t count); | 48 // destroyed during a callback. |
| 49 bool SyncWatch(const bool* should_stop); |
45 | 50 |
46 private: | 51 private: |
47 struct HandleHasher { | 52 void IncrementRegisterCount(); |
48 size_t operator()(const Handle& handle) const { | 53 void DecrementRegisterCount(); |
49 return std::hash<uint32_t>()(static_cast<uint32_t>(handle.value())); | |
50 } | |
51 }; | |
52 using HandleMap = std::unordered_map<Handle, HandleCallback, HandleHasher>; | |
53 | 54 |
54 SyncHandleWatcher(); | 55 const Handle handle_; |
55 ~SyncHandleWatcher() override; | 56 const MojoHandleSignals handle_signals_; |
| 57 SyncHandleRegistry::HandleCallback callback_; |
56 | 58 |
57 // base::MessageLoop::DestructionObserver implementation: | 59 // Whether |handle_| has been registered with SyncHandleRegistry. |
58 void WillDestroyCurrentMessageLoop() override; | 60 bool registered_; |
| 61 // If non-zero, |handle_| should be registered with SyncHandleRegistry. |
| 62 size_t register_request_count_; |
59 | 63 |
60 HandleMap handles_; | 64 scoped_refptr<base::RefCountedData<bool>> destroyed_; |
61 | |
62 ScopedHandle wait_set_handle_; | |
63 | 65 |
64 base::ThreadChecker thread_checker_; | 66 base::ThreadChecker thread_checker_; |
65 | 67 |
66 DISALLOW_COPY_AND_ASSIGN(SyncHandleWatcher); | 68 DISALLOW_COPY_AND_ASSIGN(SyncHandleWatcher); |
67 }; | 69 }; |
68 | 70 |
69 } // namespace internal | 71 } // namespace internal |
70 } // namespace mojo | 72 } // namespace mojo |
71 | 73 |
72 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ | 74 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ |
OLD | NEW |