1 /* 2 * Copyright (C) 2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31 #include "config.h" 32 #include "SQLTransactionCoordinator.h" 33 34 #if ENABLE(DATABASE) 35 36 #include "CString.h" 37 #include "Database.h" 38 #include "SQLTransaction.h" 39 #include <wtf/Deque.h> 40 #include <wtf/HashMap.h> 41 #include <wtf/HashSet.h> 42 #include <wtf/RefPtr.h> 43 44 namespace WebCore { 45 46 static String getDatabaseIdentifier(SQLTransaction* transaction) 47 { 48 Database* database = transaction->database(); 49 ASSERT(database); 50 return database->stringIdentifier(); 51 } 52 53 void SQLTransactionCoordinator::processPendingTransactions(CoordinationInfo& info) 54 { 55 if (info.activeWriteTransaction || info.pendingTransactions.isEmpty()) 56 return; 57 58 RefPtr<SQLTransaction> firstPendingTransaction = info.pendingTransactions.first(); 59 if (firstPendingTransaction->isReadOnly()) { 60 do { 61 firstPendingTransaction = info.pendingTransactions.first(); 62 info.pendingTransactions.removeFirst(); 63 info.activeReadTransactions.add(firstPendingTransaction); 64 firstPendingTransaction->lockAcquired(); 65 } while (!info.pendingTransactions.isEmpty() && info.pendingTransactions.first()->isReadOnly()); 66 } else if (info.activeReadTransactions.isEmpty()) { 67 info.pendingTransactions.removeFirst(); 68 info.activeWriteTransaction = firstPendingTransaction; 69 firstPendingTransaction->lockAcquired(); 70 } 71 } 72 73 void SQLTransactionCoordinator::acquireLock(SQLTransaction* transaction) 74 { 75 String dbIdentifier = getDatabaseIdentifier(transaction); 76 77 CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier); 78 if (coordinationInfoIterator == m_coordinationInfoMap.end()) { 79 // No pending transactions for this DB 80 coordinationInfoIterator = m_coordinationInfoMap.add(dbIdentifier, CoordinationInfo()).first; 81 } 82 83 CoordinationInfo& info = coordinationInfoIterator->second; 84 info.pendingTransactions.append(transaction); 85 processPendingTransactions(info); 86 } 87 88 void SQLTransactionCoordinator::releaseLock(SQLTransaction* transaction) 89 { 90 if (m_coordinationInfoMap.isEmpty()) 91 return; 92 93 String dbIdentifier = getDatabaseIdentifier(transaction); 94 95 CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier); 96 ASSERT(coordinationInfoIterator != m_coordinationInfoMap.end()); 97 CoordinationInfo& info = coordinationInfoIterator->second; 98 99 if (transaction->isReadOnly()) { 100 ASSERT(info.activeReadTransactions.contains(transaction)); 101 info.activeReadTransactions.remove(transaction); 102 } else { 103 ASSERT(info.activeWriteTransaction == transaction); 104 info.activeWriteTransaction = 0; 105 } 106 107 processPendingTransactions(info); 108 } 109 110 void SQLTransactionCoordinator::shutdown() 111 { 112 // Notify all transactions in progress that the database thread is shutting down 113 for (CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.begin(); 114 coordinationInfoIterator != m_coordinationInfoMap.end(); ++coordinationInfoIterator) { 115 CoordinationInfo& info = coordinationInfoIterator->second; 116 if (info.activeWriteTransaction) 117 info.activeWriteTransaction->notifyDatabaseThreadIsShuttingDown(); 118 for (HashSet<RefPtr<SQLTransaction> >::iterator activeReadTransactionsIterator = 119 info.activeReadTransactions.begin(); 120 activeReadTransactionsIterator != info.activeReadTransactions.end(); 121 ++activeReadTransactionsIterator) { 122 (*activeReadTransactionsIterator)->notifyDatabaseThreadIsShuttingDown(); 123 } 124 } 125 126 // Clean up all pending transactions for all databases 127 m_coordinationInfoMap.clear(); 128 } 129 130 } // namespace WebCore 131 132 #endif // ENABLE(DATABASE) 133