]>
Commit | Line | Data |
---|---|---|
14957cd0 A |
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 ParallelJobsGeneric_h | |
29 | #define ParallelJobsGeneric_h | |
30 | ||
31 | #if ENABLE(THREADING_GENERIC) | |
32 | ||
33 | #include <wtf/RefCounted.h> | |
34 | #include <wtf/Threading.h> | |
35 | ||
36 | namespace WTF { | |
37 | ||
38 | static const unsigned int maxParallelThreads = 2; | |
39 | ||
40 | class ParallelEnvironment { | |
41 | WTF_MAKE_FAST_ALLOCATED; | |
42 | public: | |
43 | typedef void (*ThreadFunction)(void*); | |
44 | ||
45 | ParallelEnvironment(ThreadFunction threadFunction, size_t sizeOfParameter, unsigned int requestedJobNumber) : | |
46 | m_threadFunction(threadFunction), | |
47 | m_sizeOfParameter(sizeOfParameter) | |
48 | { | |
49 | if (!requestedJobNumber || requestedJobNumber > maxParallelThreads) | |
50 | requestedJobNumber = maxParallelThreads; | |
51 | ||
52 | if (!s_threadPool) | |
53 | s_threadPool = new Vector< RefPtr<ThreadPrivate> >(); | |
54 | ||
55 | // The main thread should be also a worker. | |
56 | unsigned int maxNewThreads = requestedJobNumber - 1; | |
57 | ||
58 | for (unsigned int i = 0; i < maxParallelThreads && m_threads.size() < maxNewThreads; ++i) { | |
59 | if (s_threadPool->size() < i + 1) | |
60 | s_threadPool->append(ThreadPrivate::create()); | |
61 | ||
62 | if ((*s_threadPool)[i]->tryLockFor(this)) | |
63 | m_threads.append((*s_threadPool)[i]); | |
64 | } | |
65 | ||
66 | m_numberOfJobs = m_threads.size() + 1; | |
67 | } | |
68 | ||
69 | int numberOfJobs() | |
70 | { | |
71 | return m_numberOfJobs; | |
72 | } | |
73 | ||
74 | void execute(unsigned char* parameters) | |
75 | { | |
76 | size_t i; | |
77 | for (i = 0; i < m_threads.size(); ++i) { | |
78 | m_threads[i]->execute(m_threadFunction, parameters); | |
79 | parameters += m_sizeOfParameter; | |
80 | } | |
81 | ||
82 | // The work for the main thread | |
83 | (*m_threadFunction)(parameters); | |
84 | ||
85 | // Wait until all jobs are done. | |
86 | for (i = 0; i < m_threads.size(); ++i) | |
87 | m_threads[i]->waitForFinish(); | |
88 | } | |
89 | ||
90 | class ThreadPrivate : public RefCounted<ThreadPrivate> { | |
91 | public: | |
92 | ThreadPrivate() | |
93 | : m_threadID(0) | |
94 | , m_running(false) | |
95 | , m_parent(0) | |
96 | { | |
97 | } | |
98 | ||
99 | bool tryLockFor(ParallelEnvironment*); | |
100 | ||
101 | void execute(ThreadFunction, void*); | |
102 | ||
103 | void waitForFinish(); | |
104 | ||
105 | static PassRefPtr<ThreadPrivate> create() | |
106 | { | |
107 | return adoptRef(new ThreadPrivate()); | |
108 | } | |
109 | ||
110 | static void* workerThread(void*); | |
111 | ||
112 | private: | |
113 | ThreadIdentifier m_threadID; | |
114 | bool m_running; | |
115 | ParallelEnvironment* m_parent; | |
116 | ||
117 | mutable Mutex m_mutex; | |
118 | ThreadCondition m_threadCondition; | |
119 | ||
120 | ThreadFunction m_threadFunction; | |
121 | void* m_parameters; | |
122 | }; | |
123 | ||
124 | private: | |
125 | ThreadFunction m_threadFunction; | |
126 | size_t m_sizeOfParameter; | |
127 | int m_numberOfJobs; | |
128 | ||
129 | Vector< RefPtr<ThreadPrivate> > m_threads; | |
130 | static Vector< RefPtr<ThreadPrivate> >* s_threadPool; | |
131 | }; | |
132 | ||
133 | } // namespace WTF | |
134 | ||
135 | #endif // ENABLE(THREADING_GENERIC) | |
136 | ||
137 | ||
138 | #endif // ParallelJobsGeneric_h |