blob: 5fe10255b5ae599945cd4fa3c5051a35dcdb05f3 [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
scroggo@google.com4177ef42012-10-31 15:52:16 +00008#include "SkRunnable.h"
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +00009#include "SkThreadPool.h"
scroggo@google.com4177ef42012-10-31 15:52:16 +000010#include "SkThreadUtils.h"
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000011#include "SkTypes.h"
scroggo@google.com4177ef42012-10-31 15:52:16 +000012
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000013#if defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_ANDROID)
14#include <unistd.h>
15#endif
16
17// Returns the number of cores on this machine.
18static int num_cores() {
19#if defined(SK_BUILD_FOR_WIN32)
20 SYSTEM_INFO sysinfo;
21 GetSystemInfo(&sysinfo);
22 return sysinfo.dwNumberOfProcessors;
23#elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_ANDROID)
24 return sysconf(_SC_NPROCESSORS_ONLN);
25#else
26 return 1;
27#endif
28}
29
30SkThreadPool::SkThreadPool(int count)
scroggo@google.com4177ef42012-10-31 15:52:16 +000031: fDone(false) {
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000032 if (count < 0) count = num_cores();
scroggo@google.com4177ef42012-10-31 15:52:16 +000033 // Create count threads, all running SkThreadPool::Loop.
34 for (int i = 0; i < count; i++) {
35 SkThread* thread = SkNEW_ARGS(SkThread, (&SkThreadPool::Loop, this));
36 *fThreads.append() = thread;
37 thread->start();
38 }
39}
40
41SkThreadPool::~SkThreadPool() {
42 fDone = true;
43 fReady.lock();
44 fReady.broadcast();
45 fReady.unlock();
46
47 // Wait for all threads to stop.
48 for (int i = 0; i < fThreads.count(); i++) {
49 fThreads[i]->join();
50 SkDELETE(fThreads[i]);
51 }
52}
53
54/*static*/ void SkThreadPool::Loop(void* arg) {
55 // The SkThreadPool passes itself as arg to each thread as they're created.
56 SkThreadPool* pool = static_cast<SkThreadPool*>(arg);
57
58 while (true) {
59 // We have to be holding the lock to read the queue and to call wait.
60 pool->fReady.lock();
61 while(pool->fQueue.isEmpty()) {
62 // Is it time to die?
63 if (pool->fDone) {
64 pool->fReady.unlock();
65 return;
66 }
67 // wait yields the lock while waiting, but will have it again when awoken.
68 pool->fReady.wait();
69 }
70 // We've got the lock back here, no matter if we ran wait or not.
71
72 // The queue is not empty, so we have something to run. Claim it.
73 LinkedRunnable* r = pool->fQueue.tail();
74
75 pool->fQueue.remove(r);
76
77 // Having claimed our SkRunnable, we now give up the lock while we run it.
78 // Otherwise, we'd only ever do work on one thread at a time, which rather
79 // defeats the point of this code.
80 pool->fReady.unlock();
81
82 // OK, now really do the work.
83 r->fRunnable->run();
84 SkDELETE(r);
85 }
86
87 SkASSERT(false); // Unreachable. The only exit happens when pool->fDone.
88}
89
90void SkThreadPool::add(SkRunnable* r) {
91 if (NULL == r) {
92 return;
93 }
94
95 // If we don't have any threads, obligingly just run the thing now.
96 if (fThreads.isEmpty()) {
97 return r->run();
98 }
99
100 // We have some threads. Queue it up!
101 fReady.lock();
102 LinkedRunnable* linkedRunnable = SkNEW(LinkedRunnable);
103 linkedRunnable->fRunnable = r;
104 fQueue.addToHead(linkedRunnable);
105 fReady.signal();
106 fReady.unlock();
107}