1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 et sw=2 tw=80: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Indexed Database.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * The Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2010
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Ben Turner <bent.mozilla@gmail.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozilla_dom_indexeddb_transactionthreadpool_h__
41 : #define mozilla_dom_indexeddb_transactionthreadpool_h__
42 :
43 : // Only meant to be included in IndexedDB source files, not exported.
44 : #include "IndexedDatabase.h"
45 :
46 : #include "nsIObserver.h"
47 : #include "nsIRunnable.h"
48 :
49 : #include "mozilla/Monitor.h"
50 : #include "nsClassHashtable.h"
51 : #include "nsHashKeys.h"
52 : #include "nsRefPtrHashtable.h"
53 :
54 : #include "IDBTransaction.h"
55 :
56 : class nsIThreadPool;
57 :
58 : BEGIN_INDEXEDDB_NAMESPACE
59 :
60 : class FinishTransactionRunnable;
61 : class QueuedDispatchInfo;
62 :
63 : class TransactionThreadPool
64 : {
65 : friend class nsAutoPtr<TransactionThreadPool>;
66 : friend class FinishTransactionRunnable;
67 :
68 : public:
69 : // returns a non-owning ref!
70 : static TransactionThreadPool* GetOrCreate();
71 :
72 : // returns a non-owning ref!
73 : static TransactionThreadPool* Get();
74 :
75 : static void Shutdown();
76 :
77 : nsresult Dispatch(IDBTransaction* aTransaction,
78 : nsIRunnable* aRunnable,
79 : bool aFinish,
80 : nsIRunnable* aFinishRunnable);
81 :
82 : bool WaitForAllDatabasesToComplete(
83 : nsTArray<nsRefPtr<IDBDatabase> >& aDatabases,
84 : nsIRunnable* aCallback);
85 :
86 : // Abort all transactions, unless they are already in the process of being
87 : // committed, for aDatabase.
88 : void AbortTransactionsForDatabase(IDBDatabase* aDatabase);
89 :
90 : // Returns true if there are running or pending transactions for aDatabase.
91 : bool HasTransactionsForDatabase(IDBDatabase* aDatabase);
92 :
93 : protected:
94 : class TransactionQueue : public nsIRunnable
95 540 : {
96 : public:
97 : NS_DECL_ISUPPORTS
98 : NS_DECL_NSIRUNNABLE
99 :
100 : inline TransactionQueue(IDBTransaction* aTransaction,
101 : nsIRunnable* aRunnable);
102 :
103 : inline void Dispatch(nsIRunnable* aRunnable);
104 :
105 : inline void Finish(nsIRunnable* aFinishRunnable);
106 :
107 : private:
108 : mozilla::Monitor mMonitor;
109 : IDBTransaction* mTransaction;
110 : nsAutoTArray<nsCOMPtr<nsIRunnable>, 10> mQueue;
111 : nsCOMPtr<nsIRunnable> mFinishRunnable;
112 : bool mShouldFinish;
113 : };
114 :
115 : struct TransactionInfo
116 1080 : {
117 : nsRefPtr<IDBTransaction> transaction;
118 : nsRefPtr<TransactionQueue> queue;
119 : nsTArray<nsString> objectStoreNames;
120 : };
121 :
122 : struct DatabaseTransactionInfo
123 808 : {
124 : nsTArray<TransactionInfo> transactions;
125 : nsTArray<nsString> storesReading;
126 : nsTArray<nsString> storesWriting;
127 : };
128 :
129 : struct QueuedDispatchInfo
130 1133 : {
131 1133 : QueuedDispatchInfo()
132 1133 : : finish(false)
133 1133 : { }
134 :
135 : nsRefPtr<IDBTransaction> transaction;
136 : nsCOMPtr<nsIRunnable> runnable;
137 : nsCOMPtr<nsIRunnable> finishRunnable;
138 : bool finish;
139 : };
140 :
141 : struct DatabasesCompleteCallback
142 6 : {
143 : nsTArray<nsRefPtr<IDBDatabase> > mDatabases;
144 : nsCOMPtr<nsIRunnable> mCallback;
145 : };
146 :
147 : TransactionThreadPool();
148 : ~TransactionThreadPool();
149 :
150 : nsresult Init();
151 : nsresult Cleanup();
152 :
153 : void FinishTransaction(IDBTransaction* aTransaction);
154 :
155 : nsresult TransactionCanRun(IDBTransaction* aTransaction,
156 : bool* aCanRun,
157 : TransactionQueue** aExistingQueue);
158 :
159 1133 : nsresult Dispatch(const QueuedDispatchInfo& aInfo)
160 : {
161 : return Dispatch(aInfo.transaction, aInfo.runnable, aInfo.finish,
162 1133 : aInfo.finishRunnable);
163 : }
164 :
165 : void MaybeFireCallback(PRUint32 aCallbackIndex);
166 :
167 : nsCOMPtr<nsIThreadPool> mThreadPool;
168 :
169 : nsClassHashtable<nsISupportsHashKey, DatabaseTransactionInfo>
170 : mTransactionsInProgress;
171 :
172 : nsTArray<QueuedDispatchInfo> mDelayedDispatchQueue;
173 :
174 : nsTArray<DatabasesCompleteCallback> mCompleteCallbacks;
175 : };
176 :
177 : END_INDEXEDDB_NAMESPACE
178 :
179 : #endif // mozilla_dom_indexeddb_transactionthreadpool_h__
|