ParallelJobsGeneric.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2011 University of Szeged
  3. * Copyright (C) 2011 Gabor Loki <loki@webkit.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "config.h"
  28. #if ENABLE(THREADING_GENERIC)
  29. #include "ParallelJobs.h"
  30. #include <wtf/NumberOfCores.h>
  31. namespace WTF {
  32. Vector< RefPtr<ParallelEnvironment::ThreadPrivate> >* ParallelEnvironment::s_threadPool = 0;
  33. ParallelEnvironment::ParallelEnvironment(ThreadFunction threadFunction, size_t sizeOfParameter, int requestedJobNumber) :
  34. m_threadFunction(threadFunction),
  35. m_sizeOfParameter(sizeOfParameter)
  36. {
  37. ASSERT_ARG(requestedJobNumber, requestedJobNumber >= 1);
  38. int maxNumberOfCores = numberOfProcessorCores();
  39. if (!requestedJobNumber || requestedJobNumber > maxNumberOfCores)
  40. requestedJobNumber = static_cast<unsigned>(maxNumberOfCores);
  41. if (!s_threadPool)
  42. s_threadPool = new Vector< RefPtr<ThreadPrivate> >();
  43. // The main thread should be also a worker.
  44. int maxNumberOfNewThreads = requestedJobNumber - 1;
  45. for (int i = 0; i < maxNumberOfCores && m_threads.size() < static_cast<unsigned>(maxNumberOfNewThreads); ++i) {
  46. if (s_threadPool->size() < static_cast<unsigned>(i) + 1U)
  47. s_threadPool->append(ThreadPrivate::create());
  48. if ((*s_threadPool)[i]->tryLockFor(this))
  49. m_threads.append((*s_threadPool)[i]);
  50. }
  51. m_numberOfJobs = m_threads.size() + 1;
  52. }
  53. void ParallelEnvironment::execute(void* parameters)
  54. {
  55. unsigned char* currentParameter = static_cast<unsigned char*>(parameters);
  56. size_t i;
  57. for (i = 0; i < m_threads.size(); ++i) {
  58. m_threads[i]->execute(m_threadFunction, currentParameter);
  59. currentParameter += m_sizeOfParameter;
  60. }
  61. // The work for the main thread.
  62. (*m_threadFunction)(currentParameter);
  63. // Wait until all jobs are done.
  64. for (i = 0; i < m_threads.size(); ++i)
  65. m_threads[i]->waitForFinish();
  66. }
  67. bool ParallelEnvironment::ThreadPrivate::tryLockFor(ParallelEnvironment* parent)
  68. {
  69. bool locked = m_mutex.tryLock();
  70. if (!locked)
  71. return false;
  72. if (m_parent) {
  73. m_mutex.unlock();
  74. return false;
  75. }
  76. if (!m_threadID)
  77. m_threadID = createThread(&ParallelEnvironment::ThreadPrivate::workerThread, this, "Parallel worker");
  78. if (m_threadID)
  79. m_parent = parent;
  80. m_mutex.unlock();
  81. return m_threadID;
  82. }
  83. void ParallelEnvironment::ThreadPrivate::execute(ThreadFunction threadFunction, void* parameters)
  84. {
  85. MutexLocker lock(m_mutex);
  86. m_threadFunction = threadFunction;
  87. m_parameters = parameters;
  88. m_running = true;
  89. m_threadCondition.signal();
  90. }
  91. void ParallelEnvironment::ThreadPrivate::waitForFinish()
  92. {
  93. MutexLocker lock(m_mutex);
  94. while (m_running)
  95. m_threadCondition.wait(m_mutex);
  96. }
  97. void ParallelEnvironment::ThreadPrivate::workerThread(void* threadData)
  98. {
  99. ThreadPrivate* sharedThread = reinterpret_cast<ThreadPrivate*>(threadData);
  100. MutexLocker lock(sharedThread->m_mutex);
  101. while (sharedThread->m_threadID) {
  102. if (sharedThread->m_running) {
  103. (*sharedThread->m_threadFunction)(sharedThread->m_parameters);
  104. sharedThread->m_running = false;
  105. sharedThread->m_parent = 0;
  106. sharedThread->m_threadCondition.signal();
  107. }
  108. sharedThread->m_threadCondition.wait(sharedThread->m_mutex);
  109. }
  110. }
  111. } // namespace WTF
  112. #endif // ENABLE(THREADING_GENERIC)