Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Unified Diff: native_client_sdk/src/examples/demo/voronoi/threadpool.cc

Issue 16266004: Change default to not use pthreads at startup (all calculations on main thread) (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/examples/demo/voronoi/threadpool.cc
===================================================================
--- native_client_sdk/src/examples/demo/voronoi/threadpool.cc (revision 203001)
+++ native_client_sdk/src/examples/demo/voronoi/threadpool.cc (working copy)
@@ -9,7 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
-// TODO(nfullagar): Switch DecCounter to use atomic decrement.
+#include "utils/auto_lock.h"
// Initializes mutex, semaphores and a pool of threads. If 0 is passed for
// num_threads, all work will be performed on the dispatch thread.
@@ -18,11 +18,6 @@
user_data_(NULL), user_work_function_(NULL) {
if (num_threads_ > 0) {
int status;
- status = pthread_mutex_init(&mutex_, NULL);
- if (0 != status) {
- fprintf(stderr, "Failed to initialize mutex!\n");
- exit(-1);
- }
status = sem_init(&work_sem_, 0, 0);
if (-1 == status) {
fprintf(stderr, "Failed to initialize semaphore!\n");
@@ -46,12 +41,11 @@
// Post exit request, wait for all threads to join, and cleanup.
ThreadPool::~ThreadPool() {
- if (num_threads_ > 1) {
+ if (num_threads_ > 0) {
PostExitAndJoinAll();
delete[] threads_;
sem_destroy(&done_sem_);
sem_destroy(&work_sem_);
- pthread_mutex_destroy(&mutex_);
}
}
@@ -63,16 +57,18 @@
user_data_ = data;
}
-// Decrement and get the value of the mutex protected counter. This function
+// Return decremented task counter. This function
// can be called from multiple threads at any given time.
int ThreadPool::DecCounter() {
- int v;
- pthread_mutex_lock(&mutex_);
- {
- v = --counter_;
- }
- pthread_mutex_unlock(&mutex_);
- return v;
+#if defined(__native_client__)
+ // Use fast atomic sub & fetch.
+ return __sync_sub_and_fetch(&counter_, 1);
+#else
+ // Fallback to a more platform independent pthread mutex via AutoLock.
+ static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+ AutoLock lock(&mutex);
+ return --counter_;
+#endif
}
// Set exit flag, post and join all the threads in the pool. This function is
« no previous file with comments | « native_client_sdk/src/examples/demo/voronoi/threadpool.h ('k') | native_client_sdk/src/examples/demo/voronoi/voronoi.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698