dom/indexedDB/TransactionThreadPool.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/TransactionThreadPool.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,182 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozilla_dom_indexeddb_transactionthreadpool_h__
    1.11 +#define mozilla_dom_indexeddb_transactionthreadpool_h__
    1.12 +
    1.13 +// Only meant to be included in IndexedDB source files, not exported.
    1.14 +#include "IndexedDatabase.h"
    1.15 +
    1.16 +#include "nsIObserver.h"
    1.17 +#include "nsIRunnable.h"
    1.18 +
    1.19 +#include "mozilla/Monitor.h"
    1.20 +#include "nsClassHashtable.h"
    1.21 +#include "nsHashKeys.h"
    1.22 +
    1.23 +#include "IDBTransaction.h"
    1.24 +
    1.25 +class nsIThreadPool;
    1.26 +
    1.27 +BEGIN_INDEXEDDB_NAMESPACE
    1.28 +
    1.29 +class FinishTransactionRunnable;
    1.30 +class QueuedDispatchInfo;
    1.31 +
    1.32 +class TransactionThreadPool
    1.33 +{
    1.34 +  friend class nsAutoPtr<TransactionThreadPool>;
    1.35 +  friend class FinishTransactionRunnable;
    1.36 +
    1.37 +public:
    1.38 +  // returns a non-owning ref!
    1.39 +  static TransactionThreadPool* GetOrCreate();
    1.40 +
    1.41 +  // returns a non-owning ref!
    1.42 +  static TransactionThreadPool* Get();
    1.43 +
    1.44 +  static void Shutdown();
    1.45 +
    1.46 +  nsresult Dispatch(IDBTransaction* aTransaction,
    1.47 +                    nsIRunnable* aRunnable,
    1.48 +                    bool aFinish,
    1.49 +                    nsIRunnable* aFinishRunnable);
    1.50 +
    1.51 +  void WaitForDatabasesToComplete(nsTArray<nsRefPtr<IDBDatabase> >& aDatabases,
    1.52 +                                  nsIRunnable* aCallback);
    1.53 +
    1.54 +  // Abort all transactions, unless they are already in the process of being
    1.55 +  // committed, for aDatabase.
    1.56 +  void AbortTransactionsForDatabase(IDBDatabase* aDatabase);
    1.57 +
    1.58 +  // Returns true if there are running or pending transactions for aDatabase.
    1.59 +  bool HasTransactionsForDatabase(IDBDatabase* aDatabase);
    1.60 +
    1.61 +protected:
    1.62 +  class TransactionQueue MOZ_FINAL : public nsIRunnable
    1.63 +  {
    1.64 +  public:
    1.65 +    NS_DECL_THREADSAFE_ISUPPORTS
    1.66 +    NS_DECL_NSIRUNNABLE
    1.67 +
    1.68 +    TransactionQueue(IDBTransaction* aTransaction);
    1.69 +
    1.70 +    void Unblock();
    1.71 +
    1.72 +    void Dispatch(nsIRunnable* aRunnable);
    1.73 +
    1.74 +    void Finish(nsIRunnable* aFinishRunnable);
    1.75 +
    1.76 +  private:
    1.77 +    mozilla::Monitor mMonitor;
    1.78 +    IDBTransaction* mTransaction;
    1.79 +    nsAutoTArray<nsCOMPtr<nsIRunnable>, 10> mQueue;
    1.80 +    nsCOMPtr<nsIRunnable> mFinishRunnable;
    1.81 +    bool mShouldFinish;
    1.82 +  };
    1.83 +
    1.84 +  friend class TransactionQueue;
    1.85 +
    1.86 +  struct TransactionInfo
    1.87 +  {
    1.88 +    TransactionInfo(IDBTransaction* aTransaction)
    1.89 +    {
    1.90 +      MOZ_COUNT_CTOR(TransactionInfo);
    1.91 +
    1.92 +      transaction = aTransaction;
    1.93 +      queue = new TransactionQueue(aTransaction);
    1.94 +    }
    1.95 +
    1.96 +    ~TransactionInfo()
    1.97 +    {
    1.98 +      MOZ_COUNT_DTOR(TransactionInfo);
    1.99 +    }
   1.100 +
   1.101 +    nsRefPtr<IDBTransaction> transaction;
   1.102 +    nsRefPtr<TransactionQueue> queue;
   1.103 +    nsTHashtable<nsPtrHashKey<TransactionInfo> > blockedOn;
   1.104 +    nsTHashtable<nsPtrHashKey<TransactionInfo> > blocking;
   1.105 +  };
   1.106 +
   1.107 +  struct TransactionInfoPair
   1.108 +  {
   1.109 +    TransactionInfoPair()
   1.110 +      : lastBlockingReads(nullptr)
   1.111 +    {
   1.112 +      MOZ_COUNT_CTOR(TransactionInfoPair);
   1.113 +    }
   1.114 +
   1.115 +    ~TransactionInfoPair()
   1.116 +    {
   1.117 +      MOZ_COUNT_DTOR(TransactionInfoPair);
   1.118 +    }
   1.119 +    // Multiple reading transactions can block future writes.
   1.120 +    nsTArray<TransactionInfo*> lastBlockingWrites;
   1.121 +    // But only a single writing transaction can block future reads.
   1.122 +    TransactionInfo* lastBlockingReads;
   1.123 +  };
   1.124 +
   1.125 +  struct DatabaseTransactionInfo
   1.126 +  {
   1.127 +    DatabaseTransactionInfo()
   1.128 +    {
   1.129 +      MOZ_COUNT_CTOR(DatabaseTransactionInfo);
   1.130 +    }
   1.131 +
   1.132 +    ~DatabaseTransactionInfo()
   1.133 +    {
   1.134 +      MOZ_COUNT_DTOR(DatabaseTransactionInfo);
   1.135 +    }
   1.136 +
   1.137 +    typedef nsClassHashtable<nsPtrHashKey<IDBTransaction>, TransactionInfo >
   1.138 +      TransactionHashtable;
   1.139 +    TransactionHashtable transactions;
   1.140 +    nsClassHashtable<nsStringHashKey, TransactionInfoPair> blockingTransactions;
   1.141 +  };
   1.142 +
   1.143 +  static PLDHashOperator
   1.144 +  CollectTransactions(IDBTransaction* aKey,
   1.145 +                      TransactionInfo* aValue,
   1.146 +                      void* aUserArg);
   1.147 +
   1.148 +  static PLDHashOperator
   1.149 +  FindTransaction(IDBTransaction* aKey,
   1.150 +                  TransactionInfo* aValue,
   1.151 +                  void* aUserArg);
   1.152 +
   1.153 +  static PLDHashOperator
   1.154 +  MaybeUnblockTransaction(nsPtrHashKey<TransactionInfo>* aKey,
   1.155 +                          void* aUserArg);
   1.156 +
   1.157 +  struct DatabasesCompleteCallback
   1.158 +  {
   1.159 +    nsTArray<nsRefPtr<IDBDatabase> > mDatabases;
   1.160 +    nsCOMPtr<nsIRunnable> mCallback;
   1.161 +  };
   1.162 +
   1.163 +  TransactionThreadPool();
   1.164 +  ~TransactionThreadPool();
   1.165 +
   1.166 +  nsresult Init();
   1.167 +  nsresult Cleanup();
   1.168 +
   1.169 +  void FinishTransaction(IDBTransaction* aTransaction);
   1.170 +
   1.171 +  TransactionQueue& GetQueueForTransaction(IDBTransaction* aTransaction);
   1.172 +
   1.173 +  bool MaybeFireCallback(DatabasesCompleteCallback aCallback);
   1.174 +
   1.175 +  nsCOMPtr<nsIThreadPool> mThreadPool;
   1.176 +
   1.177 +  nsClassHashtable<nsCStringHashKey, DatabaseTransactionInfo>
   1.178 +    mTransactionsInProgress;
   1.179 +
   1.180 +  nsTArray<DatabasesCompleteCallback> mCompleteCallbacks;
   1.181 +};
   1.182 +
   1.183 +END_INDEXEDDB_NAMESPACE
   1.184 +
   1.185 +#endif // mozilla_dom_indexeddb_transactionthreadpool_h__

mercurial