2009-03-11 Darin Adler <darin@apple.com>
[WebKit-https.git] / WebCore / storage / LocalStorageThread.cpp
1 /*
2  * Copyright (C) 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "LocalStorageThread.h"
28
29 #include "LocalStorage.h"
30 #include "LocalStorageArea.h"
31 #include "LocalStorageTask.h"
32
33 namespace WebCore {
34
35 PassRefPtr<LocalStorageThread> LocalStorageThread::create()
36 {
37     return adoptRef(new LocalStorageThread);
38 }
39
40 LocalStorageThread::LocalStorageThread()
41     : m_threadID(0)
42 {
43     m_selfRef = this;
44 }
45
46 bool LocalStorageThread::start()
47 {
48     MutexLocker lock(m_threadCreationMutex);
49
50     if (m_threadID)
51         return true;
52
53     m_threadID = createThread(LocalStorageThread::localStorageThreadStart, this, "WebCore: LocalStorage");
54
55     return m_threadID;
56 }
57
58 void* LocalStorageThread::localStorageThreadStart(void* thread)
59 {
60     return static_cast<LocalStorageThread*>(thread)->localStorageThread();
61 }
62
63 void* LocalStorageThread::localStorageThread()
64 {
65     {
66         // Wait for LocalStorageThread::start() to complete.
67         MutexLocker lock(m_threadCreationMutex);
68     }
69
70     while (true) {
71         RefPtr<LocalStorageTask> task;
72         if (!m_queue.waitForMessage(task))
73             break;
74
75         task->performTask();
76     }
77
78     // Detach the thread so its resources are no longer of any concern to anyone else
79     detachThread(m_threadID);
80     m_threadID = 0;
81
82     // Clear the self refptr, possibly resulting in deletion
83     m_selfRef = 0;
84
85     return 0;
86 }
87
88 void LocalStorageThread::scheduleImport(PassRefPtr<LocalStorage> storage)
89 {
90     ASSERT(!m_queue.killed() && m_threadID);
91     m_queue.append(LocalStorageTask::createImport(storage));
92 }
93
94 void LocalStorageThread::scheduleSync(PassRefPtr<LocalStorage> storage)
95 {
96     ASSERT(!m_queue.killed() && m_threadID);
97     m_queue.append(LocalStorageTask::createSync(storage));
98 }
99
100 void LocalStorageThread::scheduleImport(PassRefPtr<LocalStorageArea> area)
101 {
102     ASSERT(!m_queue.killed() && m_threadID);
103     m_queue.append(LocalStorageTask::createImport(area));
104 }
105
106 void LocalStorageThread::scheduleSync(PassRefPtr<LocalStorageArea> area)
107 {
108     ASSERT(!m_queue.killed() && m_threadID);
109     m_queue.append(LocalStorageTask::createSync(area));
110 }
111
112 void LocalStorageThread::terminate()
113 {
114     ASSERT(isMainThread());
115     
116     // Ideally we'd never be killing a thread that wasn't live, so ASSERT it.
117     // But if we do in a release build, make sure to not wait on a condition that will never get signalled
118     ASSERT(!m_queue.killed() && m_threadID);
119     if (!m_threadID)
120         return;
121
122     MutexLocker locker(m_terminateLock);
123     
124     m_queue.append(LocalStorageTask::createTerminate(this));
125     
126     m_terminateCondition.wait(m_terminateLock);
127 }
128
129 void LocalStorageThread::performTerminate()
130 {
131     ASSERT(!isMainThread());
132
133     m_queue.kill();
134         
135     MutexLocker locker(m_terminateLock);
136     m_terminateCondition.signal();
137 }
138
139 }