OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_SYSTEM_HANDLE_TABLE_H_ |
| 6 #define MOJO_SYSTEM_HANDLE_TABLE_H_ |
| 7 |
| 8 #include <utility> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/containers/hash_tables.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "mojo/public/c/system/core.h" |
| 15 #include "mojo/system/system_impl_export.h" |
| 16 |
| 17 namespace mojo { |
| 18 namespace system { |
| 19 |
| 20 class CoreImpl; |
| 21 class Dispatcher; |
| 22 class DispatcherTransport; |
| 23 |
| 24 // Test-only function (defined/used in embedder/test_embedder.cc). Declared here |
| 25 // so it can be friended. |
| 26 namespace internal { |
| 27 bool ShutdownCheckNoLeaks(CoreImpl*); |
| 28 } |
| 29 |
| 30 // This class provides the (global) handle table (owned by |CoreImpl|), which |
| 31 // maps (valid) |MojoHandle|s to |Dispatcher|s. This is abstracted so that, |
| 32 // e.g., caching may be added. |
| 33 // |
| 34 // This class is NOT thread-safe; locking is left to |CoreImpl| (since it may |
| 35 // need to make several changes -- "atomically" or in rapid successsion, in |
| 36 // which case the extra locking/unlocking would be unnecessary overhead). |
| 37 |
| 38 class MOJO_SYSTEM_IMPL_EXPORT HandleTable { |
| 39 public: |
| 40 HandleTable(); |
| 41 ~HandleTable(); |
| 42 |
| 43 // Gets the dispatcher for a given handle (which should not be |
| 44 // |MOJO_HANDLE_INVALID|). Returns null if there's no dispatcher for the given |
| 45 // handle. |
| 46 // WARNING: For efficiency, this returns a dumb pointer. If you're going to |
| 47 // use the result outside |CoreImpl|'s lock, you MUST take a reference (e.g., |
| 48 // by storing the result inside a |scoped_refptr|). |
| 49 Dispatcher* GetDispatcher(MojoHandle handle); |
| 50 |
| 51 // On success, gets the dispatcher for a given handle (which should not be |
| 52 // |MOJO_HANDLE_INVALID|) and removes it. (On failure, returns an appropriate |
| 53 // result (and leaves |dispatcher| alone), namely |
| 54 // |MOJO_RESULT_INVALID_ARGUMENT| if there's no dispatcher for the given |
| 55 // handle or |MOJO_RESULT_BUSY| if the handle is marked as busy.) |
| 56 MojoResult GetAndRemoveDispatcher(MojoHandle handle, |
| 57 scoped_refptr<Dispatcher>* dispatcher); |
| 58 |
| 59 // Adds a dispatcher (which must be valid), returning the handle for it. |
| 60 // Returns |MOJO_HANDLE_INVALID| on failure (if the handle table is full). |
| 61 MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); |
| 62 |
| 63 // Adds a pair of dispatchers (which must be valid), return a pair of handles |
| 64 // for them. On failure (if the handle table is full), the first (and second) |
| 65 // handles will be |MOJO_HANDLE_INVALID|, and neither dispatcher will be |
| 66 // added. |
| 67 std::pair<MojoHandle, MojoHandle> AddDispatcherPair( |
| 68 const scoped_refptr<Dispatcher>& dispatcher0, |
| 69 const scoped_refptr<Dispatcher>& dispatcher1); |
| 70 |
| 71 // Adds the given vector of dispatchers (of size at most |
| 72 // |kMaxMessageNumHandles|). |handles| must point to an array of size at least |
| 73 // |dispatchers.size()|. Unlike the other |AddDispatcher...()| functions, some |
| 74 // of the dispatchers may be invalid (null). Returns true on success and false |
| 75 // on failure (if the handle table is full), in which case it leaves |
| 76 // |handles[...]| untouched (and all dispatchers unadded). |
| 77 bool AddDispatcherVector( |
| 78 const std::vector<scoped_refptr<Dispatcher> >& dispatchers, |
| 79 MojoHandle* handles); |
| 80 |
| 81 // Tries to mark the given handles as busy and start transport on them (i.e., |
| 82 // take their dispatcher locks); |transports| must be sized to contain |
| 83 // |num_handles| elements. On failure, returns them to their original |
| 84 // (non-busy, unlocked state). |
| 85 MojoResult MarkBusyAndStartTransport( |
| 86 MojoHandle disallowed_handle, |
| 87 const MojoHandle* handles, |
| 88 uint32_t num_handles, |
| 89 std::vector<DispatcherTransport>* transports); |
| 90 |
| 91 // Remove the given handles, which must all be present and which should have |
| 92 // previously been marked busy by |MarkBusyAndStartTransport()|. |
| 93 void RemoveBusyHandles(const MojoHandle* handles, uint32_t num_handles); |
| 94 |
| 95 // Restores the given handles, which must all be present and which should have |
| 96 // previously been marked busy by |MarkBusyAndStartTransport()|, to a non-busy |
| 97 // state. |
| 98 void RestoreBusyHandles(const MojoHandle* handles, uint32_t num_handles); |
| 99 |
| 100 private: |
| 101 friend bool internal::ShutdownCheckNoLeaks(CoreImpl*); |
| 102 |
| 103 struct Entry { |
| 104 Entry(); |
| 105 explicit Entry(const scoped_refptr<Dispatcher>& dispatcher); |
| 106 ~Entry(); |
| 107 |
| 108 scoped_refptr<Dispatcher> dispatcher; |
| 109 bool busy; |
| 110 }; |
| 111 typedef base::hash_map<MojoHandle, Entry> HandleToEntryMap; |
| 112 |
| 113 // Adds the given dispatcher to the handle table, not doing any size checks. |
| 114 MojoHandle AddDispatcherNoSizeCheck( |
| 115 const scoped_refptr<Dispatcher>& dispatcher); |
| 116 |
| 117 HandleToEntryMap handle_to_entry_map_; |
| 118 MojoHandle next_handle_; // Invariant: never |MOJO_HANDLE_INVALID|. |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(HandleTable); |
| 121 }; |
| 122 |
| 123 } // namespace system |
| 124 } // namespace mojo |
| 125 |
| 126 #endif // MOJO_SYSTEM_HANDLE_TABLE_H_ |
OLD | NEW |