| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Simple thread pool class | 5 // Simple thread pool class |
| 6 | 6 |
| 7 #ifndef LIBRARIES_SDK_UTIL_THREAD_POOL_H_ | 7 #ifndef LIBRARIES_SDK_UTIL_THREAD_POOL_H_ |
| 8 #define LIBRARIES_SDK_UTIL_THREAD_POOL_H_ | 8 #define LIBRARIES_SDK_UTIL_THREAD_POOL_H_ |
| 9 | 9 |
| 10 #include <pthread.h> | 10 #include <pthread.h> |
| 11 #include <semaphore.h> | 11 #include <semaphore.h> |
| 12 | 12 |
| 13 #include "sdk_util/atomicops.h" |
| 14 |
| 13 // typdef helper for work function | 15 // typdef helper for work function |
| 14 typedef void (*WorkFunction)(int task_index, void* data); | 16 typedef void (*WorkFunction)(int task_index, void* data); |
| 15 | 17 |
| 16 // ThreadPool is a class to manage num_threads and assign | 18 // ThreadPool is a class to manage num_threads and assign |
| 17 // them num_tasks of work at a time. Each call | 19 // them num_tasks of work at a time. Each call |
| 18 // to Dispatch(..) will block until all tasks complete. | 20 // to Dispatch(..) will block until all tasks complete. |
| 19 // If 0 is passed in for num_threads, all tasks will be | 21 // If 0 is passed in for num_threads, all tasks will be |
| 20 // issued on the dispatch thread. | 22 // issued on the dispatch thread. |
| 21 | 23 |
| 22 class ThreadPool { | 24 class ThreadPool { |
| 23 public: | 25 public: |
| 24 void Dispatch(int num_tasks, WorkFunction work, void* data); | 26 void Dispatch(int num_tasks, WorkFunction work, void* data); |
| 25 explicit ThreadPool(int num_threads); | 27 explicit ThreadPool(int num_threads); |
| 26 ~ThreadPool(); | 28 ~ThreadPool(); |
| 27 private: | 29 private: |
| 28 int DecCounter(); | 30 int DecCounter(); |
| 29 void Setup(int counter, WorkFunction work, void* data); | 31 void Setup(int counter, WorkFunction work, void* data); |
| 30 void DispatchMany(int num_tasks, WorkFunction work, void* data); | 32 void DispatchMany(int num_tasks, WorkFunction work, void* data); |
| 31 void DispatchHere(int num_tasks, WorkFunction work, void* data); | 33 void DispatchHere(int num_tasks, WorkFunction work, void* data); |
| 32 void WorkLoop(); | 34 void WorkLoop(); |
| 33 static void* WorkerThreadEntry(void* data); | 35 static void* WorkerThreadEntry(void* data); |
| 34 void PostExitAndJoinAll(); | 36 void PostExitAndJoinAll(); |
| 35 pthread_t* threads_; | 37 pthread_t* threads_; |
| 36 int counter_; | 38 Atomic32 counter_; |
| 37 const int num_threads_; | 39 const int num_threads_; |
| 38 bool exiting_; | 40 bool exiting_; |
| 39 void* user_data_; | 41 void* user_data_; |
| 40 WorkFunction user_work_function_; | 42 WorkFunction user_work_function_; |
| 41 sem_t work_sem_; | 43 sem_t work_sem_; |
| 42 sem_t done_sem_; | 44 sem_t done_sem_; |
| 43 }; | 45 }; |
| 44 #endif // LIBRARIES_SDK_UTIL_THREAD_POOL_H_ | 46 #endif // LIBRARIES_SDK_UTIL_THREAD_POOL_H_ |
| 45 | 47 |
| OLD | NEW |