rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 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_SYNC_HANDLE_REGISTRY_H_ |
| 6 | #define MOJO_PUBLIC_CPP_BINDINGS_SYNC_HANDLE_REGISTRY_H_ |
| 7 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 8 | #include <map> |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 9 | |
| 10 | #include "base/callback.h" |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 11 | #include "base/containers/stack_container.h" |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 12 | #include "base/macros.h" |
| 13 | #include "base/memory/ref_counted.h" |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 15 | #include "base/synchronization/waitable_event.h" |
dcheng | 9c3e658 | 2016-09-23 03:10:50 | [diff] [blame] | 16 | #include "mojo/public/cpp/bindings/bindings_export.h" |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 17 | #include "mojo/public/cpp/system/core.h" |
rockot | a9d566a | 2017-03-17 19:36:15 | [diff] [blame] | 18 | #include "mojo/public/cpp/system/wait_set.h" |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 19 | |
| 20 | namespace mojo { |
| 21 | |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 22 | // SyncHandleRegistry is a sequence-local storage to register handles that want |
| 23 | // to be watched together. |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 24 | // |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 25 | // This class is thread unsafe. |
dcheng | 9c3e658 | 2016-09-23 03:10:50 | [diff] [blame] | 26 | class MOJO_CPP_BINDINGS_EXPORT SyncHandleRegistry |
| 27 | : public base::RefCounted<SyncHandleRegistry> { |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 28 | public: |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 29 | // Returns a sequence-local object. |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 30 | static scoped_refptr<SyncHandleRegistry> current(); |
| 31 | |
| 32 | using HandleCallback = base::Callback<void(MojoResult)>; |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 33 | |
| 34 | // Registers a |Handle| to be watched for |handle_signals|. If any such |
| 35 | // signals are satisfied during a Wait(), the Wait() is woken up and |
| 36 | // |callback| is run. |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 37 | bool RegisterHandle(const Handle& handle, |
| 38 | MojoHandleSignals handle_signals, |
| 39 | const HandleCallback& callback); |
| 40 | |
| 41 | void UnregisterHandle(const Handle& handle); |
| 42 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 43 | // Registers a |base::WaitableEvent| which can be used to wake up |
| 44 | // Wait() before any handle signals. |event| is not owned, and if it signals |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 45 | // during Wait(), |callback| is invoked. Note that |event| may be registered |
| 46 | // multiple times with different callbacks. |
| 47 | void RegisterEvent(base::WaitableEvent* event, const base::Closure& callback); |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 48 | |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 49 | // Unregisters a specific |event|+|callback| pair. |
| 50 | void UnregisterEvent(base::WaitableEvent* event, |
| 51 | const base::Closure& callback); |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 52 | |
| 53 | // Waits on all the registered handles and events and runs callbacks |
| 54 | // synchronously for any that become ready. |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 55 | // The method: |
| 56 | // - returns true when any element of |should_stop| is set to true; |
| 57 | // - returns false when any error occurs. |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 58 | bool Wait(const bool* should_stop[], size_t count); |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | friend class base::RefCounted<SyncHandleRegistry>; |
| 62 | |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 63 | using EventCallbackList = base::StackVector<base::Closure, 1>; |
| 64 | using EventMap = std::map<base::WaitableEvent*, EventCallbackList>; |
| 65 | |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 66 | SyncHandleRegistry(); |
| 67 | ~SyncHandleRegistry(); |
| 68 | |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 69 | void RemoveInvalidEventCallbacks(); |
| 70 | |
rockot | a9d566a | 2017-03-17 19:36:15 | [diff] [blame] | 71 | WaitSet wait_set_; |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 72 | std::map<Handle, HandleCallback> handles_; |
Ken Rockot | 267df5a | 2017-08-30 23:31:45 | [diff] [blame] | 73 | EventMap events_; |
| 74 | |
| 75 | // |true| iff this registry is currently dispatching event callbacks in |
| 76 | // Wait(). Used to allow for safe event registration/unregistration from event |
| 77 | // callbacks. |
| 78 | bool is_dispatching_event_callbacks_ = false; |
| 79 | |
| 80 | // Indicates if one or more event callbacks was unregistered during the most |
| 81 | // recent event callback dispatch. |
| 82 | bool remove_invalid_event_callbacks_after_dispatch_ = false; |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 83 | |
Sam McNally | de5ae67 | 2017-06-19 23:34:45 | [diff] [blame] | 84 | SEQUENCE_CHECKER(sequence_checker_); |
rockot | 6d7be62 | 2016-06-15 18:25:19 | [diff] [blame] | 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(SyncHandleRegistry); |
| 87 | }; |
| 88 | |
| 89 | } // namespace mojo |
| 90 | |
| 91 | #endif // MOJO_PUBLIC_CPP_BINDINGS_SYNC_HANDLE_REGISTRY_H_ |