Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: mojo/public/cpp/bindings/sync_handle_registry.h

Issue 2754143005: Use WaitableEvents to wake up sync IPC waiting (Closed)
Patch Set: . Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map>
8 #include <unordered_map> 9 #include <unordered_map>
9 10
10 #include "base/callback.h" 11 #include "base/callback.h"
11 #include "base/macros.h" 12 #include "base/macros.h"
12 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/waitable_event.h"
13 #include "base/threading/thread_checker.h" 15 #include "base/threading/thread_checker.h"
14 #include "mojo/public/cpp/bindings/bindings_export.h" 16 #include "mojo/public/cpp/bindings/bindings_export.h"
15 #include "mojo/public/cpp/system/core.h" 17 #include "mojo/public/cpp/system/core.h"
16 #include "mojo/public/cpp/system/wait_set.h" 18 #include "mojo/public/cpp/system/wait_set.h"
17 19
18 namespace mojo { 20 namespace mojo {
19 21
20 // SyncHandleRegistry is a thread-local storage to register handles that want to 22 // SyncHandleRegistry is a thread-local storage to register handles that want to
21 // be watched together. 23 // be watched together.
22 // 24 //
23 // This class is not thread safe. 25 // This class is not thread safe.
24 class MOJO_CPP_BINDINGS_EXPORT SyncHandleRegistry 26 class MOJO_CPP_BINDINGS_EXPORT SyncHandleRegistry
25 : public base::RefCounted<SyncHandleRegistry> { 27 : public base::RefCounted<SyncHandleRegistry> {
26 public: 28 public:
27 // Returns a thread-local object. 29 // Returns a thread-local object.
28 static scoped_refptr<SyncHandleRegistry> current(); 30 static scoped_refptr<SyncHandleRegistry> current();
29 31
30 using HandleCallback = base::Callback<void(MojoResult)>; 32 using HandleCallback = base::Callback<void(MojoResult)>;
31 bool RegisterHandle(const Handle& handle, 33 bool RegisterHandle(const Handle& handle,
32 MojoHandleSignals handle_signals, 34 MojoHandleSignals handle_signals,
33 const HandleCallback& callback); 35 const HandleCallback& callback);
34 36
35 void UnregisterHandle(const Handle& handle); 37 void UnregisterHandle(const Handle& handle);
36 38
37 // Waits on all the registered handles and runs callbacks synchronously for 39 // Registers a |base::WaitableEvent| which can be used to wake up
38 // those ready handles. 40 // Wait() before any handle signals. |event| is not owned, and if it signals
41 // during Wait(), |callback| is invoked. Returns |true| if registered
42 // successfully or |false| if |event| was already registered.
43 bool RegisterEvent(base::WaitableEvent* event, const base::Closure& callback);
44
45 void UnregisterEvent(base::WaitableEvent* event);
46
47 // Waits on all the registered handles and events and runs callbacks
48 // synchronously for any that become ready.
39 // The method: 49 // The method:
40 // - returns true when any element of |should_stop| is set to true; 50 // - returns true when any element of |should_stop| is set to true;
41 // - returns false when any error occurs. 51 // - returns false when any error occurs.
42 bool WatchAllHandles(const bool* should_stop[], size_t count); 52 bool Wait(const bool* should_stop[], size_t count);
43 53
44 private: 54 private:
45 friend class base::RefCounted<SyncHandleRegistry>; 55 friend class base::RefCounted<SyncHandleRegistry>;
46 56
47 struct HandleHasher {
48 size_t operator()(const Handle& handle) const {
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 SyncHandleRegistry(); 57 SyncHandleRegistry();
55 ~SyncHandleRegistry(); 58 ~SyncHandleRegistry();
56 59
57 HandleMap handles_;
58
59 WaitSet wait_set_; 60 WaitSet wait_set_;
61 std::map<Handle, HandleCallback> handles_;
62 std::map<base::WaitableEvent*, base::Closure> events_;
60 63
61 base::ThreadChecker thread_checker_; 64 base::ThreadChecker thread_checker_;
62 65
63 DISALLOW_COPY_AND_ASSIGN(SyncHandleRegistry); 66 DISALLOW_COPY_AND_ASSIGN(SyncHandleRegistry);
64 }; 67 };
65 68
66 } // namespace mojo 69 } // namespace mojo
67 70
68 #endif // MOJO_PUBLIC_CPP_BINDINGS_SYNC_HANDLE_REGISTRY_H_ 71 #endif // MOJO_PUBLIC_CPP_BINDINGS_SYNC_HANDLE_REGISTRY_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/sync_event_watcher.h ('k') | mojo/public/cpp/system/tests/wait_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698