| 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_REGISTRY_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_REGISTRY_H_ |
| 7 | 7 |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 14 #include "mojo/public/cpp/system/core.h" | 14 #include "mojo/public/cpp/system/core.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 namespace internal { | 17 namespace internal { |
| 18 | 18 |
| 19 // SyncHandleWatcher is used to support sync methods. While a sync call is | 19 // SyncHandleRegistry is a thread-local storage to register handles that want to |
| 20 // waiting for response, we would like incoming sync method requests on the same | 20 // be watched together. |
| 21 // thread to be able to reenter. We also would like master endpoints to continue | |
| 22 // dispatching messages for associated endpoints on different threads. | |
| 23 // Therefore, SyncHandleWatcher is used as thread-local storage to register all | |
| 24 // handles that need to be watched while waiting for sync call responses. | |
| 25 // | 21 // |
| 26 // This class is not thread safe. | 22 // This class is not thread safe. |
| 27 class SyncHandleWatcher : public base::MessageLoop::DestructionObserver { | 23 class SyncHandleRegistry : public base::MessageLoop::DestructionObserver { |
| 28 public: | 24 public: |
| 29 // Returns a thread-local object. | 25 // Returns a thread-local object. |
| 30 static SyncHandleWatcher* current(); | 26 static SyncHandleRegistry* current(); |
| 31 | 27 |
| 32 using HandleCallback = base::Callback<void(MojoResult)>; | 28 using HandleCallback = base::Callback<void(MojoResult)>; |
| 33 bool RegisterHandle(const Handle& handle, | 29 bool RegisterHandle(const Handle& handle, |
| 34 MojoHandleSignals handle_signals, | 30 MojoHandleSignals handle_signals, |
| 35 const HandleCallback& callback); | 31 const HandleCallback& callback); |
| 36 | 32 |
| 37 void UnregisterHandle(const Handle& handle); | 33 void UnregisterHandle(const Handle& handle); |
| 38 | 34 |
| 39 // Waits on all the registered handles and runs callbacks synchronously for | 35 // Waits on all the registered handles and runs callbacks synchronously for |
| 40 // those ready handles. | 36 // those ready handles. |
| 41 // The method: | 37 // The method: |
| 42 // - returns true when any element of |should_stop| is set to true; | 38 // - returns true when any element of |should_stop| is set to true; |
| 43 // - returns false when any error occurs. | 39 // - returns false when any error occurs. |
| 44 bool WatchAllHandles(const bool* should_stop[], size_t count); | 40 bool WatchAllHandles(const bool* should_stop[], size_t count); |
| 45 | 41 |
| 46 private: | 42 private: |
| 47 struct HandleHasher { | 43 struct HandleHasher { |
| 48 size_t operator()(const Handle& handle) const { | 44 size_t operator()(const Handle& handle) const { |
| 49 return std::hash<uint32_t>()(static_cast<uint32_t>(handle.value())); | 45 return std::hash<uint32_t>()(static_cast<uint32_t>(handle.value())); |
| 50 } | 46 } |
| 51 }; | 47 }; |
| 52 using HandleMap = std::unordered_map<Handle, HandleCallback, HandleHasher>; | 48 using HandleMap = std::unordered_map<Handle, HandleCallback, HandleHasher>; |
| 53 | 49 |
| 54 SyncHandleWatcher(); | 50 SyncHandleRegistry(); |
| 55 ~SyncHandleWatcher() override; | 51 ~SyncHandleRegistry() override; |
| 56 | 52 |
| 57 // base::MessageLoop::DestructionObserver implementation: | 53 // base::MessageLoop::DestructionObserver implementation: |
| 58 void WillDestroyCurrentMessageLoop() override; | 54 void WillDestroyCurrentMessageLoop() override; |
| 59 | 55 |
| 60 HandleMap handles_; | 56 HandleMap handles_; |
| 61 | 57 |
| 62 ScopedHandle wait_set_handle_; | 58 ScopedHandle wait_set_handle_; |
| 63 | 59 |
| 60 scoped_refptr<base::RefCountedData<bool>> destroyed_; |
| 61 |
| 64 base::ThreadChecker thread_checker_; | 62 base::ThreadChecker thread_checker_; |
| 65 | 63 |
| 66 DISALLOW_COPY_AND_ASSIGN(SyncHandleWatcher); | 64 DISALLOW_COPY_AND_ASSIGN(SyncHandleRegistry); |
| 67 }; | 65 }; |
| 68 | 66 |
| 69 } // namespace internal | 67 } // namespace internal |
| 70 } // namespace mojo | 68 } // namespace mojo |
| 71 | 69 |
| 72 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_WATCHER_H_ | 70 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SYNC_HANDLE_REGISTRY_H_ |
| OLD | NEW |