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

Unified Diff: runtime/bin/thread_pool.h

Issue 9212043: Refactored thread pool (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 11 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: runtime/bin/thread_pool.h
diff --git a/runtime/bin/thread_pool.h b/runtime/bin/thread_pool.h
index 3eaa2e4901e04a81666c9ff1179b581e5d520fe1..bf15cb51f893449aac8e8f78cd12eec290b4dc4d 100644
--- a/runtime/bin/thread_pool.h
+++ b/runtime/bin/thread_pool.h
@@ -9,72 +9,64 @@
#include "platform/globals.h"
#include "platform/thread.h"
-typedef void* Task;
-
-
-class TaskQueueEntry {
+class ThreadPool {
public:
- explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {}
-
- Task task() { return task_; }
-
- TaskQueueEntry* next() { return next_; }
- void set_next(TaskQueueEntry* value) { next_ = value; }
-
- private:
- Task task_;
- TaskQueueEntry* next_;
-};
+ typedef void* Task;
+ typedef void (*TaskHandler)(Task args);
+ ThreadPool(TaskHandler task_handler, int initial_size = 4)
+ : initial_size_(initial_size),
+ terminate_(false),
+ drain_(false),
+ size_(0),
+ head_(NULL),
+ tail_(NULL),
+ task_handler_(task_handler) {}
-// The task queue is a single linked list. Link direction is from tail
-// to head. New entried are inserted at the tail and entries are
-// removed from the head.
-class TaskQueue {
- public:
- TaskQueue() : terminate_(false), head_(NULL), tail_(NULL) {}
+ // Start the thread pool running.
Mads Ager (google) 2012/01/24 12:29:39 Remove ' running'
Søren Gjesse 2012/01/24 13:04:35 Done.
+ void Start();
+ // Shutdown the thread pool. If drain is set to true all tasks
Mads Ager (google) 2012/01/24 12:29:39 I would put a blank line here.
Søren Gjesse 2012/01/24 13:04:35 Done.
+ // pending in the queue will be processed. When this function
+ // returns all threads are terminated.
+ void Shutdown(bool drain = false);
Mads Ager (google) 2012/01/24 12:29:39 How about an enum instead of the bool: kDrain, kDo
Søren Gjesse 2012/01/24 13:04:35 Done.
- void Insert(TaskQueueEntry* task);
- TaskQueueEntry* Remove();
- void Shutdown();
+ // Insert a new task into the thread pool. Returns true on success.
+ bool InsertTask(Task task);
private:
- bool terminate_;
- TaskQueueEntry* head_;
- TaskQueueEntry* tail_;
- dart::Monitor monitor_;
-
- DISALLOW_COPY_AND_ASSIGN(TaskQueue);
-};
+ class TaskQueueEntry {
+ public:
+ explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {}
+ Task task() { return task_; }
+ TaskQueueEntry* next() { return next_; }
+ void set_next(TaskQueueEntry* value) { next_ = value; }
-class ThreadPool {
- public:
- typedef void* (*TaskHandler)(void* args);
-
- ThreadPool(TaskHandler task_handler, int initial_size = 4)
- : terminate_(false),
- size_(initial_size),
- task_handler_(task_handler) {}
+ private:
+ Task task_;
+ TaskQueueEntry* next_;
- void Start();
- void Shutdown();
+ DISALLOW_COPY_AND_ASSIGN(TaskQueueEntry);
+ };
- void InsertTask(Task task);
+ TaskQueueEntry* WaitForTask();
void ThreadTerminated();
- private:
- Task WaitForTask();
-
static void Main(uword args);
- TaskQueue queue_;
- // TODO(sgjesse): Move the monitor in TaskQueue to ThreadPool and
- // obtain it for updating terminate_.
- dart::Monitor monitor_;
- bool terminate_;
- int size_; // Number of threads.
+ dart::Monitor monitor_; // Monitor protecting all shared state.
+
+ int initial_size_; // Initial number of threads to start.
+ bool terminate_; // Set to true when the thread pool is terminating.
+ bool drain_; // Process all queue entries before termination.
+ int size_; // Current number of threads.
+
+ // The task queue is a single linked list. Link direction is from tail
+ // to head. New entried are inserted at the tail and entries are
Mads Ager (google) 2012/01/24 12:29:39 entries
Søren Gjesse 2012/01/24 13:04:35 Done.
+ // removed from the head.
+ TaskQueueEntry* head_;
+ TaskQueueEntry* tail_;
TaskHandler task_handler_;
DISALLOW_COPY_AND_ASSIGN(ThreadPool);

Powered by Google App Engine
This is Rietveld 408576698