14ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved. 24ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 34ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch// found in the LICENSE file. 44ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 54ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#ifndef MOJO_SYSTEM_HANDLE_TABLE_H_ 64ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#define MOJO_SYSTEM_HANDLE_TABLE_H_ 74ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 84ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#include <utility> 94ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#include <vector> 104ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 114ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#include "base/containers/hash_tables.h" 124ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#include "base/macros.h" 134ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#include "base/memory/ref_counted.h" 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "mojo/public/c/system/types.h" 154ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#include "mojo/system/system_impl_export.h" 164ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 174ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdochnamespace mojo { 184ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdochnamespace system { 194ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 20a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochclass Core; 214ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdochclass Dispatcher; 224ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdochclass DispatcherTransport; 234ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccitypedef std::vector<scoped_refptr<Dispatcher>> DispatcherVector; 25010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 264ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch// Test-only function (defined/used in embedder/test_embedder.cc). Declared here 274ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch// so it can be friended. 284ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdochnamespace internal { 29a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochbool ShutdownCheckNoLeaks(Core*); 304ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch} 314ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 32a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// This class provides the (global) handle table (owned by |Core|), which maps 33a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// (valid) |MojoHandle|s to |Dispatcher|s. This is abstracted so that, e.g., 34a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// caching may be added. 354ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch// 36a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// This class is NOT thread-safe; locking is left to |Core| (since it may need 37a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// to make several changes -- "atomically" or in rapid successsion, in which 38a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// case the extra locking/unlocking would be unnecessary overhead). 394ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 404ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdochclass MOJO_SYSTEM_IMPL_EXPORT HandleTable { 414ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch public: 424ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch HandleTable(); 434ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch ~HandleTable(); 444ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 454ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Gets the dispatcher for a given handle (which should not be 464ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // |MOJO_HANDLE_INVALID|). Returns null if there's no dispatcher for the given 474ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // handle. 484ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // WARNING: For efficiency, this returns a dumb pointer. If you're going to 49a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // use the result outside |Core|'s lock, you MUST take a reference (e.g., by 50a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // storing the result inside a |scoped_refptr|). 514ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Dispatcher* GetDispatcher(MojoHandle handle); 524ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 534ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // On success, gets the dispatcher for a given handle (which should not be 544ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // |MOJO_HANDLE_INVALID|) and removes it. (On failure, returns an appropriate 554ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // result (and leaves |dispatcher| alone), namely 564ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // |MOJO_RESULT_INVALID_ARGUMENT| if there's no dispatcher for the given 574ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // handle or |MOJO_RESULT_BUSY| if the handle is marked as busy.) 584ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch MojoResult GetAndRemoveDispatcher(MojoHandle handle, 594ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch scoped_refptr<Dispatcher>* dispatcher); 604ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 614ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Adds a dispatcher (which must be valid), returning the handle for it. 624ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Returns |MOJO_HANDLE_INVALID| on failure (if the handle table is full). 634ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); 644ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 654ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Adds a pair of dispatchers (which must be valid), return a pair of handles 664ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // for them. On failure (if the handle table is full), the first (and second) 674ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // handles will be |MOJO_HANDLE_INVALID|, and neither dispatcher will be 684ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // added. 694ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch std::pair<MojoHandle, MojoHandle> AddDispatcherPair( 704ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch const scoped_refptr<Dispatcher>& dispatcher0, 714ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch const scoped_refptr<Dispatcher>& dispatcher1); 724ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 734ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Adds the given vector of dispatchers (of size at most 744ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // |kMaxMessageNumHandles|). |handles| must point to an array of size at least 754ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // |dispatchers.size()|. Unlike the other |AddDispatcher...()| functions, some 764ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // of the dispatchers may be invalid (null). Returns true on success and false 774ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // on failure (if the handle table is full), in which case it leaves 784ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // |handles[...]| untouched (and all dispatchers unadded). 79010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) bool AddDispatcherVector(const DispatcherVector& dispatchers, 80010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) MojoHandle* handles); 814ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 824ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Tries to mark the given handles as busy and start transport on them (i.e., 834ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // take their dispatcher locks); |transports| must be sized to contain 844ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // |num_handles| elements. On failure, returns them to their original 854ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // (non-busy, unlocked state). 864ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch MojoResult MarkBusyAndStartTransport( 874ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch MojoHandle disallowed_handle, 884ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch const MojoHandle* handles, 894ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch uint32_t num_handles, 904ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch std::vector<DispatcherTransport>* transports); 914ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 924ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Remove the given handles, which must all be present and which should have 934ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // previously been marked busy by |MarkBusyAndStartTransport()|. 944ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch void RemoveBusyHandles(const MojoHandle* handles, uint32_t num_handles); 954ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 964ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Restores the given handles, which must all be present and which should have 974ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // previously been marked busy by |MarkBusyAndStartTransport()|, to a non-busy 984ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // state. 994ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch void RestoreBusyHandles(const MojoHandle* handles, uint32_t num_handles); 1004ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 1014ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch private: 102a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch friend bool internal::ShutdownCheckNoLeaks(Core*); 1034ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // The |busy| member is used only to deal with functions (in particular 105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // |Core::WriteMessage()|) that want to hold on to a dispatcher and later 106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // remove it from the handle table, without holding on to the handle table 107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // lock. 108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // 109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // For example, if |Core::WriteMessage()| is called with a handle to be sent, 110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // (under the handle table lock) it must first check that that handle is not 111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // busy (if it is busy, then it fails with |MOJO_RESULT_BUSY|) and then marks 112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // it as busy. To avoid deadlock, it should also try to acquire the locks for 113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // all the dispatchers for the handles that it is sending (and fail with 114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // |MOJO_RESULT_BUSY| if the attempt fails). At this point, it can release the 115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // handle table lock. 116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // 117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // If |Core::Close()| is simultaneously called on that handle, it too checks 118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // if the handle is marked busy. If it is, it fails (with |MOJO_RESULT_BUSY|). 119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // This prevents |Core::WriteMessage()| from sending a handle that has been 120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // closed (or learning about this too late). 1214ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch struct Entry { 1224ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch Entry(); 1234ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch explicit Entry(const scoped_refptr<Dispatcher>& dispatcher); 1244ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch ~Entry(); 1254ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 1264ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch scoped_refptr<Dispatcher> dispatcher; 1274ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch bool busy; 1284ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch }; 1294ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch typedef base::hash_map<MojoHandle, Entry> HandleToEntryMap; 1304ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 1314ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch // Adds the given dispatcher to the handle table, not doing any size checks. 1324ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch MojoHandle AddDispatcherNoSizeCheck( 1334ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch const scoped_refptr<Dispatcher>& dispatcher); 1344ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 1354ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch HandleToEntryMap handle_to_entry_map_; 1364ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch MojoHandle next_handle_; // Invariant: never |MOJO_HANDLE_INVALID|. 1374ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 1384ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch DISALLOW_COPY_AND_ASSIGN(HandleTable); 1394ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch}; 1404ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 1414ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch} // namespace system 1424ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch} // namespace mojo 1434ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch 1444ad1aa43a48567659193a298fad74f55e00b3dd9Ben Murdoch#endif // MOJO_SYSTEM_HANDLE_TABLE_H_ 145