OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Simple thread pool class |
| 6 |
| 7 #ifndef EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ |
| 8 #define EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ |
| 9 |
| 10 #if !defined(DISABLE_THREADS) |
| 11 #include <pthread.h> |
| 12 #include <semaphore.h> |
| 13 #endif |
| 14 |
| 15 // typdef helper for work function |
| 16 typedef void (*WorkFunction)(int task_index, void *data); |
| 17 |
| 18 // ThreadPool is a class to manage num_threads and assign |
| 19 // them num_tasks of work at a time. Each call |
| 20 // to Dispatch(..) will block until all tasks complete. |
| 21 |
| 22 class ThreadPool { |
| 23 #if !defined(DISABLE_THREADS) |
| 24 public: |
| 25 void Dispatch(int num_tasks, WorkFunction work, void *data); |
| 26 explicit ThreadPool(int num_threads); |
| 27 ~ThreadPool(); |
| 28 private: |
| 29 int DecCounter(); |
| 30 void Setup(int counter, WorkFunction work, void *data); |
| 31 void MultiThread(int num_tasks, WorkFunction work, void *data); |
| 32 void SingleThread(int num_tasks, WorkFunction work, void *data); |
| 33 void WorkLoop(); |
| 34 static void* WorkerThreadEntry(void *data); |
| 35 void PostExitAndJoinAll(); |
| 36 pthread_t *threads_; |
| 37 int counter_; |
| 38 int num_threads_; |
| 39 bool exiting_; |
| 40 void *user_data_; |
| 41 WorkFunction user_work_function_; |
| 42 pthread_mutex_t mutex_; |
| 43 sem_t work_tasks_; |
| 44 sem_t done_tasks_; |
| 45 #else |
| 46 public: |
| 47 void Dispatch(int num_tasks, WorkFunction work, void *data) { |
| 48 for(int i = 0; i < num_tasks; i++) work(i, data); } |
| 49 explicit ThreadPool(int num_threads) { ; } |
| 50 ~ThreadPool() { ; } |
| 51 #endif // DISABLE_THREADS |
| 52 }; |
| 53 #endif // EXAMPLES_DEMO_VORONOI_THREADPOOL_H_ |
OLD | NEW |