author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 3 | 41300fa6a67c |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 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 |
#include "SQLTransactionClient.h" |
|
39 |
#include "SQLTransactionCoordinator.h" |
|
40 |
||
41 |
namespace WebCore { |
|
42 |
||
43 |
DatabaseThread::DatabaseThread() |
|
44 |
: m_threadID(0) |
|
45 |
, m_transactionClient(new SQLTransactionClient()) |
|
46 |
, m_transactionCoordinator(new SQLTransactionCoordinator()) |
|
47 |
{ |
|
48 |
m_selfRef = this; |
|
49 |
} |
|
50 |
||
51 |
DatabaseThread::~DatabaseThread() |
|
52 |
{ |
|
53 |
// FIXME: Any cleanup required here? Since the thread deletes itself after running its detached course, I don't think so. Lets be sure. |
|
54 |
} |
|
55 |
||
56 |
bool DatabaseThread::start() |
|
57 |
{ |
|
58 |
MutexLocker lock(m_threadCreationMutex); |
|
59 |
||
60 |
if (m_threadID) |
|
61 |
return true; |
|
62 |
||
63 |
m_threadID = createThread(DatabaseThread::databaseThreadStart, this, "WebCore: Database"); |
|
64 |
||
65 |
return m_threadID; |
|
66 |
} |
|
67 |
||
68 |
void DatabaseThread::requestTermination() |
|
69 |
{ |
|
70 |
LOG(StorageAPI, "DatabaseThread %p was asked to terminate\n", this); |
|
71 |
m_queue.kill(); |
|
72 |
} |
|
73 |
||
74 |
bool DatabaseThread::terminationRequested() const |
|
75 |
{ |
|
76 |
return m_queue.killed(); |
|
77 |
} |
|
78 |
||
79 |
void* DatabaseThread::databaseThreadStart(void* vDatabaseThread) |
|
80 |
{ |
|
81 |
DatabaseThread* dbThread = static_cast<DatabaseThread*>(vDatabaseThread); |
|
82 |
return dbThread->databaseThread(); |
|
83 |
} |
|
84 |
||
85 |
void* DatabaseThread::databaseThread() |
|
86 |
{ |
|
87 |
{ |
|
88 |
// Wait for DatabaseThread::start() to complete. |
|
89 |
MutexLocker lock(m_threadCreationMutex); |
|
90 |
LOG(StorageAPI, "Started DatabaseThread %p", this); |
|
91 |
} |
|
92 |
||
93 |
AutodrainedPool pool; |
|
94 |
while (true) { |
|
95 |
RefPtr<DatabaseTask> task; |
|
96 |
if (!m_queue.waitForMessage(task)) |
|
97 |
break; |
|
98 |
||
99 |
task->performTask(); |
|
100 |
||
101 |
pool.cycle(); |
|
102 |
} |
|
103 |
||
104 |
// Clean up the list of all pending transactions on this database thread |
|
105 |
m_transactionCoordinator->shutdown(); |
|
106 |
||
107 |
LOG(StorageAPI, "About to detach thread %i and clear the ref to DatabaseThread %p, which currently has %i ref(s)", m_threadID, this, refCount()); |
|
108 |
||
109 |
// 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 |
|
110 |
// inconsistent or locked state. |
|
111 |
if (m_openDatabaseSet.size() > 0) { |
|
112 |
// As the call to close will modify the original set, we must take a copy to iterate over. |
|
113 |
DatabaseSet openSetCopy; |
|
114 |
openSetCopy.swap(m_openDatabaseSet); |
|
115 |
DatabaseSet::iterator end = openSetCopy.end(); |
|
116 |
for (DatabaseSet::iterator it = openSetCopy.begin(); it != end; ++it) |
|
117 |
(*it)->close(); |
|
118 |
} |
|
119 |
||
120 |
// Detach the thread so its resources are no longer of any concern to anyone else |
|
121 |
detachThread(m_threadID); |
|
122 |
||
123 |
// Clear the self refptr, possibly resulting in deletion |
|
124 |
m_selfRef = 0; |
|
125 |
||
126 |
return 0; |
|
127 |
} |
|
128 |
||
129 |
void DatabaseThread::recordDatabaseOpen(Database* database) |
|
130 |
{ |
|
131 |
ASSERT(currentThread() == m_threadID); |
|
132 |
ASSERT(database); |
|
133 |
ASSERT(!m_openDatabaseSet.contains(database)); |
|
134 |
m_openDatabaseSet.add(database); |
|
135 |
} |
|
136 |
||
137 |
void DatabaseThread::recordDatabaseClosed(Database* database) |
|
138 |
{ |
|
139 |
ASSERT(currentThread() == m_threadID); |
|
140 |
ASSERT(database); |
|
141 |
ASSERT(m_queue.killed() || m_openDatabaseSet.contains(database)); |
|
142 |
m_openDatabaseSet.remove(database); |
|
143 |
} |
|
144 |
||
145 |
void DatabaseThread::scheduleTask(PassRefPtr<DatabaseTask> task) |
|
146 |
{ |
|
147 |
m_queue.append(task); |
|
148 |
} |
|
149 |
||
150 |
void DatabaseThread::scheduleImmediateTask(PassRefPtr<DatabaseTask> task) |
|
151 |
{ |
|
152 |
m_queue.prepend(task); |
|
153 |
} |
|
154 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
class SameDatabasePredicate { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
public: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
SameDatabasePredicate(const Database* database) : m_database(database) { } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
bool operator()(RefPtr<DatabaseTask>& task) const { return task->database() == m_database; } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
private: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
const Database* m_database; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
}; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
|
0 | 163 |
void DatabaseThread::unscheduleDatabaseTasks(Database* database) |
164 |
{ |
|
165 |
// Note that the thread loop is running, so some tasks for the database |
|
166 |
// may still be executed. This is unavoidable. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
SameDatabasePredicate predicate(database); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
m_queue.removeIf(predicate); |
0 | 169 |
} |
170 |
||
171 |
} // namespace WebCore |
|
172 |
#endif |