| 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 #include "sdk_util/thread_pool.h" | 5 #include "sdk_util/thread_pool.h" |
| 6 | 6 |
| 7 #include <pthread.h> | 7 #include <pthread.h> |
| 8 #include <semaphore.h> | 8 #include <semaphore.h> |
| 9 #include <stdio.h> | 9 #include <stdio.h> |
| 10 #include <stdlib.h> | 10 #include <stdlib.h> |
| 11 | 11 |
| 12 #include "sdk_util/auto_lock.h" | 12 #include "sdk_util/auto_lock.h" |
| 13 | 13 |
| 14 namespace sdk_util { |
| 15 |
| 14 // Initializes mutex, semaphores and a pool of threads. If 0 is passed for | 16 // Initializes mutex, semaphores and a pool of threads. If 0 is passed for |
| 15 // num_threads, all work will be performed on the dispatch thread. | 17 // num_threads, all work will be performed on the dispatch thread. |
| 16 ThreadPool::ThreadPool(int num_threads) | 18 ThreadPool::ThreadPool(int num_threads) |
| 17 : threads_(NULL), counter_(0), num_threads_(num_threads), exiting_(false), | 19 : threads_(NULL), counter_(0), num_threads_(num_threads), exiting_(false), |
| 18 user_data_(NULL), user_work_function_(NULL) { | 20 user_data_(NULL), user_work_function_(NULL) { |
| 19 if (num_threads_ > 0) { | 21 if (num_threads_ > 0) { |
| 20 int status; | 22 int status; |
| 21 status = sem_init(&work_sem_, 0, 0); | 23 status = sem_init(&work_sem_, 0, 0); |
| 22 if (-1 == status) { | 24 if (-1 == status) { |
| 23 fprintf(stderr, "Failed to initialize semaphore!\n"); | 25 fprintf(stderr, "Failed to initialize semaphore!\n"); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 // Dispatch() will invoke the user supplied work function across | 129 // Dispatch() will invoke the user supplied work function across |
| 128 // one or more threads for each task. | 130 // one or more threads for each task. |
| 129 // Note: This function will block until all work has completed. | 131 // Note: This function will block until all work has completed. |
| 130 void ThreadPool::Dispatch(int num_tasks, WorkFunction work, void* data) { | 132 void ThreadPool::Dispatch(int num_tasks, WorkFunction work, void* data) { |
| 131 if (num_threads_ > 0) | 133 if (num_threads_ > 0) |
| 132 DispatchMany(num_tasks, work, data); | 134 DispatchMany(num_tasks, work, data); |
| 133 else | 135 else |
| 134 DispatchHere(num_tasks, work, data); | 136 DispatchHere(num_tasks, work, data); |
| 135 } | 137 } |
| 136 | 138 |
| 139 } // namespace sdk_util |
| 140 |
| OLD | NEW |