Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_dom_indexeddb_transactionthreadpool_h__ |
michael@0 | 8 | #define mozilla_dom_indexeddb_transactionthreadpool_h__ |
michael@0 | 9 | |
michael@0 | 10 | // Only meant to be included in IndexedDB source files, not exported. |
michael@0 | 11 | #include "IndexedDatabase.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsIObserver.h" |
michael@0 | 14 | #include "nsIRunnable.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "mozilla/Monitor.h" |
michael@0 | 17 | #include "nsClassHashtable.h" |
michael@0 | 18 | #include "nsHashKeys.h" |
michael@0 | 19 | |
michael@0 | 20 | #include "IDBTransaction.h" |
michael@0 | 21 | |
michael@0 | 22 | class nsIThreadPool; |
michael@0 | 23 | |
michael@0 | 24 | BEGIN_INDEXEDDB_NAMESPACE |
michael@0 | 25 | |
michael@0 | 26 | class FinishTransactionRunnable; |
michael@0 | 27 | class QueuedDispatchInfo; |
michael@0 | 28 | |
michael@0 | 29 | class TransactionThreadPool |
michael@0 | 30 | { |
michael@0 | 31 | friend class nsAutoPtr<TransactionThreadPool>; |
michael@0 | 32 | friend class FinishTransactionRunnable; |
michael@0 | 33 | |
michael@0 | 34 | public: |
michael@0 | 35 | // returns a non-owning ref! |
michael@0 | 36 | static TransactionThreadPool* GetOrCreate(); |
michael@0 | 37 | |
michael@0 | 38 | // returns a non-owning ref! |
michael@0 | 39 | static TransactionThreadPool* Get(); |
michael@0 | 40 | |
michael@0 | 41 | static void Shutdown(); |
michael@0 | 42 | |
michael@0 | 43 | nsresult Dispatch(IDBTransaction* aTransaction, |
michael@0 | 44 | nsIRunnable* aRunnable, |
michael@0 | 45 | bool aFinish, |
michael@0 | 46 | nsIRunnable* aFinishRunnable); |
michael@0 | 47 | |
michael@0 | 48 | void WaitForDatabasesToComplete(nsTArray<nsRefPtr<IDBDatabase> >& aDatabases, |
michael@0 | 49 | nsIRunnable* aCallback); |
michael@0 | 50 | |
michael@0 | 51 | // Abort all transactions, unless they are already in the process of being |
michael@0 | 52 | // committed, for aDatabase. |
michael@0 | 53 | void AbortTransactionsForDatabase(IDBDatabase* aDatabase); |
michael@0 | 54 | |
michael@0 | 55 | // Returns true if there are running or pending transactions for aDatabase. |
michael@0 | 56 | bool HasTransactionsForDatabase(IDBDatabase* aDatabase); |
michael@0 | 57 | |
michael@0 | 58 | protected: |
michael@0 | 59 | class TransactionQueue MOZ_FINAL : public nsIRunnable |
michael@0 | 60 | { |
michael@0 | 61 | public: |
michael@0 | 62 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 63 | NS_DECL_NSIRUNNABLE |
michael@0 | 64 | |
michael@0 | 65 | TransactionQueue(IDBTransaction* aTransaction); |
michael@0 | 66 | |
michael@0 | 67 | void Unblock(); |
michael@0 | 68 | |
michael@0 | 69 | void Dispatch(nsIRunnable* aRunnable); |
michael@0 | 70 | |
michael@0 | 71 | void Finish(nsIRunnable* aFinishRunnable); |
michael@0 | 72 | |
michael@0 | 73 | private: |
michael@0 | 74 | mozilla::Monitor mMonitor; |
michael@0 | 75 | IDBTransaction* mTransaction; |
michael@0 | 76 | nsAutoTArray<nsCOMPtr<nsIRunnable>, 10> mQueue; |
michael@0 | 77 | nsCOMPtr<nsIRunnable> mFinishRunnable; |
michael@0 | 78 | bool mShouldFinish; |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | friend class TransactionQueue; |
michael@0 | 82 | |
michael@0 | 83 | struct TransactionInfo |
michael@0 | 84 | { |
michael@0 | 85 | TransactionInfo(IDBTransaction* aTransaction) |
michael@0 | 86 | { |
michael@0 | 87 | MOZ_COUNT_CTOR(TransactionInfo); |
michael@0 | 88 | |
michael@0 | 89 | transaction = aTransaction; |
michael@0 | 90 | queue = new TransactionQueue(aTransaction); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | ~TransactionInfo() |
michael@0 | 94 | { |
michael@0 | 95 | MOZ_COUNT_DTOR(TransactionInfo); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | nsRefPtr<IDBTransaction> transaction; |
michael@0 | 99 | nsRefPtr<TransactionQueue> queue; |
michael@0 | 100 | nsTHashtable<nsPtrHashKey<TransactionInfo> > blockedOn; |
michael@0 | 101 | nsTHashtable<nsPtrHashKey<TransactionInfo> > blocking; |
michael@0 | 102 | }; |
michael@0 | 103 | |
michael@0 | 104 | struct TransactionInfoPair |
michael@0 | 105 | { |
michael@0 | 106 | TransactionInfoPair() |
michael@0 | 107 | : lastBlockingReads(nullptr) |
michael@0 | 108 | { |
michael@0 | 109 | MOZ_COUNT_CTOR(TransactionInfoPair); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | ~TransactionInfoPair() |
michael@0 | 113 | { |
michael@0 | 114 | MOZ_COUNT_DTOR(TransactionInfoPair); |
michael@0 | 115 | } |
michael@0 | 116 | // Multiple reading transactions can block future writes. |
michael@0 | 117 | nsTArray<TransactionInfo*> lastBlockingWrites; |
michael@0 | 118 | // But only a single writing transaction can block future reads. |
michael@0 | 119 | TransactionInfo* lastBlockingReads; |
michael@0 | 120 | }; |
michael@0 | 121 | |
michael@0 | 122 | struct DatabaseTransactionInfo |
michael@0 | 123 | { |
michael@0 | 124 | DatabaseTransactionInfo() |
michael@0 | 125 | { |
michael@0 | 126 | MOZ_COUNT_CTOR(DatabaseTransactionInfo); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | ~DatabaseTransactionInfo() |
michael@0 | 130 | { |
michael@0 | 131 | MOZ_COUNT_DTOR(DatabaseTransactionInfo); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | typedef nsClassHashtable<nsPtrHashKey<IDBTransaction>, TransactionInfo > |
michael@0 | 135 | TransactionHashtable; |
michael@0 | 136 | TransactionHashtable transactions; |
michael@0 | 137 | nsClassHashtable<nsStringHashKey, TransactionInfoPair> blockingTransactions; |
michael@0 | 138 | }; |
michael@0 | 139 | |
michael@0 | 140 | static PLDHashOperator |
michael@0 | 141 | CollectTransactions(IDBTransaction* aKey, |
michael@0 | 142 | TransactionInfo* aValue, |
michael@0 | 143 | void* aUserArg); |
michael@0 | 144 | |
michael@0 | 145 | static PLDHashOperator |
michael@0 | 146 | FindTransaction(IDBTransaction* aKey, |
michael@0 | 147 | TransactionInfo* aValue, |
michael@0 | 148 | void* aUserArg); |
michael@0 | 149 | |
michael@0 | 150 | static PLDHashOperator |
michael@0 | 151 | MaybeUnblockTransaction(nsPtrHashKey<TransactionInfo>* aKey, |
michael@0 | 152 | void* aUserArg); |
michael@0 | 153 | |
michael@0 | 154 | struct DatabasesCompleteCallback |
michael@0 | 155 | { |
michael@0 | 156 | nsTArray<nsRefPtr<IDBDatabase> > mDatabases; |
michael@0 | 157 | nsCOMPtr<nsIRunnable> mCallback; |
michael@0 | 158 | }; |
michael@0 | 159 | |
michael@0 | 160 | TransactionThreadPool(); |
michael@0 | 161 | ~TransactionThreadPool(); |
michael@0 | 162 | |
michael@0 | 163 | nsresult Init(); |
michael@0 | 164 | nsresult Cleanup(); |
michael@0 | 165 | |
michael@0 | 166 | void FinishTransaction(IDBTransaction* aTransaction); |
michael@0 | 167 | |
michael@0 | 168 | TransactionQueue& GetQueueForTransaction(IDBTransaction* aTransaction); |
michael@0 | 169 | |
michael@0 | 170 | bool MaybeFireCallback(DatabasesCompleteCallback aCallback); |
michael@0 | 171 | |
michael@0 | 172 | nsCOMPtr<nsIThreadPool> mThreadPool; |
michael@0 | 173 | |
michael@0 | 174 | nsClassHashtable<nsCStringHashKey, DatabaseTransactionInfo> |
michael@0 | 175 | mTransactionsInProgress; |
michael@0 | 176 | |
michael@0 | 177 | nsTArray<DatabasesCompleteCallback> mCompleteCallbacks; |
michael@0 | 178 | }; |
michael@0 | 179 | |
michael@0 | 180 | END_INDEXEDDB_NAMESPACE |
michael@0 | 181 | |
michael@0 | 182 | #endif // mozilla_dom_indexeddb_transactionthreadpool_h__ |