diff options
author | scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-10-31 15:52:16 +0000 |
---|---|---|
committer | scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-10-31 15:52:16 +0000 |
commit | 4177ef4b229b5fb67f355569654981bb4bf8eb9c (patch) | |
tree | 0091c5ff6ed7dd7b89e73f9b11cde51e700c5726 /include | |
parent | 72ba668db833d25ecdca4edfbefd601e508a1e62 (diff) |
Add SkThreadPool for managing threads.
Skia-ized from https://codereview.appspot.com/6755043/
TODO: Use SkThread and platform independent features.
Review URL: https://codereview.appspot.com/6777064
git-svn-id: http://skia.googlecode.com/svn/trunk@6217 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include')
-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 |