• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008 Apple 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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "config.h"
30 #include "DatabaseThread.h"
31 
32 #if ENABLE(DATABASE)
33 
34 #include "AutodrainedPool.h"
35 #include "Database.h"
36 #include "DatabaseTask.h"
37 #include "Logging.h"
38 
39 namespace WebCore {
40 
DatabaseThread()41 DatabaseThread::DatabaseThread()
42     : m_threadID(0)
43 {
44     m_selfRef = this;
45 }
46 
~DatabaseThread()47 DatabaseThread::~DatabaseThread()
48 {
49     // FIXME: Any cleanup required here?  Since the thread deletes itself after running its detached course, I don't think so.  Lets be sure.
50 }
51 
start()52 bool DatabaseThread::start()
53 {
54     MutexLocker lock(m_threadCreationMutex);
55 
56     if (m_threadID)
57         return true;
58 
59     m_threadID = createThread(DatabaseThread::databaseThreadStart, this, "WebCore: Database");
60 
61     return m_threadID;
62 }
63 
requestTermination()64 void DatabaseThread::requestTermination()
65 {
66     LOG(StorageAPI, "DatabaseThread %p was asked to terminate\n", this);
67     m_queue.kill();
68 }
69 
terminationRequested() const70 bool DatabaseThread::terminationRequested() const
71 {
72     return m_queue.killed();
73 }
74 
databaseThreadStart(void * vDatabaseThread)75 void* DatabaseThread::databaseThreadStart(void* vDatabaseThread)
76 {
77     DatabaseThread* dbThread = static_cast<DatabaseThread*>(vDatabaseThread);
78     return dbThread->databaseThread();
79 }
80 
databaseThread()81 void* DatabaseThread::databaseThread()
82 {
83     {
84         // Wait for DatabaseThread::start() to complete.
85         MutexLocker lock(m_threadCreationMutex);
86         LOG(StorageAPI, "Started DatabaseThread %p", this);
87     }
88 
89     AutodrainedPool pool;
90     while (true) {
91         RefPtr<DatabaseTask> task;
92         if (!m_queue.waitForMessage(task))
93             break;
94 
95         task->performTask();
96 
97         pool.cycle();
98     }
99 
100     LOG(StorageAPI, "About to detach thread %i and clear the ref to DatabaseThread %p, which currently has %i ref(s)", m_threadID, this, refCount());
101 
102     // Close the databases that we ran transactions on. This ensures that if any transactions are still open, they are rolled back and we don't leave the database in an
103     // inconsistent or locked state.
104     if (m_openDatabaseSet.size() > 0) {
105         // As the call to close will modify the original set, we must take a copy to iterate over.
106         DatabaseSet openSetCopy;
107         openSetCopy.swap(m_openDatabaseSet);
108         DatabaseSet::iterator end = openSetCopy.end();
109         for (DatabaseSet::iterator it = openSetCopy.begin(); it != end; ++it)
110            (*it)->close();
111     }
112 
113     // Detach the thread so its resources are no longer of any concern to anyone else
114     detachThread(m_threadID);
115 
116     // Clear the self refptr, possibly resulting in deletion
117     m_selfRef = 0;
118 
119     return 0;
120 }
121 
recordDatabaseOpen(Database * database)122 void DatabaseThread::recordDatabaseOpen(Database* database)
123 {
124     ASSERT(currentThread() == m_threadID);
125     ASSERT(database);
126     ASSERT(!m_openDatabaseSet.contains(database));
127     m_openDatabaseSet.add(database);
128 }
129 
recordDatabaseClosed(Database * database)130 void DatabaseThread::recordDatabaseClosed(Database* database)
131 {
132     ASSERT(currentThread() == m_threadID);
133     ASSERT(database);
134     ASSERT(m_queue.killed() || m_openDatabaseSet.contains(database));
135     m_openDatabaseSet.remove(database);
136 }
137 
scheduleTask(PassRefPtr<DatabaseTask> task)138 void DatabaseThread::scheduleTask(PassRefPtr<DatabaseTask> task)
139 {
140     m_queue.append(task);
141 }
142 
scheduleImmediateTask(PassRefPtr<DatabaseTask> task)143 void DatabaseThread::scheduleImmediateTask(PassRefPtr<DatabaseTask> task)
144 {
145     m_queue.prepend(task);
146 }
147 
unscheduleDatabaseTasks(Database * database)148 void DatabaseThread::unscheduleDatabaseTasks(Database* database)
149 {
150     // Note that the thread loop is running, so some tasks for the database
151     // may still be executed. This is unavoidable.
152 
153     Deque<RefPtr<DatabaseTask> > filteredReverseQueue;
154     RefPtr<DatabaseTask> task;
155     while (m_queue.tryGetMessage(task)) {
156         if (task->database() != database)
157             filteredReverseQueue.append(task);
158     }
159 
160     while (!filteredReverseQueue.isEmpty()) {
161         m_queue.append(filteredReverseQueue.first());
162         filteredReverseQueue.removeFirst();
163     }
164 }
165 
166 } // namespace WebCore
167 #endif
168