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

Side by Side Diff: mojo/edk/system/handle_table.h

Issue 1585493002: [mojo] Ports EDK (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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_EDK_SYSTEM_HANDLE_TABLE_H_ 5 #ifndef MOJO_EDK_SYSTEM_HANDLE_TABLE_H_
6 #define MOJO_EDK_SYSTEM_HANDLE_TABLE_H_ 6 #define MOJO_EDK_SYSTEM_HANDLE_TABLE_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <utility>
11 #include <vector> 10 #include <vector>
12 11
13 #include "base/containers/hash_tables.h" 12 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h" 13 #include "base/macros.h"
15 #include "mojo/edk/system/system_impl_export.h" 14 #include "mojo/edk/system/dispatcher.h"
16 #include "mojo/public/c/system/types.h" 15 #include "mojo/public/c/system/types.h"
17 #include "mojo/public/cpp/system/macros.h"
18 16
19 namespace mojo { 17 namespace mojo {
20 namespace edk { 18 namespace edk {
21 19
22 class Core; 20 class HandleTable {
23 class Dispatcher;
24 class DispatcherTransport;
25
26 using DispatcherVector = std::vector<scoped_refptr<Dispatcher>>;
27
28 // Test-only function (defined/used in embedder/test_embedder.cc). Declared here
29 // so it can be friended.
30 namespace internal {
31 bool ShutdownCheckNoLeaks(Core*);
32 }
33
34 // This class provides the (global) handle table (owned by |Core|), which maps
35 // (valid) |MojoHandle|s to |Dispatcher|s. This is abstracted so that, e.g.,
36 // caching may be added.
37 //
38 // This class is NOT thread-safe; locking is left to |Core| (since it may need
39 // to make several changes -- "atomically" or in rapid successsion, in which
40 // case the extra locking/unlocking would be unnecessary overhead).
41
42 class MOJO_SYSTEM_IMPL_EXPORT HandleTable {
43 public: 21 public:
44 HandleTable(); 22 HandleTable();
45 ~HandleTable(); 23 ~HandleTable();
46 24
47 // Gets the dispatcher for a given handle (which should not be 25 MojoHandle AddDispatcher(scoped_refptr<Dispatcher> dispatcher);
48 // |MOJO_HANDLE_INVALID|). Returns null if there's no dispatcher for the given
49 // handle.
50 // WARNING: For efficiency, this returns a dumb pointer. If you're going to
51 // use the result outside |Core|'s lock, you MUST take a reference (e.g., by
52 // storing the result inside a |scoped_refptr|).
53 Dispatcher* GetDispatcher(MojoHandle handle);
54 26
55 // On success, gets the dispatcher for a given handle (which should not be 27 // Inserts multiple dispatchers received from message transit, populating
56 // |MOJO_HANDLE_INVALID|) and removes it. (On failure, returns an appropriate 28 // |handles| with their newly allocated handles. Returns |true| on success.
57 // result (and leaves |dispatcher| alone), namely 29 bool AddDispatchersFromTransit(
58 // |MOJO_RESULT_INVALID_ARGUMENT| if there's no dispatcher for the given 30 const std::vector<Dispatcher::DispatcherInTransit>& dispatchers,
59 // handle or |MOJO_RESULT_BUSY| if the handle is marked as busy.) 31 MojoHandle* handles);
60 MojoResult GetAndRemoveDispatcher(MojoHandle handle, 32
33 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle) const;
34 MojoResult GetAndRemoveDispatcher(MojoHandle,
61 scoped_refptr<Dispatcher>* dispatcher); 35 scoped_refptr<Dispatcher>* dispatcher);
62 36
63 // Adds a dispatcher (which must be valid), returning the handle for it. 37 // Marks handles as busy and populates |dispatchers|. Returns MOJO_RESULT_BUSY
64 // Returns |MOJO_HANDLE_INVALID| on failure (if the handle table is full). 38 // if any of the handles are already in transit; MOJO_RESULT_INVALID_ARGUMENT
65 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); 39 // if any of the handles are invalid; or MOJO_RESULT_OK if successful.
66 40 MojoResult BeginTransit(
67 // Adds a pair of dispatchers (which must be valid), return a pair of handles
68 // for them. On failure (if the handle table is full), the first (and second)
69 // handles will be |MOJO_HANDLE_INVALID|, and neither dispatcher will be
70 // added.
71 std::pair<MojoHandle, MojoHandle> AddDispatcherPair(
72 const scoped_refptr<Dispatcher>& dispatcher0,
73 const scoped_refptr<Dispatcher>& dispatcher1);
74
75 // Adds the given vector of dispatchers (of size at most
76 // |kMaxMessageNumHandles|). |handles| must point to an array of size at least
77 // |dispatchers.size()|. Unlike the other |AddDispatcher...()| functions, some
78 // of the dispatchers may be invalid (null). Returns true on success and false
79 // on failure (if the handle table is full), in which case it leaves
80 // |handles[...]| untouched (and all dispatchers unadded).
81 bool AddDispatcherVector(const DispatcherVector& dispatchers,
82 MojoHandle* handles);
83
84 // Tries to mark the given handles as busy and start transport on them (i.e.,
85 // take their dispatcher locks); |transports| must be sized to contain
86 // |num_handles| elements. On failure, returns them to their original
87 // (non-busy, unlocked state).
88 MojoResult MarkBusyAndStartTransport(
89 MojoHandle disallowed_handle,
90 const MojoHandle* handles, 41 const MojoHandle* handles,
91 uint32_t num_handles, 42 uint32_t num_handles,
92 std::vector<DispatcherTransport>* transports); 43 std::vector<Dispatcher::DispatcherInTransit>* dispatchers);
93 44
94 // Remove the given handles, which must all be present and which should have 45 void CompleteTransitAndClose(
95 // previously been marked busy by |MarkBusyAndStartTransport()|. 46 const std::vector<Dispatcher::DispatcherInTransit>& dispatchers);
96 void RemoveBusyHandles(const MojoHandle* handles, uint32_t num_handles); 47 void CancelTransit(
48 const std::vector<Dispatcher::DispatcherInTransit>& dispatchers);
97 49
98 // Restores the given handles, which must all be present and which should have 50 void GetActiveHandlesForTest(std::vector<MojoHandle> *handles);
99 // previously been marked busy by |MarkBusyAndStartTransport()|, to a non-busy
100 // state.
101 void RestoreBusyHandles(const MojoHandle* handles, uint32_t num_handles);
102 51
103 private: 52 private:
104 friend bool internal::ShutdownCheckNoLeaks(Core*); 53 struct Entry {
54 Entry();
55 explicit Entry(scoped_refptr<Dispatcher> dispatcher);
56 ~Entry();
105 57
106 // The |busy| member is used only to deal with functions (in particular 58 scoped_refptr<Dispatcher> dispatcher;
107 // |Core::WriteMessage()|) that want to hold on to a dispatcher and later 59 bool busy = false;
108 // remove it from the handle table, without holding on to the handle table 60 };
109 // lock.
110 //
111 // For example, if |Core::WriteMessage()| is called with a handle to be sent,
112 // (under the handle table lock) it must first check that that handle is not
113 // busy (if it is busy, then it fails with |MOJO_RESULT_BUSY|) and then marks
114 // it as busy. To avoid deadlock, it should also try to acquire the locks for
115 // all the dispatchers for the handles that it is sending (and fail with
116 // |MOJO_RESULT_BUSY| if the attempt fails). At this point, it can release the
117 // handle table lock.
118 //
119 // If |Core::Close()| is simultaneously called on that handle, it too checks
120 // if the handle is marked busy. If it is, it fails (with |MOJO_RESULT_BUSY|).
121 // This prevents |Core::WriteMessage()| from sending a handle that has been
122 // closed (or learning about this too late).
123 struct Entry {
124 Entry();
125 explicit Entry(const scoped_refptr<Dispatcher>& dispatcher);
126 ~Entry();
127 61
128 scoped_refptr<Dispatcher> dispatcher; 62 using HandleMap = base::hash_map<MojoHandle, Entry>;
129 bool busy;
130 };
131 using HandleToEntryMap = base::hash_map<MojoHandle, Entry>;
132 63
133 // Adds the given dispatcher to the handle table, not doing any size checks. 64 HandleMap handles_;
134 MojoHandle AddDispatcherNoSizeCheck(
135 const scoped_refptr<Dispatcher>& dispatcher);
136 65
137 HandleToEntryMap handle_to_entry_map_; 66 uint32_t next_available_handle_ = 1;
138 MojoHandle next_handle_; // Invariant: never |MOJO_HANDLE_INVALID|.
139 67
140 MOJO_DISALLOW_COPY_AND_ASSIGN(HandleTable); 68 DISALLOW_COPY_AND_ASSIGN(HandleTable);
141 }; 69 };
142 70
143 } // namespace edk 71 } // namespace edk
144 } // namespace mojo 72 } // namespace mojo
145 73
146 #endif // MOJO_EDK_SYSTEM_HANDLE_TABLE_H_ 74 #endif // MOJO_EDK_SYSTEM_HANDLE_TABLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698