OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef BIN_THREAD_POOL_H_ | 5 #ifndef BIN_THREAD_POOL_H_ |
6 #define BIN_THREAD_POOL_H_ | 6 #define BIN_THREAD_POOL_H_ |
7 | 7 |
8 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
9 #include "platform/globals.h" | 9 #include "platform/globals.h" |
10 #include "platform/thread.h" | 10 #include "platform/thread.h" |
11 | 11 |
12 typedef void* Task; | 12 class ThreadPool { |
13 public: | |
14 typedef void* Task; | |
15 typedef void (*TaskHandler)(Task args); | |
16 | |
17 ThreadPool(TaskHandler task_handler, int initial_size = 4) | |
18 : initial_size_(initial_size), | |
19 terminate_(false), | |
20 drain_(false), | |
21 size_(0), | |
22 head_(NULL), | |
23 tail_(NULL), | |
24 task_handler_(task_handler) {} | |
25 | |
26 // 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.
| |
27 void Start(); | |
28 // 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.
| |
29 // pending in the queue will be processed. When this function | |
30 // returns all threads are terminated. | |
31 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.
| |
32 | |
33 // Insert a new task into the thread pool. Returns true on success. | |
34 bool InsertTask(Task task); | |
35 | |
36 private: | |
37 class TaskQueueEntry { | |
38 public: | |
39 explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {} | |
40 | |
41 Task task() { return task_; } | |
42 TaskQueueEntry* next() { return next_; } | |
43 void set_next(TaskQueueEntry* value) { next_ = value; } | |
44 | |
45 private: | |
46 Task task_; | |
47 TaskQueueEntry* next_; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(TaskQueueEntry); | |
50 }; | |
13 | 51 |
14 | 52 |
15 class TaskQueueEntry { | 53 TaskQueueEntry* WaitForTask(); |
16 public: | |
17 explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {} | |
18 | |
19 Task task() { return task_; } | |
20 | |
21 TaskQueueEntry* next() { return next_; } | |
22 void set_next(TaskQueueEntry* value) { next_ = value; } | |
23 | |
24 private: | |
25 Task task_; | |
26 TaskQueueEntry* next_; | |
27 }; | |
28 | |
29 | |
30 // The task queue is a single linked list. Link direction is from tail | |
31 // to head. New entried are inserted at the tail and entries are | |
32 // removed from the head. | |
33 class TaskQueue { | |
34 public: | |
35 TaskQueue() : terminate_(false), head_(NULL), tail_(NULL) {} | |
36 | |
37 void Insert(TaskQueueEntry* task); | |
38 TaskQueueEntry* Remove(); | |
39 void Shutdown(); | |
40 | |
41 private: | |
42 bool terminate_; | |
43 TaskQueueEntry* head_; | |
44 TaskQueueEntry* tail_; | |
45 dart::Monitor monitor_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(TaskQueue); | |
48 }; | |
49 | |
50 | |
51 class ThreadPool { | |
52 public: | |
53 typedef void* (*TaskHandler)(void* args); | |
54 | |
55 ThreadPool(TaskHandler task_handler, int initial_size = 4) | |
56 : terminate_(false), | |
57 size_(initial_size), | |
58 task_handler_(task_handler) {} | |
59 | |
60 void Start(); | |
61 void Shutdown(); | |
62 | |
63 void InsertTask(Task task); | |
64 | |
65 void ThreadTerminated(); | 54 void ThreadTerminated(); |
66 | 55 |
67 private: | |
68 Task WaitForTask(); | |
69 | |
70 static void Main(uword args); | 56 static void Main(uword args); |
71 | 57 |
72 TaskQueue queue_; | 58 dart::Monitor monitor_; // Monitor protecting all shared state. |
73 // TODO(sgjesse): Move the monitor in TaskQueue to ThreadPool and | 59 |
74 // obtain it for updating terminate_. | 60 int initial_size_; // Initial number of threads to start. |
75 dart::Monitor monitor_; | 61 bool terminate_; // Set to true when the thread pool is terminating. |
76 bool terminate_; | 62 bool drain_; // Process all queue entries before termination. |
77 int size_; // Number of threads. | 63 int size_; // Current number of threads. |
64 | |
65 // The task queue is a single linked list. Link direction is from tail | |
66 // 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.
| |
67 // removed from the head. | |
68 TaskQueueEntry* head_; | |
69 TaskQueueEntry* tail_; | |
78 TaskHandler task_handler_; | 70 TaskHandler task_handler_; |
79 | 71 |
80 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | 72 DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
81 }; | 73 }; |
82 | 74 |
83 #endif // BIN_THREAD_POOL_H_ | 75 #endif // BIN_THREAD_POOL_H_ |
OLD | NEW |