diff options
Diffstat (limited to 'include/utils')
-rw-r--r-- | include/utils/SkCondVar.h | 45 | ||||
-rw-r--r-- | include/utils/SkCountdown.h | 36 | ||||
-rw-r--r-- | include/utils/SkRunnable.h | 16 | ||||
-rw-r--r-- | include/utils/SkThreadPool.h | 50 |
4 files changed, 147 insertions, 0 deletions
diff --git a/include/utils/SkCondVar.h b/include/utils/SkCondVar.h new file mode 100644 index 0000000000..d2539e4b9e --- /dev/null +++ b/include/utils/SkCondVar.h @@ -0,0 +1,45 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkCondVar_DEFINED +#define SkCondVar_DEFINED + +#include <pthread.h> + +class SkCondVar { +public: + SkCondVar(); + ~SkCondVar(); + + void lock(); + void unlock(); + + /** + * Pause the calling thread. Will be awoken when signal() is called. + * Must be called while lock() is held (but gives it up while waiting). + */ + void wait(); + + /** + * Wake one thread waiting on this condition. Must be called while lock() + * is held. + */ + void signal(); + + /** + * Wake all threads waiting on this condition. Must be called while lock() + * is held. + */ + void broadcast(); + +private: + // FIXME: Make this independent of pthreads. + pthread_mutex_t fMutex; + pthread_cond_t fCond; +}; + +#endif diff --git a/include/utils/SkCountdown.h b/include/utils/SkCountdown.h new file mode 100644 index 0000000000..6bcec7d5ff --- /dev/null +++ b/include/utils/SkCountdown.h @@ -0,0 +1,36 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkCountdown_DEFINED +#define SkCountdown_DEFINED + +#include "SkCondVar.h" +#include "SkRunnable.h" +#include "SkTypes.h" + +class SkCountdown : public SkRunnable { +public: + explicit SkCountdown(int32_t count); + + /** + * Resets the countdown to the count provided. + */ + void reset(int32_t count); + + virtual void run() SK_OVERRIDE; + + /** + * Blocks until run() has been called count times. + */ + void wait(); + +private: + SkCondVar fReady; + int32_t fCount; +}; + +#endif diff --git a/include/utils/SkRunnable.h b/include/utils/SkRunnable.h new file mode 100644 index 0000000000..c7bedcc979 --- /dev/null +++ b/include/utils/SkRunnable.h @@ -0,0 +1,16 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkRunnable_DEFINED +#define SkRunnable_DEFINED + +class SkRunnable { +public: + virtual void run() = 0; +}; + +#endif diff --git a/include/utils/SkThreadPool.h b/include/utils/SkThreadPool.h new file mode 100644 index 0000000000..a96f87b275 --- /dev/null +++ b/include/utils/SkThreadPool.h @@ -0,0 +1,50 @@ +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkThreadPool_DEFINED +#define SkThreadPool_DEFINED + +#include "SkCondVar.h" +#include "SkTDArray.h" +#include "SkTDLinkedList.h" + +class SkRunnable; +class SkThread; + +class SkThreadPool { + +public: + /** + * Create a threadpool with exactly count (>=0) threads. + */ + explicit SkThreadPool(int count); + ~SkThreadPool(); + + /** + * Queues up an SkRunnable to run when a thread is available, or immediately if + * count is 0. NULL is a safe no-op. Does not take ownership. + */ + void add(SkRunnable*); + + private: + struct LinkedRunnable { + // Unowned pointer. + SkRunnable* fRunnable; + + private: + SK_DEFINE_DLINKEDLIST_INTERFACE(LinkedRunnable) + }; + + SkTDLinkedList<LinkedRunnable> fQueue; + SkCondVar fReady; + SkTDArray<SkThread*> fThreads; + bool fDone; + + static void Loop(void*); // Static because we pass in this. +}; + +#endif |