2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "modules/webdatabase/SQLTransactionCoordinator.h"
35 #include "modules/webdatabase/Database.h"
36 #include "modules/webdatabase/SQLTransactionBackend.h"
40 static String getDatabaseIdentifier(SQLTransactionBackend* transaction)
42 Database* database = transaction->database();
44 return database->stringIdentifier();
47 SQLTransactionCoordinator::SQLTransactionCoordinator()
48 : m_isShuttingDown(false)
52 void SQLTransactionCoordinator::trace(Visitor* visitor)
54 visitor->trace(m_coordinationInfoMap);
57 void SQLTransactionCoordinator::processPendingTransactions(CoordinationInfo& info)
59 if (info.activeWriteTransaction || info.pendingTransactions.isEmpty())
62 SQLTransactionBackend* firstPendingTransaction = info.pendingTransactions.first();
63 if (firstPendingTransaction->isReadOnly()) {
65 firstPendingTransaction = info.pendingTransactions.takeFirst();
66 info.activeReadTransactions.add(firstPendingTransaction);
67 firstPendingTransaction->lockAcquired();
68 } while (!info.pendingTransactions.isEmpty() && info.pendingTransactions.first()->isReadOnly());
69 } else if (info.activeReadTransactions.isEmpty()) {
70 info.pendingTransactions.removeFirst();
71 info.activeWriteTransaction = firstPendingTransaction;
72 firstPendingTransaction->lockAcquired();
76 void SQLTransactionCoordinator::acquireLock(SQLTransactionBackend* transaction)
78 ASSERT(!m_isShuttingDown);
80 String dbIdentifier = getDatabaseIdentifier(transaction);
82 CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
83 if (coordinationInfoIterator == m_coordinationInfoMap.end()) {
84 // No pending transactions for this DB
85 CoordinationInfo& info = m_coordinationInfoMap.add(dbIdentifier, CoordinationInfo()).storedValue->value;
86 info.pendingTransactions.append(transaction);
87 processPendingTransactions(info);
89 CoordinationInfo& info = coordinationInfoIterator->value;
90 info.pendingTransactions.append(transaction);
91 processPendingTransactions(info);
96 void SQLTransactionCoordinator::releaseLock(SQLTransactionBackend* transaction)
101 String dbIdentifier = getDatabaseIdentifier(transaction);
103 CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
104 ASSERT_WITH_SECURITY_IMPLICATION(coordinationInfoIterator != m_coordinationInfoMap.end());
105 CoordinationInfo& info = coordinationInfoIterator->value;
107 if (transaction->isReadOnly()) {
108 ASSERT(info.activeReadTransactions.contains(transaction));
109 info.activeReadTransactions.remove(transaction);
111 ASSERT(info.activeWriteTransaction == transaction);
112 info.activeWriteTransaction = nullptr;
115 processPendingTransactions(info);
118 void SQLTransactionCoordinator::shutdown()
120 // Prevent releaseLock() from accessing / changing the coordinationInfo
121 // while we're shutting down.
122 m_isShuttingDown = true;
124 // Notify all transactions in progress that the database thread is shutting down
125 for (CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.begin();
126 coordinationInfoIterator != m_coordinationInfoMap.end(); ++coordinationInfoIterator) {
127 CoordinationInfo& info = coordinationInfoIterator->value;
129 // Clean up transactions that have reached "lockAcquired":
130 // Transaction phase 4 cleanup. See comment on "What happens if a
131 // transaction is interrupted?" at the top of SQLTransactionBackend.cpp.
132 if (info.activeWriteTransaction)
133 info.activeWriteTransaction->notifyDatabaseThreadIsShuttingDown();
134 for (HeapHashSet<Member<SQLTransactionBackend>>::iterator activeReadTransactionsIterator =
135 info.activeReadTransactions.begin();
136 activeReadTransactionsIterator != info.activeReadTransactions.end();
137 ++activeReadTransactionsIterator) {
138 (*activeReadTransactionsIterator)->notifyDatabaseThreadIsShuttingDown();
141 // Clean up transactions that have NOT reached "lockAcquired":
142 // Transaction phase 3 cleanup. See comment on "What happens if a
143 // transaction is interrupted?" at the top of SQLTransactionBackend.cpp.
144 while (!info.pendingTransactions.isEmpty()) {
145 SQLTransactionBackend* transaction = info.pendingTransactions.takeFirst();
146 transaction->notifyDatabaseThreadIsShuttingDown();
150 // Clean up all pending transactions for all databases
151 m_coordinationInfoMap.clear();