Home | History | Annotate | Download | only in storage
      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 "Database.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>
     42 
     43 namespace WebCore {
     44 
     45 static String getDatabaseIdentifier(SQLTransaction* transaction)
     46 {
     47     Database* database = transaction->database();
     48     ASSERT(database);
     49     return database->stringIdentifier();
     50 }
     51 
     52 void SQLTransactionCoordinator::processPendingTransactions(CoordinationInfo& info)
     53 {
     54     if (info.activeWriteTransaction || info.pendingTransactions.isEmpty())
     55         return;
     56 
     57     RefPtr<SQLTransaction> firstPendingTransaction = info.pendingTransactions.first();
     58     if (firstPendingTransaction->isReadOnly()) {
     59         do {
     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();
     68     }
     69 }
     70 
     71 void SQLTransactionCoordinator::acquireLock(SQLTransaction* transaction)
     72 {
     73     String dbIdentifier = getDatabaseIdentifier(transaction);
     74 
     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;
     79     }
     80 
     81     CoordinationInfo& info = coordinationInfoIterator->second;
     82     info.pendingTransactions.append(transaction);
     83     processPendingTransactions(info);
     84 }
     85 
     86 void SQLTransactionCoordinator::releaseLock(SQLTransaction* transaction)
     87 {
     88     if (m_coordinationInfoMap.isEmpty())
     89         return;
     90 
     91     String dbIdentifier = getDatabaseIdentifier(transaction);
     92 
     93     CoordinationInfoMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
     94     ASSERT(coordinationInfoIterator != m_coordinationInfoMap.end());
     95     CoordinationInfo& info = coordinationInfoIterator->second;
     96 
     97     if (transaction->isReadOnly()) {
     98         ASSERT(info.activeReadTransactions.contains(transaction));
     99         info.activeReadTransactions.remove(transaction);
    100     } else {
    101         ASSERT(info.activeWriteTransaction == transaction);
    102         info.activeWriteTransaction = 0;
    103     }
    104 
    105     processPendingTransactions(info);
    106 }
    107 
    108 void SQLTransactionCoordinator::shutdown()
    109 {
    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();
    121         }
    122     }
    123 
    124     // Clean up all pending transactions for all databases
    125     m_coordinationInfoMap.clear();
    126 }
    127 
    128 } // namespace WebCore
    129 
    130 #endif // ENABLE(DATABASE)
    131