]> git.saurik.com Git - apple/javascriptcore.git/blob - wtf/ParallelJobs.h
2e0524dc519c402b8a54c2ee05299e6b1ae6ecfc
[apple/javascriptcore.git] / wtf / ParallelJobs.h
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
28 #ifndef ParallelJobs_h
29 #define ParallelJobs_h
30
31 #include "Assertions.h"
32 #include "Noncopyable.h"
33 #include "RefPtr.h"
34 #include <wtf/Vector.h>
35
36 #if ENABLE(PARALLEL_JOBS)
37
38 // Usage:
39 //
40 // #if ENABLE(PARALLEL_JOBS)
41 //
42 // // Initialize parallel jobs
43 // ParallelJobs<TypeOfParameter> parallelJobs(&worker [, requestedNumberOfJobs]);
44 //
45 // // Fill the parameter array
46 // for(i = 0; i < parallelJobs.numberOfJobs(); ++i) {
47 // TypeOfParameter& params = parallelJobs.parameter(i);
48 // params.attr1 = localVars ...
49 // ...
50 // }
51 //
52 // // Execute parallel jobs
53 // parallelJobs.execute();
54 //
55 // #else
56 //
57 // inlineFunction(args...);
58 //
59 // #endif // ENABLE(PARALLEL_JOBS)
60
61 #if ENABLE(THREADING_GENERIC)
62 #include "ParallelJobsGeneric.h"
63
64 #elif ENABLE(THREADING_OPENMP)
65 #include "ParallelJobsOpenMP.h"
66
67 #elif ENABLE(THREADING_LIBDISPATCH)
68 #include "ParallelJobsLibdispatch.h"
69
70 #else
71 #error "No parallel processing API for ParallelJobs"
72
73 #endif
74
75 namespace WTF {
76
77 template<typename Type>
78 class ParallelJobs {
79 WTF_MAKE_FAST_ALLOCATED;
80 public:
81 typedef void (*WorkerFunction)(Type*);
82
83 ParallelJobs(WorkerFunction func, int requestedJobNumber = 0) :
84 m_parallelEnvironment(reinterpret_cast<ParallelEnvironment::ThreadFunction>(func), sizeof(Type), requestedJobNumber)
85 {
86 m_parameters.grow(m_parallelEnvironment.numberOfJobs());
87 ASSERT(numberOfJobs() == m_parameters.size());
88 }
89
90 size_t numberOfJobs()
91 {
92 return m_parameters.size();
93 }
94
95 Type& parameter(size_t i)
96 {
97 return m_parameters[i];
98 }
99
100 void execute()
101 {
102 m_parallelEnvironment.execute(reinterpret_cast<unsigned char*>(m_parameters.data()));
103 }
104
105 private:
106 ParallelEnvironment m_parallelEnvironment;
107 Vector<Type> m_parameters;
108 };
109
110 } // namespace WTF
111
112 using WTF::ParallelJobs;
113
114 #endif // ENABLE(PARALLEL_JOBS)
115
116 #endif // ParallelJobs_h