Index: native_client_sdk/src/examples/demo/voronoi/threadpool.h |
=================================================================== |
--- native_client_sdk/src/examples/demo/voronoi/threadpool.h (revision 0) |
+++ native_client_sdk/src/examples/demo/voronoi/threadpool.h (revision 0) |
@@ -0,0 +1,53 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Simple thread pool class |
+ |
+#ifndef EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ |
+#define EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ |
+ |
+#if !defined(DISABLE_THREADS) |
+#include <pthread.h> |
+#include <semaphore.h> |
+#endif |
+ |
+// typdef helper for work function |
+typedef void (*WorkFunction)(int task_index, void *data); |
+ |
+// ThreadPool is a class to manage num_threads and assign |
+// them num_tasks of work at a time. Each call |
+// to Dispatch(..) will block until all tasks complete. |
+ |
+class ThreadPool { |
+#if !defined(DISABLE_THREADS) |
noelallen1
2013/05/25 00:21:11
I would recommend removing. We either should sup
nfullagar1
2013/05/28 23:11:50
removed
|
+ public: |
+ void Dispatch(int num_tasks, WorkFunction work, void *data); |
+ explicit ThreadPool(int num_threads); |
+ ~ThreadPool(); |
+ private: |
+ int DecCounter(); |
+ void Setup(int counter, WorkFunction work, void *data); |
+ void MultiThread(int num_tasks, WorkFunction work, void *data); |
+ void SingleThread(int num_tasks, WorkFunction work, void *data); |
+ void WorkLoop(); |
+ static void* WorkerThreadEntry(void *data); |
+ void PostExitAndJoinAll(); |
+ pthread_t *threads_; |
binji
2013/05/24 21:47:55
might want to run clang-format on this diff to fix
nfullagar1
2013/05/28 23:11:50
Done.
|
+ int counter_; |
+ int num_threads_; |
noelallen1
2013/05/25 00:21:11
Did you mean to make this one const?
nfullagar1
2013/05/28 23:11:50
yes! thx.
|
+ bool exiting_; |
+ void *user_data_; |
+ WorkFunction user_work_function_; |
+ pthread_mutex_t mutex_; |
+ sem_t work_tasks_; |
+ sem_t done_tasks_; |
+#else |
+ public: |
+ void Dispatch(int num_tasks, WorkFunction work, void *data) { |
+ for(int i = 0; i < num_tasks; i++) work(i, data); } |
binji
2013/05/24 21:47:55
share with SingleThread?
nfullagar1
2013/05/28 23:11:50
Fishing this out and sharing this one line of code
|
+ explicit ThreadPool(int num_threads) { ; } |
binji
2013/05/24 21:47:55
nit: {}
noelallen1
2013/05/25 00:21:11
A bit strange that in the DISABLE_THREADS case the
nfullagar1
2013/05/28 23:11:50
Done.
nfullagar1
2013/05/28 23:11:50
DISABLE_THREADS removed
|
+ ~ThreadPool() { ; } |
+#endif // DISABLE_THREADS |
+}; |
+#endif // EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ |
Property changes on: native_client_sdk/src/examples/demo/voronoi/threadpool.h |
___________________________________________________________________ |
Added: svn:executable |
+ * |