blob: 3c8615809089a21d1076c62642f3ba4d9b218cd3 [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"
12#include "SkTDArray.h"
bsalomon@google.com42619d82012-12-03 14:54:59 +000013#include "SkTInternalLList.h"
scroggo@google.com4177ef42012-10-31 15:52:16 +000014
15class SkRunnable;
16class SkThread;
17
18class SkThreadPool {
19
20public:
21 /**
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000022 * Create a threadpool with count threads, or one thread per core if kThreadPerCore.
scroggo@google.com4177ef42012-10-31 15:52:16 +000023 */
commit-bot@chromium.org44c661f2013-04-22 15:23:14 +000024 static const int kThreadPerCore = -1;
scroggo@google.com4177ef42012-10-31 15:52:16 +000025 explicit SkThreadPool(int count);
26 ~SkThreadPool();
27
28 /**
29 * Queues up an SkRunnable to run when a thread is available, or immediately if
30 * count is 0. NULL is a safe no-op. Does not take ownership.
31 */
32 void add(SkRunnable*);
33
34 private:
35 struct LinkedRunnable {
36 // Unowned pointer.
37 SkRunnable* fRunnable;
38
39 private:
bsalomon@google.com42619d82012-12-03 14:54:59 +000040 SK_DECLARE_INTERNAL_LLIST_INTERFACE(LinkedRunnable);
scroggo@google.com4177ef42012-10-31 15:52:16 +000041 };
42
bsalomon@google.com42619d82012-12-03 14:54:59 +000043 SkTInternalLList<LinkedRunnable> fQueue;
44 SkCondVar fReady;
45 SkTDArray<SkThread*> fThreads;
46 bool fDone;
scroggo@google.com4177ef42012-10-31 15:52:16 +000047
48 static void Loop(void*); // Static because we pass in this.
49};
50
51#endif