2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "SQLTransactionCoordinator.h"
37 #include "SQLTransaction.h"
38 #include <wtf/Deque.h>
39 #include <wtf/HashMap.h>
40 #include <wtf/HashSet.h>
41 #include <wtf/RefPtr.h>
45 static String getDatabaseIdentifier(SQLTransaction* transaction)
47 Database* database = transaction->database();
49 return database->stringIdentifier();
52 void SQLTransactionCoordinator::processPendingTransactions(CoordinationInfo& info)
54 if (info.activeWriteTransaction || info.pendingTransactions.isEmpty())
57 RefPtr<SQLTransaction> firstPendingTransaction = info.pendingTransactions.first();
58 if (firstPendingTransaction->isReadOnly()) {
60 firstPendingTransaction = info.pendingTransactions.takeFirst();
61 info.activeReadTransactions.add(firstPendingTransaction);
62 firstPendingTransaction->lockAcquired();
63 } while (!info.pendingTransactions.isEmpty() && info.pendingTransactions.first()->isReadOnly());
64 } else if (info.activeReadTransactions.isEmpty()) {
65 info.pendingTransactions.removeFirst();
66 info.activeWriteTransaction = firstPendingTransaction;
67 firstPendingTransaction->lockAcquired();
71 void SQLTransactionCoordinator::acquireLock(SQLTransaction* transaction)
73 String dbIdentifier = getDatabaseIdentifier(transaction);
75 CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
76 if (coordinationInfoIterator == m_coordinationInfoMap.end()) {
77 // No pending transactions for this DB
78 coordinationInfoIterator = m_coordinationInfoMap.add(dbIdentifier, CoordinationInfo()).first;
81 CoordinationInfo& info = coordinationInfoIterator->second;
82 info.pendingTransactions.append(transaction);
83 processPendingTransactions(info);
86 void SQLTransactionCoordinator::releaseLock(SQLTransaction* transaction)
88 if (m_coordinationInfoMap.isEmpty())
91 String dbIdentifier = getDatabaseIdentifier(transaction);
93 CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
94 ASSERT(coordinationInfoIterator != m_coordinationInfoMap.end());
95 CoordinationInfo& info = coordinationInfoIterator->second;
97 if (transaction->isReadOnly()) {
98 ASSERT(info.activeReadTransactions.contains(transaction));
99 info.activeReadTransactions.remove(transaction);
101 ASSERT(info.activeWriteTransaction == transaction);
102 info.activeWriteTransaction = 0;
105 processPendingTransactions(info);
108 void SQLTransactionCoordinator::shutdown()
110 // Notify all transactions in progress that the database thread is shutting down
111 for (CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.begin();
112 coordinationInfoIterator != m_coordinationInfoMap.end(); ++coordinationInfoIterator) {
113 CoordinationInfo& info = coordinationInfoIterator->second;
114 if (info.activeWriteTransaction)
115 info.activeWriteTransaction->notifyDatabaseThreadIsShuttingDown();
116 for (HashSet<RefPtr<SQLTransaction> >::iterator activeReadTransactionsIterator =
117 info.activeReadTransactions.begin();
118 activeReadTransactionsIterator != info.activeReadTransactions.end();
119 ++activeReadTransactionsIterator) {
120 (*activeReadTransactionsIterator)->notifyDatabaseThreadIsShuttingDown();
124 // Clean up all pending transactions for all databases
125 m_coordinationInfoMap.clear();
128 } // namespace WebCore
130 #endif // ENABLE(DATABASE)