blob: cc45fc2cd53f9f2317c7301ad25bd9b181bb0fbb [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 /**
22 * Create a threadpool with exactly count (>=0) threads.
23 */
24 explicit SkThreadPool(int count);
25 ~SkThreadPool();
26
27 /**
28 * Queues up an SkRunnable to run when a thread is available, or immediately if
29 * count is 0. NULL is a safe no-op. Does not take ownership.
30 */
31 void add(SkRunnable*);
32
33 private:
34 struct LinkedRunnable {
35 // Unowned pointer.
36 SkRunnable* fRunnable;
37
38 private:
bsalomon@google.com42619d82012-12-03 14:54:59 +000039 SK_DECLARE_INTERNAL_LLIST_INTERFACE(LinkedRunnable);
scroggo@google.com4177ef42012-10-31 15:52:16 +000040 };
41
bsalomon@google.com42619d82012-12-03 14:54:59 +000042 SkTInternalLList<LinkedRunnable> fQueue;
43 SkCondVar fReady;
44 SkTDArray<SkThread*> fThreads;
45 bool fDone;
scroggo@google.com4177ef42012-10-31 15:52:16 +000046
47 static void Loop(void*); // Static because we pass in this.
48};
49
50#endif