ParallelJobsGeneric.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef ParallelJobsGeneric_h
  28. #define ParallelJobsGeneric_h
  29. #if ENABLE(THREADING_GENERIC)
  30. #include <wtf/RefCounted.h>
  31. #include <wtf/Threading.h>
  32. namespace WTF {
  33. class ParallelEnvironment {
  34. WTF_MAKE_FAST_ALLOCATED;
  35. public:
  36. typedef void (*ThreadFunction)(void*);
  37. WTF_EXPORT_PRIVATE ParallelEnvironment(ThreadFunction, size_t sizeOfParameter, int requestedJobNumber);
  38. int numberOfJobs()
  39. {
  40. return m_numberOfJobs;
  41. }
  42. WTF_EXPORT_PRIVATE void execute(void* parameters);
  43. class ThreadPrivate : public RefCounted<ThreadPrivate> {
  44. public:
  45. ThreadPrivate()
  46. : m_threadID(0)
  47. , m_running(false)
  48. , m_parent(0)
  49. #if USE(MANX_COND_INIT)
  50. , m_threadCondition(m_mutex)
  51. #endif
  52. {
  53. }
  54. bool tryLockFor(ParallelEnvironment*);
  55. void execute(ThreadFunction, void*);
  56. void waitForFinish();
  57. static PassRefPtr<ThreadPrivate> create()
  58. {
  59. return adoptRef(new ThreadPrivate());
  60. }
  61. static void workerThread(void*);
  62. private:
  63. ThreadIdentifier m_threadID;
  64. bool m_running;
  65. ParallelEnvironment* m_parent;
  66. mutable Mutex m_mutex;
  67. ThreadCondition m_threadCondition;
  68. ThreadFunction m_threadFunction;
  69. void* m_parameters;
  70. };
  71. private:
  72. ThreadFunction m_threadFunction;
  73. size_t m_sizeOfParameter;
  74. int m_numberOfJobs;
  75. Vector< RefPtr<ThreadPrivate> > m_threads;
  76. static Vector< RefPtr<ThreadPrivate> >* s_threadPool;
  77. };
  78. } // namespace WTF
  79. #endif // ENABLE(THREADING_GENERIC)
  80. #endif // ParallelJobsGeneric_h