1/*
2 * Copyright (C) 2010 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32
33#include "FileThread.h"
34
35#include "Logging.h"
36#include <wtf/AutodrainedPool.h>
37
38namespace WebCore {
39
40FileThread::FileThread()
41    : m_threadID(0)
42{
43    m_selfRef = this;
44}
45
46FileThread::~FileThread()
47{
48    ASSERT(m_queue.killed());
49}
50
51bool FileThread::start()
52{
53    MutexLocker lock(m_threadCreationMutex);
54    if (m_threadID)
55        return true;
56    m_threadID = createThread(FileThread::fileThreadStart, this, "WebCore: File");
57    return m_threadID;
58}
59
60void FileThread::stop()
61{
62    m_queue.kill();
63}
64
65void FileThread::postTask(Task task)
66{
67    m_queue.append(std::make_unique<FileThread::Task>(WTF::move(task)));
68}
69
70class SameInstancePredicate {
71public:
72    SameInstancePredicate(const void* instance) : m_instance(instance) { }
73    bool operator()(FileThread::Task& task) const { return task.instance() == m_instance; }
74private:
75    const void* m_instance;
76};
77
78void FileThread::unscheduleTasks(const void* instance)
79{
80    SameInstancePredicate predicate(instance);
81    m_queue.removeIf(predicate);
82}
83
84void FileThread::fileThreadStart(void* arg)
85{
86    FileThread* fileThread = static_cast<FileThread*>(arg);
87    fileThread->runLoop();
88}
89
90void FileThread::runLoop()
91{
92    {
93        // Wait for FileThread::start() to complete to have m_threadID
94        // established before starting the main loop.
95        MutexLocker lock(m_threadCreationMutex);
96        LOG(FileAPI, "Started FileThread %p", this);
97    }
98
99    while (auto task = m_queue.waitForMessage()) {
100        AutodrainedPool pool;
101
102        task->performTask();
103    }
104
105    LOG(FileAPI, "About to detach thread %i and clear the ref to FileThread %p, which currently has %i ref(s)", m_threadID, this, refCount());
106
107    detachThread(m_threadID);
108
109    // Clear the self refptr, possibly resulting in deletion
110    m_selfRef = 0;
111}
112
113} // namespace WebCore
114