| 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_SYNC_HANDLE_REGISTRY_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_SYNC_HANDLE_REGISTRY_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_SYNC_HANDLE_REGISTRY_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_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/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/threading/thread_checker.h" | 13 #include "base/sequence_checker.h" |
| 14 #include "mojo/public/cpp/bindings/bindings_export.h" | 14 #include "mojo/public/cpp/bindings/bindings_export.h" |
| 15 #include "mojo/public/cpp/system/core.h" | 15 #include "mojo/public/cpp/system/core.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace mojo { |
| 18 | 18 |
| 19 // SyncHandleRegistry is a thread-local storage to register handles that want to | 19 // SyncHandleRegistry is a sequence-local storage to register handles that want |
| 20 // be watched together. | 20 // to be watched together. |
| 21 // | 21 // |
| 22 // This class is not thread safe. | 22 // This class is not thread safe. |
| 23 class MOJO_CPP_BINDINGS_EXPORT SyncHandleRegistry | 23 class MOJO_CPP_BINDINGS_EXPORT SyncHandleRegistry |
| 24 : public base::RefCounted<SyncHandleRegistry> { | 24 : public base::RefCountedThreadSafe<SyncHandleRegistry> { |
| 25 public: | 25 public: |
| 26 // Returns a thread-local object. | 26 // Returns a sequence-local object. |
| 27 static scoped_refptr<SyncHandleRegistry> current(); | 27 static scoped_refptr<SyncHandleRegistry> current(); |
| 28 | 28 |
| 29 using HandleCallback = base::Callback<void(MojoResult)>; | 29 using HandleCallback = base::Callback<void(MojoResult)>; |
| 30 bool RegisterHandle(const Handle& handle, | 30 bool RegisterHandle(const Handle& handle, |
| 31 MojoHandleSignals handle_signals, | 31 MojoHandleSignals handle_signals, |
| 32 const HandleCallback& callback); | 32 const HandleCallback& callback); |
| 33 | 33 |
| 34 void UnregisterHandle(const Handle& handle); | 34 void UnregisterHandle(const Handle& handle); |
| 35 | 35 |
| 36 // Waits on all the registered handles and runs callbacks synchronously for | 36 // Waits on all the registered handles and runs callbacks synchronously for |
| 37 // those ready handles. | 37 // those ready handles. |
| 38 // The method: | 38 // The method: |
| 39 // - returns true when any element of |should_stop| is set to true; | 39 // - returns true when any element of |should_stop| is set to true; |
| 40 // - returns false when any error occurs. | 40 // - returns false when any error occurs. |
| 41 bool WatchAllHandles(const bool* should_stop[], size_t count); | 41 bool WatchAllHandles(const bool* should_stop[], size_t count); |
| 42 | 42 |
| 43 private: | 43 private: |
| 44 friend class base::RefCounted<SyncHandleRegistry>; | 44 friend class base::RefCountedThreadSafe<SyncHandleRegistry>; |
| 45 | 45 |
| 46 struct HandleHasher { | 46 struct HandleHasher { |
| 47 size_t operator()(const Handle& handle) const { | 47 size_t operator()(const Handle& handle) const { |
| 48 return std::hash<uint32_t>()(static_cast<uint32_t>(handle.value())); | 48 return std::hash<uint32_t>()(static_cast<uint32_t>(handle.value())); |
| 49 } | 49 } |
| 50 }; | 50 }; |
| 51 using HandleMap = std::unordered_map<Handle, HandleCallback, HandleHasher>; | 51 using HandleMap = std::unordered_map<Handle, HandleCallback, HandleHasher>; |
| 52 | 52 |
| 53 SyncHandleRegistry(); | 53 SyncHandleRegistry(); |
| 54 ~SyncHandleRegistry(); | 54 ~SyncHandleRegistry(); |
| 55 | 55 |
| 56 HandleMap handles_; | 56 HandleMap handles_; |
| 57 | 57 |
| 58 ScopedHandle wait_set_handle_; | 58 ScopedHandle wait_set_handle_; |
| 59 | 59 |
| 60 base::ThreadChecker thread_checker_; | 60 base::SequenceChecker sequence_checker_; |
| 61 | 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(SyncHandleRegistry); | 62 DISALLOW_COPY_AND_ASSIGN(SyncHandleRegistry); |
| 63 }; | 63 }; |
| 64 | 64 |
| 65 } // namespace mojo | 65 } // namespace mojo |
| 66 | 66 |
| 67 #endif // MOJO_PUBLIC_CPP_BINDINGS_SYNC_HANDLE_REGISTRY_H_ | 67 #endif // MOJO_PUBLIC_CPP_BINDINGS_SYNC_HANDLE_REGISTRY_H_ |
| OLD | NEW |