/src/3rdparty/webkit/Source/WebCore/fileapi/FileThread.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 119 lines · 66 code · 21 blank · 32 comment · 4 complexity · 75b85afe196d5bef107ef8503888f637 MD5 · raw file

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