1 /* 2 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * Copyright (C) 2013 Apple Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 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 14 * distribution. 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. 18 * 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. 30 */ 31 32 #include "config.h" 33 #include "modules/webdatabase/SQLTransactionCoordinator.h" 34 35 #include "modules/webdatabase/DatabaseBackend.h" 36 #include "modules/webdatabase/SQLTransactionBackend.h" 37 #include "wtf/Deque.h" 38 #include "wtf/HashMap.h" 39 #include "wtf/HashSet.h" 40 #include "wtf/RefPtr.h" 41 42 namespace WebCore { 43 44 static String getDatabaseIdentifier(SQLTransactionBackend* transaction) 45 { 46 DatabaseBackend* database = transaction->database(); 47 ASSERT(database); 48 return database->stringIdentifier(); 49 } 50 51 SQLTransactionCoordinator::SQLTransactionCoordinator() 52 : m_isShuttingDown(false) 53 { 54 } 55 56 void SQLTransactionCoordinator::processPendingTransactions(CoordinationInfo& info) 57 { 58 if (info.activeWriteTransaction || info.pendingTransactions.isEmpty()) 59 return; 60 61 RefPtr<SQLTransactionBackend> firstPendingTransaction = info.pendingTransactions.first(); 62 if (firstPendingTransaction->isReadOnly()) { 63 do { 64 firstPendingTransaction = info.pendingTransactions.takeFirst(); 65 info.activeReadTransactions.add(firstPendingTransaction); 66 firstPendingTransaction->lockAcquired(); 67 } while (!info.pendingTransactions.isEmpty() && info.pendingTransactions.first()->isReadOnly()); 68 } else if (info.activeReadTransactions.isEmpty()) { 69 info.pendingTransactions.removeFirst(); 70 info.activeWriteTransaction = firstPendingTransaction; 71 firstPendingTransaction->lockAcquired(); 72 } 73 } 74 75 void SQLTransactionCoordinator::acquireLock(SQLTransactionBackend* transaction) 76 { 77 ASSERT(!m_isShuttingDown); 78 79 String dbIdentifier = getDatabaseIdentifier(transaction); 80 81 CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier); 82 if (coordinationInfoIterator == m_coordinationInfoMap.end()) { 83 // No pending transactions for this DB 84 coordinationInfoIterator = m_coordinationInfoMap.add(dbIdentifier, CoordinationInfo()).iterator; 85 } 86 87 CoordinationInfo& info = coordinationInfoIterator->value; 88 info.pendingTransactions.append(transaction); 89 processPendingTransactions(info); 90 } 91 92 void SQLTransactionCoordinator::releaseLock(SQLTransactionBackend* transaction) 93 { 94 if (m_isShuttingDown) 95 return; 96 97 String dbIdentifier = getDatabaseIdentifier(transaction); 98 99 CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier); 100 ASSERT(coordinationInfoIterator != m_coordinationInfoMap.end()); 101 CoordinationInfo& info = coordinationInfoIterator->value; 102 103 if (transaction->isReadOnly()) { 104 ASSERT(info.activeReadTransactions.contains(transaction)); 105 info.activeReadTransactions.remove(transaction); 106 } else { 107 ASSERT(info.activeWriteTransaction == transaction); 108 info.activeWriteTransaction = 0; 109 } 110 111 processPendingTransactions(info); 112 } 113 114 void SQLTransactionCoordinator::shutdown() 115 { 116 // Prevent releaseLock() from accessing / changing the coordinationInfo 117 // while we're shutting down. 118 m_isShuttingDown = true; 119 120 // Notify all transactions in progress that the database thread is shutting down 121 for (CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.begin(); 122 coordinationInfoIterator != m_coordinationInfoMap.end(); ++coordinationInfoIterator) { 123 CoordinationInfo& info = coordinationInfoIterator->value; 124 125 // Clean up transactions that have reached "lockAcquired": 126 // Transaction phase 4 cleanup. See comment on "What happens if a 127 // transaction is interrupted?" at the top of SQLTransactionBackend.cpp. 128 if (info.activeWriteTransaction) 129 info.activeWriteTransaction->notifyDatabaseThreadIsShuttingDown(); 130 for (HashSet<RefPtr<SQLTransactionBackend> >::iterator activeReadTransactionsIterator = 131 info.activeReadTransactions.begin(); 132 activeReadTransactionsIterator != info.activeReadTransactions.end(); 133 ++activeReadTransactionsIterator) { 134 (*activeReadTransactionsIterator)->notifyDatabaseThreadIsShuttingDown(); 135 } 136 137 // Clean up transactions that have NOT reached "lockAcquired": 138 // Transaction phase 3 cleanup. See comment on "What happens if a 139 // transaction is interrupted?" at the top of SQLTransactionBackend.cpp. 140 while (!info.pendingTransactions.isEmpty()) { 141 RefPtr<SQLTransactionBackend> transaction = info.pendingTransactions.first(); 142 transaction->notifyDatabaseThreadIsShuttingDown(); 143 } 144 } 145 146 // Clean up all pending transactions for all databases 147 m_coordinationInfoMap.clear(); 148 } 149 150 } // namespace WebCore 151