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" | 13 #include "sdk_util/atomicops.h" |
14 | 14 |
| 15 namespace sdk_util { |
| 16 |
15 // typdef helper for work function | 17 // typdef helper for work function |
16 typedef void (*WorkFunction)(int task_index, void* data); | 18 typedef void (*WorkFunction)(int task_index, void* data); |
17 | 19 |
18 // ThreadPool is a class to manage num_threads and assign | 20 // ThreadPool is a class to manage num_threads and assign |
19 // them num_tasks of work at a time. Each call | 21 // them num_tasks of work at a time. Each call |
20 // to Dispatch(..) will block until all tasks complete. | 22 // to Dispatch(..) will block until all tasks complete. |
21 // If 0 is passed in for num_threads, all tasks will be | 23 // If 0 is passed in for num_threads, all tasks will be |
22 // issued on the dispatch thread. | 24 // issued on the dispatch thread. |
23 | 25 |
24 class ThreadPool { | 26 class ThreadPool { |
(...skipping 11 matching lines...) Expand all Loading... |
36 void PostExitAndJoinAll(); | 38 void PostExitAndJoinAll(); |
37 pthread_t* threads_; | 39 pthread_t* threads_; |
38 Atomic32 counter_; | 40 Atomic32 counter_; |
39 const int num_threads_; | 41 const int num_threads_; |
40 bool exiting_; | 42 bool exiting_; |
41 void* user_data_; | 43 void* user_data_; |
42 WorkFunction user_work_function_; | 44 WorkFunction user_work_function_; |
43 sem_t work_sem_; | 45 sem_t work_sem_; |
44 sem_t done_sem_; | 46 sem_t done_sem_; |
45 }; | 47 }; |
| 48 |
| 49 } // namespace sdk_util |
| 50 |
46 #endif // LIBRARIES_SDK_UTIL_THREAD_POOL_H_ | 51 #endif // LIBRARIES_SDK_UTIL_THREAD_POOL_H_ |
47 | 52 |
OLD | NEW |