Home | History | Annotate | Download | only in utils
      1 /*
      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"
     13 #include "SkTInternalLList.h"
     14 
     15 class SkRunnable;
     16 class SkThread;
     17 
     18 class SkThreadPool {
     19 
     20 public:
     21     /**
     22      * Create a threadpool with count threads, or one thread per core if kThreadPerCore.
     23      */
     24     static const int kThreadPerCore = -1;
     25     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:
     40         SK_DECLARE_INTERNAL_LLIST_INTERFACE(LinkedRunnable);
     41     };
     42 
     43     SkTInternalLList<LinkedRunnable>    fQueue;
     44     SkCondVar                           fReady;
     45     SkTDArray<SkThread*>                fThreads;
     46     bool                            fDone;
     47 
     48     static void Loop(void*);  // Static because we pass in this.
     49 };
     50 
     51 #endif
     52