Home | History | Annotate | Download | only in fileapi
      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 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
     34 
     35 #include "FileThread.h"
     36 
     37 #include "AutodrainedPool.h"
     38 #include "Logging.h"
     39 
     40 namespace WebCore {
     41 
     42 FileThread::FileThread()
     43     : m_threadID(0)
     44 {
     45     m_selfRef = this;
     46 }
     47 
     48 FileThread::~FileThread()
     49 {
     50     ASSERT(m_queue.killed());
     51 }
     52 
     53 bool FileThread::start()
     54 {
     55     MutexLocker lock(m_threadCreationMutex);
     56     if (m_threadID)
     57         return true;
     58     m_threadID = createThread(FileThread::fileThreadStart, this, "WebCore: File");
     59     return m_threadID;
     60 }
     61 
     62 void FileThread::stop()
     63 {
     64     m_queue.kill();
     65 }
     66 
     67 void FileThread::postTask(PassOwnPtr<Task> task)
     68 {
     69     m_queue.append(task);
     70 }
     71 
     72 class SameInstancePredicate {
     73 public:
     74     SameInstancePredicate(const void* instance) : m_instance(instance) { }
     75     bool operator()(FileThread::Task* task) const { return task->instance() == m_instance; }
     76 private:
     77     const void* m_instance;
     78 };
     79 
     80 void FileThread::unscheduleTasks(const void* instance)
     81 {
     82     SameInstancePredicate predicate(instance);
     83     m_queue.removeIf(predicate);
     84 }
     85 
     86 void* FileThread::fileThreadStart(void* arg)
     87 {
     88     FileThread* fileThread = static_cast<FileThread*>(arg);
     89     return fileThread->runLoop();
     90 }
     91 
     92 void* FileThread::runLoop()
     93 {
     94     {
     95         // Wait for FileThread::start() to complete to have m_threadID
     96         // established before starting the main loop.
     97         MutexLocker lock(m_threadCreationMutex);
     98         LOG(FileAPI, "Started FileThread %p", this);
     99     }
    100 
    101     AutodrainedPool pool;
    102     while (OwnPtr<Task> task = m_queue.waitForMessage()) {
    103         task->performTask();
    104         pool.cycle();
    105     }
    106 
    107     LOG(FileAPI, "About to detach thread %i and clear the ref to FileThread %p, which currently has %i ref(s)", m_threadID, this, refCount());
    108 
    109     detachThread(m_threadID);
    110 
    111     // Clear the self refptr, possibly resulting in deletion
    112     m_selfRef = 0;
    113 
    114     return 0;
    115 }
    116 
    117 } // namespace WebCore
    118 
    119 #endif // ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
    120