OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007, 2008, 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007, 2008, 2013 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 23 matching lines...) Expand all Loading... |
34 #include "modules/webdatabase/SQLTransactionCoordinator.h" | 34 #include "modules/webdatabase/SQLTransactionCoordinator.h" |
35 #include "platform/Logging.h" | 35 #include "platform/Logging.h" |
36 #include "platform/ThreadSafeFunctional.h" | 36 #include "platform/ThreadSafeFunctional.h" |
37 #include "platform/WebThreadSupportingGC.h" | 37 #include "platform/WebThreadSupportingGC.h" |
38 #include "public/platform/Platform.h" | 38 #include "public/platform/Platform.h" |
39 | 39 |
40 namespace blink { | 40 namespace blink { |
41 | 41 |
42 DatabaseThread::DatabaseThread() | 42 DatabaseThread::DatabaseThread() |
43 : m_transactionClient(adoptPtr(new SQLTransactionClient())) | 43 : m_transactionClient(adoptPtr(new SQLTransactionClient())) |
44 , m_transactionCoordinator(new SQLTransactionCoordinator()) | |
45 , m_cleanupSync(nullptr) | 44 , m_cleanupSync(nullptr) |
46 , m_terminationRequested(false) | 45 , m_terminationRequested(false) |
47 { | 46 { |
| 47 DCHECK(isMainThread()); |
48 } | 48 } |
49 | 49 |
50 DatabaseThread::~DatabaseThread() | 50 DatabaseThread::~DatabaseThread() |
51 { | 51 { |
52 ASSERT(m_openDatabaseSet.isEmpty()); | 52 DCHECK(!m_openDatabaseSet); |
53 ASSERT(!m_thread); | 53 ASSERT(!m_thread); |
54 } | 54 } |
55 | 55 |
56 DEFINE_TRACE(DatabaseThread) | 56 DEFINE_TRACE(DatabaseThread) |
57 { | 57 { |
58 visitor->trace(m_openDatabaseSet); | |
59 visitor->trace(m_transactionCoordinator); | |
60 } | 58 } |
61 | 59 |
62 void DatabaseThread::start() | 60 void DatabaseThread::start() |
63 { | 61 { |
64 ASSERT(isMainThread()); | 62 DCHECK(isMainThread()); |
65 if (m_thread) | 63 if (m_thread) |
66 return; | 64 return; |
67 m_thread = WebThreadSupportingGC::create("WebCore: Database"); | 65 m_thread = WebThreadSupportingGC::create("WebCore: Database", true); |
68 m_thread->postTask(BLINK_FROM_HERE, threadSafeBind(&DatabaseThread::setupDat
abaseThread, wrapCrossThreadPersistent(this))); | 66 m_thread->postTask(BLINK_FROM_HERE, threadSafeBind(&DatabaseThread::setupDat
abaseThread, wrapCrossThreadPersistent(this))); |
69 } | 67 } |
70 | 68 |
71 void DatabaseThread::setupDatabaseThread() | 69 void DatabaseThread::setupDatabaseThread() |
72 { | 70 { |
73 m_thread->initialize(); | 71 m_thread->initialize(); |
| 72 m_transactionCoordinator = new SQLTransactionCoordinator(); |
74 } | 73 } |
75 | 74 |
76 void DatabaseThread::terminate() | 75 void DatabaseThread::terminate() |
77 { | 76 { |
78 ASSERT(isMainThread()); | 77 DCHECK(isMainThread()); |
79 TaskSynchronizer sync; | 78 TaskSynchronizer sync; |
80 { | 79 { |
81 MutexLocker lock(m_terminationRequestedMutex); | 80 MutexLocker lock(m_terminationRequestedMutex); |
82 ASSERT(!m_terminationRequested); | 81 ASSERT(!m_terminationRequested); |
83 m_terminationRequested = true; | 82 m_terminationRequested = true; |
84 m_cleanupSync = &sync; | 83 m_cleanupSync = &sync; |
85 WTF_LOG(StorageAPI, "DatabaseThread %p was asked to terminate\n", this); | 84 WTF_LOG(StorageAPI, "DatabaseThread %p was asked to terminate\n", this); |
86 m_thread->postTask(BLINK_FROM_HERE, threadSafeBind(&DatabaseThread::clea
nupDatabaseThread, wrapCrossThreadPersistent(this))); | 85 m_thread->postTask(BLINK_FROM_HERE, threadSafeBind(&DatabaseThread::clea
nupDatabaseThread, wrapCrossThreadPersistent(this))); |
87 } | 86 } |
88 sync.waitForTaskCompletion(); | 87 sync.waitForTaskCompletion(); |
89 // The WebThread destructor blocks until all the tasks of the database | 88 // The WebThread destructor blocks until all the tasks of the database |
90 // thread are processed. However, it shouldn't block at all because | 89 // thread are processed. However, it shouldn't block at all because |
91 // the database thread has already finished processing the cleanup task. | 90 // the database thread has already finished processing the cleanup task. |
92 m_thread.clear(); | 91 m_thread.clear(); |
93 } | 92 } |
94 | 93 |
95 void DatabaseThread::cleanupDatabaseThread() | 94 void DatabaseThread::cleanupDatabaseThread() |
96 { | 95 { |
| 96 DCHECK(isDatabaseThread()); |
| 97 |
97 WTF_LOG(StorageAPI, "Cleaning up DatabaseThread %p", this); | 98 WTF_LOG(StorageAPI, "Cleaning up DatabaseThread %p", this); |
98 | 99 |
99 // Clean up the list of all pending transactions on this database thread | 100 // Clean up the list of all pending transactions on this database thread |
100 m_transactionCoordinator->shutdown(); | 101 m_transactionCoordinator->shutdown(); |
101 | 102 |
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 databa
se in an | 103 // 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 databa
se in an |
103 // inconsistent or locked state. | 104 // inconsistent or locked state. |
104 if (m_openDatabaseSet.size() > 0) { | 105 if (m_openDatabaseSet) { |
105 // As the call to close will modify the original set, we must take a cop
y to iterate over. | 106 // As the call to close will modify the original set, we must take a cop
y to iterate over. |
106 HeapHashSet<Member<Database>> openSetCopy; | 107 HashSet<CrossThreadPersistent<Database>> openSetCopy; |
107 openSetCopy.swap(m_openDatabaseSet); | 108 openSetCopy.swap(*m_openDatabaseSet); |
108 HeapHashSet<Member<Database>>::iterator end = openSetCopy.end(); | 109 HashSet<CrossThreadPersistent<Database>>::iterator end = openSetCopy.end
(); |
109 for (HeapHashSet<Member<Database>>::iterator it = openSetCopy.begin(); i
t != end; ++it) | 110 for (HashSet<CrossThreadPersistent<Database>>::iterator it = openSetCopy
.begin(); it != end; ++it) |
110 (*it)->close(); | 111 (*it)->close(); |
| 112 m_openDatabaseSet = nullptr; |
111 } | 113 } |
112 m_openDatabaseSet.clear(); | |
113 | 114 |
114 m_thread->postTask(BLINK_FROM_HERE, WTF::bind(&DatabaseThread::cleanupDataba
seThreadCompleted, this)); | 115 m_thread->postTask(BLINK_FROM_HERE, WTF::bind(&DatabaseThread::cleanupDataba
seThreadCompleted, this)); |
115 } | 116 } |
116 | 117 |
117 void DatabaseThread::cleanupDatabaseThreadCompleted() | 118 void DatabaseThread::cleanupDatabaseThreadCompleted() |
118 { | 119 { |
119 m_thread->shutdown(); | 120 m_thread->shutdown(); |
120 if (m_cleanupSync) // Someone wanted to know when we were done cleaning up. | 121 if (m_cleanupSync) // Someone wanted to know when we were done cleaning up. |
121 m_cleanupSync->taskCompleted(); | 122 m_cleanupSync->taskCompleted(); |
122 } | 123 } |
123 | 124 |
124 void DatabaseThread::recordDatabaseOpen(Database* database) | 125 void DatabaseThread::recordDatabaseOpen(Database* database) |
125 { | 126 { |
126 ASSERT(isDatabaseThread()); | 127 ASSERT(isDatabaseThread()); |
127 ASSERT(database); | 128 ASSERT(database); |
128 ASSERT(!m_openDatabaseSet.contains(database)); | 129 if (!m_openDatabaseSet) |
| 130 m_openDatabaseSet = adoptPtr(new HashSet<CrossThreadPersistent<Database>
>()); |
| 131 DCHECK(!m_openDatabaseSet->contains(database)); |
129 MutexLocker lock(m_terminationRequestedMutex); | 132 MutexLocker lock(m_terminationRequestedMutex); |
130 if (!m_terminationRequested) | 133 if (!m_terminationRequested) |
131 m_openDatabaseSet.add(database); | 134 m_openDatabaseSet->add(database); |
132 } | 135 } |
133 | 136 |
134 void DatabaseThread::recordDatabaseClosed(Database* database) | 137 void DatabaseThread::recordDatabaseClosed(Database* database) |
135 { | 138 { |
136 ASSERT(isDatabaseThread()); | 139 ASSERT(isDatabaseThread()); |
137 ASSERT(database); | 140 ASSERT(database); |
138 #if ENABLE(ASSERT) | 141 #if ENABLE(ASSERT) |
139 { | 142 { |
140 MutexLocker lock(m_terminationRequestedMutex); | 143 MutexLocker lock(m_terminationRequestedMutex); |
141 ASSERT(m_terminationRequested || m_openDatabaseSet.contains(database)); | 144 DCHECK(m_terminationRequested || m_openDatabaseSet->contains(database)); |
142 } | 145 } |
143 #endif | 146 #endif |
144 m_openDatabaseSet.remove(database); | 147 if (!m_openDatabaseSet) { |
| 148 m_openDatabaseSet->remove(database); |
| 149 if (m_openDatabaseSet->isEmpty()) |
| 150 m_openDatabaseSet = nullptr; |
| 151 } |
145 } | 152 } |
146 | 153 |
147 bool DatabaseThread::isDatabaseOpen(Database* database) | 154 bool DatabaseThread::isDatabaseOpen(Database* database) |
148 { | 155 { |
149 ASSERT(isDatabaseThread()); | 156 ASSERT(isDatabaseThread()); |
150 ASSERT(database); | 157 ASSERT(database); |
151 MutexLocker lock(m_terminationRequestedMutex); | 158 MutexLocker lock(m_terminationRequestedMutex); |
152 return !m_terminationRequested && m_openDatabaseSet.contains(database); | 159 return !m_terminationRequested && m_openDatabaseSet->contains(database); |
153 } | 160 } |
154 | 161 |
155 bool DatabaseThread::isDatabaseThread() const | 162 bool DatabaseThread::isDatabaseThread() const |
156 { | 163 { |
157 // This function is called only from the main thread or the database | 164 // This function is called only from the main thread or the database |
158 // thread. If we are not in the main thread, we are in the database thread. | 165 // thread. If we are not in the main thread, we are in the database thread. |
159 return !isMainThread(); | 166 return !isMainThread(); |
160 } | 167 } |
161 | 168 |
162 void DatabaseThread::scheduleTask(PassOwnPtr<DatabaseTask> task) | 169 void DatabaseThread::scheduleTask(PassOwnPtr<DatabaseTask> task) |
163 { | 170 { |
164 ASSERT(m_thread); | 171 ASSERT(m_thread); |
165 #if ENABLE(ASSERT) | 172 #if ENABLE(ASSERT) |
166 { | 173 { |
167 MutexLocker lock(m_terminationRequestedMutex); | 174 MutexLocker lock(m_terminationRequestedMutex); |
168 ASSERT(!m_terminationRequested); | 175 ASSERT(!m_terminationRequested); |
169 } | 176 } |
170 #endif | 177 #endif |
171 // WebThread takes ownership of the task. | 178 // WebThread takes ownership of the task. |
172 m_thread->postTask(BLINK_FROM_HERE, threadSafeBind(&DatabaseTask::run, std::
move(task))); | 179 m_thread->postTask(BLINK_FROM_HERE, threadSafeBind(&DatabaseTask::run, std::
move(task))); |
173 } | 180 } |
174 | 181 |
175 } // namespace blink | 182 } // namespace blink |
OLD | NEW |