blob: a75bed8be44f538eeb87d3c21658f2de83290618 [file] [log] [blame]
scroggo@google.com4177ef42012-10-31 15:52:16 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkThreadPool_DEFINED
9#define SkThreadPool_DEFINED
10
11#include "SkCondVar.h"
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +000012#include "SkRunnable.h"
scroggo@google.com4177ef42012-10-31 15:52:16 +000013#include "SkTDArray.h"
bsalomon@google.com42619d82012-12-03 14:54:59 +000014#include "SkTInternalLList.h"
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000015#include "SkThreadUtils.h"
16#include "SkTypes.h"
scroggo@google.com4177ef42012-10-31 15:52:16 +000017
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000018#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_ANDROID)
19# include <unistd.h>
20#endif
scroggo@google.com4177ef42012-10-31 15:52:16 +000021
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000022// Returns the number of cores on this machine.
23static inline int num_cores() {
24#if defined(SK_BUILD_FOR_WIN32)
25 SYSTEM_INFO sysinfo;
26 GetSystemInfo(&sysinfo);
27 return sysinfo.dwNumberOfProcessors;
28#elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_ANDROID)
29 return sysconf(_SC_NPROCESSORS_ONLN);
30#else
31 return 1;
32#endif
33}
scroggo@google.com4177ef42012-10-31 15:52:16 +000034
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000035template <typename T>
36class SkTThreadPool {
scroggo@google.com4177ef42012-10-31 15:52:16 +000037public:
38 /**
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000039 * Create a threadpool with count threads, or one thread per core if kThreadPerCore.
scroggo@google.com4177ef42012-10-31 15:52:16 +000040 */
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000041 static const int kThreadPerCore = -1;
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000042 explicit SkTThreadPool(int count);
43 ~SkTThreadPool();
scroggo@google.com4177ef42012-10-31 15:52:16 +000044
45 /**
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000046 * Queues up an SkRunnable to run when a thread is available, or synchronously if count is 0.
47 * Does not take ownership. NULL is a safe no-op. If T is not void, the runnable will be passed
48 * a reference to a T on the thread's local stack.
scroggo@google.com4177ef42012-10-31 15:52:16 +000049 */
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000050 void add(SkTRunnable<T>*);
scroggo@google.com4177ef42012-10-31 15:52:16 +000051
commit-bot@chromium.orga7538ba2013-10-10 18:49:04 +000052 /**
53 * Block until all added SkRunnables have completed. Once called, calling add() is undefined.
54 */
55 void wait();
56
scroggo@google.com4177ef42012-10-31 15:52:16 +000057 private:
58 struct LinkedRunnable {
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000059 SkTRunnable<T>* fRunnable; // Unowned.
bsalomon@google.com42619d82012-12-03 14:54:59 +000060 SK_DECLARE_INTERNAL_LLIST_INTERFACE(LinkedRunnable);
scroggo@google.com4177ef42012-10-31 15:52:16 +000061 };
62
commit-bot@chromium.org6ee68582013-10-18 14:19:19 +000063 enum State {
64 kRunning_State, // Normal case. We've been constructed and no one has called wait().
65 kWaiting_State, // wait has been called, but there still might be work to do or being done.
66 kHalting_State, // There's no work to do and no thread is busy. All threads can shut down.
67 };
68
69 SkTInternalLList<LinkedRunnable> fQueue;
70 SkCondVar fReady;
71 SkTDArray<SkThread*> fThreads;
72 State fState;
73 int fBusyThreads;
scroggo@google.com4177ef42012-10-31 15:52:16 +000074
75 static void Loop(void*); // Static because we pass in this.
76};
77
commit-bot@chromium.orgef57b7e2014-02-28 20:31:31 +000078template <typename T>
79SkTThreadPool<T>::SkTThreadPool(int count) : fState(kRunning_State), fBusyThreads(0) {
80 if (count < 0) {
81 count = num_cores();
82 }
83 // Create count threads, all running SkTThreadPool::Loop.
84 for (int i = 0; i < count; i++) {
85 SkThread* thread = SkNEW_ARGS(SkThread, (&SkTThreadPool::Loop, this));
86 *fThreads.append() = thread;
87 thread->start();
88 }
89}
90
91template <typename T>
92SkTThreadPool<T>::~SkTThreadPool() {
93 if (kRunning_State == fState) {
94 this->wait();
95 }
96}
97
98namespace SkThreadPoolPrivate {
99
100template <typename T>
101struct ThreadLocal {
102 void run(SkTRunnable<T>* r) { r->run(data); }
103 T data;
104};
105
106template <>
107struct ThreadLocal<void> {
108 void run(SkTRunnable<void>* r) { r->run(); }
109};
110
111} // namespace SkThreadPoolPrivate
112
113template <typename T>
114void SkTThreadPool<T>::add(SkTRunnable<T>* r) {
115 if (r == NULL) {
116 return;
117 }
118
119 if (fThreads.isEmpty()) {
120 SkThreadPoolPrivate::ThreadLocal<T> threadLocal;
121 threadLocal.run(r);
122 return;
123 }
124
125 LinkedRunnable* linkedRunnable = SkNEW(LinkedRunnable);
126 linkedRunnable->fRunnable = r;
127 fReady.lock();
128 SkASSERT(fState != kHalting_State); // Shouldn't be able to add work when we're halting.
129 fQueue.addToHead(linkedRunnable);
130 fReady.signal();
131 fReady.unlock();
132}
133
134
135template <typename T>
136void SkTThreadPool<T>::wait() {
137 fReady.lock();
138 fState = kWaiting_State;
139 fReady.broadcast();
140 fReady.unlock();
141
142 // Wait for all threads to stop.
143 for (int i = 0; i < fThreads.count(); i++) {
144 fThreads[i]->join();
145 SkDELETE(fThreads[i]);
146 }
147 SkASSERT(fQueue.isEmpty());
148}
149
150template <typename T>
151/*static*/ void SkTThreadPool<T>::Loop(void* arg) {
152 // The SkTThreadPool passes itself as arg to each thread as they're created.
153 SkTThreadPool<T>* pool = static_cast<SkTThreadPool<T>*>(arg);
154 SkThreadPoolPrivate::ThreadLocal<T> threadLocal;
155
156 while (true) {
157 // We have to be holding the lock to read the queue and to call wait.
158 pool->fReady.lock();
159 while(pool->fQueue.isEmpty()) {
160 // Does the client want to stop and are all the threads ready to stop?
161 // If so, we move into the halting state, and whack all the threads so they notice.
162 if (kWaiting_State == pool->fState && pool->fBusyThreads == 0) {
163 pool->fState = kHalting_State;
164 pool->fReady.broadcast();
165 }
166 // Any time we find ourselves in the halting state, it's quitting time.
167 if (kHalting_State == pool->fState) {
168 pool->fReady.unlock();
169 return;
170 }
171 // wait yields the lock while waiting, but will have it again when awoken.
172 pool->fReady.wait();
173 }
174 // We've got the lock back here, no matter if we ran wait or not.
175
176 // The queue is not empty, so we have something to run. Claim it.
177 LinkedRunnable* r = pool->fQueue.tail();
178
179 pool->fQueue.remove(r);
180
181 // Having claimed our SkRunnable, we now give up the lock while we run it.
182 // Otherwise, we'd only ever do work on one thread at a time, which rather
183 // defeats the point of this code.
184 pool->fBusyThreads++;
185 pool->fReady.unlock();
186
187 // OK, now really do the work.
188 threadLocal.run(r->fRunnable);
189 SkDELETE(r);
190
191 // Let everyone know we're not busy.
192 pool->fReady.lock();
193 pool->fBusyThreads--;
194 pool->fReady.unlock();
195 }
196
197 SkASSERT(false); // Unreachable. The only exit happens when pool->fState is kHalting_State.
198}
199
200typedef SkTThreadPool<void> SkThreadPool;
201
scroggo@google.com4177ef42012-10-31 15:52:16 +0000202#endif