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