ParallelJobs.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 ParallelJobs_h
  28. #define ParallelJobs_h
  29. #include <wtf/Assertions.h>
  30. #include <wtf/Noncopyable.h>
  31. #include <wtf/RefPtr.h>
  32. #include <wtf/Vector.h>
  33. // Usage:
  34. //
  35. // // Initialize parallel jobs
  36. // ParallelJobs<TypeOfParameter> parallelJobs(&worker [, requestedNumberOfJobs]);
  37. //
  38. // // Fill the parameter array
  39. // for(i = 0; i < parallelJobs.numberOfJobs(); ++i) {
  40. // TypeOfParameter& params = parallelJobs.parameter(i);
  41. // params.attr1 = localVars ...
  42. // ...
  43. // }
  44. //
  45. // // Execute parallel jobs
  46. // parallelJobs.execute();
  47. //
  48. #if ENABLE(THREADING_GENERIC)
  49. #include <wtf/ParallelJobsGeneric.h>
  50. #elif ENABLE(THREADING_OPENMP)
  51. #include <wtf/ParallelJobsOpenMP.h>
  52. #elif ENABLE(THREADING_LIBDISPATCH)
  53. #include <wtf/ParallelJobsLibdispatch.h>
  54. #else
  55. #error "No parallel processing API for ParallelJobs"
  56. #endif
  57. namespace WTF {
  58. template<typename Type>
  59. class ParallelJobs {
  60. WTF_MAKE_FAST_ALLOCATED;
  61. public:
  62. typedef void (*WorkerFunction)(Type*);
  63. ParallelJobs(WorkerFunction func, int requestedJobNumber) :
  64. m_parallelEnvironment(reinterpret_cast<ParallelEnvironment::ThreadFunction>(func), sizeof(Type), requestedJobNumber)
  65. {
  66. m_parameters.grow(m_parallelEnvironment.numberOfJobs());
  67. ASSERT(numberOfJobs() == m_parameters.size());
  68. }
  69. size_t numberOfJobs()
  70. {
  71. return m_parameters.size();
  72. }
  73. Type& parameter(size_t i)
  74. {
  75. return m_parameters[i];
  76. }
  77. void execute()
  78. {
  79. m_parallelEnvironment.execute(reinterpret_cast<unsigned char*>(m_parameters.data()));
  80. }
  81. private:
  82. ParallelEnvironment m_parallelEnvironment;
  83. Vector<Type> m_parameters;
  84. };
  85. } // namespace WTF
  86. using WTF::ParallelJobs;
  87. #endif // ParallelJobs_h