initial import
[vuplus_webkit] / Source / 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 #if ENABLE(DOM_STORAGE)
30
31 #include "LocalStorageTask.h"
32 #include "StorageAreaSync.h"
33 #include <wtf/MainThread.h>
34
35 namespace WebCore {
36
37 PassOwnPtr<LocalStorageThread> LocalStorageThread::create()
38 {
39     return adoptPtr(new LocalStorageThread);
40 }
41
42 LocalStorageThread::LocalStorageThread()
43     : m_threadID(0)
44 {
45 }
46
47 LocalStorageThread::~LocalStorageThread()
48 {
49     ASSERT(isMainThread());
50     ASSERT(!m_threadID);
51 }
52
53 bool LocalStorageThread::start()
54 {
55     ASSERT(isMainThread());
56     if (!m_threadID)
57         m_threadID = createThread(LocalStorageThread::threadEntryPointCallback, this, "WebCore: LocalStorage");
58     return m_threadID;
59 }
60
61 void* LocalStorageThread::threadEntryPointCallback(void* thread)
62 {
63     return static_cast<LocalStorageThread*>(thread)->threadEntryPoint();
64 }
65
66 void* LocalStorageThread::threadEntryPoint()
67 {
68     ASSERT(!isMainThread());
69     while (OwnPtr<LocalStorageTask> task = m_queue.waitForMessage())
70         task->performTask();
71
72     return 0;
73 }
74
75 void LocalStorageThread::scheduleTask(PassOwnPtr<LocalStorageTask> task)
76 {
77     ASSERT(isMainThread());
78     ASSERT(!m_queue.killed() && m_threadID);
79     m_queue.append(task);
80 }
81
82 void LocalStorageThread::terminate()
83 {
84     ASSERT(isMainThread());
85     ASSERT(!m_queue.killed() && m_threadID);
86     // Even in weird, exceptional cases, don't wait on a nonexistent thread to terminate.
87     if (!m_threadID)
88         return;
89
90     void* returnValue;
91     m_queue.append(LocalStorageTask::createTerminate(this));
92     waitForThreadCompletion(m_threadID, &returnValue);
93     ASSERT(m_queue.killed());
94     m_threadID = 0;
95 }
96
97 void LocalStorageThread::performTerminate()
98 {
99     ASSERT(!isMainThread());
100     m_queue.kill();
101 }
102
103 }
104
105 #endif // ENABLE(DOM_STORAGE)