Chromium Code Reviews| 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> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 // when all worker threads are sleeping. | 53 // when all worker threads are sleeping. |
| 54 void ThreadPool::Setup(int counter, WorkFunction work, void *data) { | 54 void ThreadPool::Setup(int counter, WorkFunction work, void *data) { |
| 55 counter_ = counter; | 55 counter_ = counter; |
| 56 user_work_function_ = work; | 56 user_work_function_ = work; |
| 57 user_data_ = data; | 57 user_data_ = data; |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Return decremented task counter. This function | 60 // Return decremented task counter. This function |
| 61 // can be called from multiple threads at any given time. | 61 // can be called from multiple threads at any given time. |
| 62 int ThreadPool::DecCounter() { | 62 int ThreadPool::DecCounter() { |
| 63 #if defined(__native_client__) | 63 return AtomicAddFetch(&counter_, 1); |
|
nfullagar1
2013/07/18 00:18:06
This should return the decremented task counter, b
| |
| 64 // Use fast atomic sub & fetch. | |
| 65 return __sync_sub_and_fetch(&counter_, 1); | |
| 66 #else | |
| 67 // Fallback to a more platform independent pthread mutex via AutoLock. | |
| 68 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | |
| 69 AutoLock lock(&mutex); | |
| 70 return --counter_; | |
| 71 #endif | |
| 72 } | 64 } |
| 73 | 65 |
| 74 // Set exit flag, post and join all the threads in the pool. This function is | 66 // Set exit flag, post and join all the threads in the pool. This function is |
| 75 // called only from the dispatch thread, and only when all worker threads are | 67 // called only from the dispatch thread, and only when all worker threads are |
| 76 // sleeping. | 68 // sleeping. |
| 77 void ThreadPool::PostExitAndJoinAll() { | 69 void ThreadPool::PostExitAndJoinAll() { |
| 78 exiting_ = true; | 70 exiting_ = true; |
| 79 // Wake up all the sleeping worker threads. | 71 // Wake up all the sleeping worker threads. |
| 80 for (int i = 0; i < num_threads_; ++i) | 72 for (int i = 0; i < num_threads_; ++i) |
| 81 sem_post(&work_sem_); | 73 sem_post(&work_sem_); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 // Dispatch() will invoke the user supplied work function across | 127 // Dispatch() will invoke the user supplied work function across |
| 136 // one or more threads for each task. | 128 // one or more threads for each task. |
| 137 // Note: This function will block until all work has completed. | 129 // Note: This function will block until all work has completed. |
| 138 void ThreadPool::Dispatch(int num_tasks, WorkFunction work, void* data) { | 130 void ThreadPool::Dispatch(int num_tasks, WorkFunction work, void* data) { |
| 139 if (num_threads_ > 0) | 131 if (num_threads_ > 0) |
| 140 DispatchMany(num_tasks, work, data); | 132 DispatchMany(num_tasks, work, data); |
| 141 else | 133 else |
| 142 DispatchHere(num_tasks, work, data); | 134 DispatchHere(num_tasks, work, data); |
| 143 } | 135 } |
| 144 | 136 |
| OLD | NEW |