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 enum DrainFlag { | |
18 kDrain, | |
19 kDoNotDrain | |
20 }; | |
21 | |
22 ThreadPool(TaskHandler task_handler, int initial_size = 4) | |
23 : initial_size_(initial_size), | |
24 terminate_(false), | |
25 drain_flag_(kDoNotDrain), | |
26 size_(0), | |
27 head_(NULL), | |
28 tail_(NULL), | |
29 task_handler_(task_handler) {} | |
30 | |
31 // Start the thread pool. | |
32 void Start(); | |
33 | |
34 // Shutdown the thread pool. If drain flags specifies whether all | |
Mads Ager (google)
2012/01/24 13:44:27
If drain flags -> The drain flag
Søren Gjesse
2012/01/24 14:38:50
Done.
| |
35 // tasks pending in the queue will be processed. When this function | |
36 // returns all threads are terminated. | |
37 void Shutdown(DrainFlag drain_flag = kDoNotDrain); | |
38 | |
39 // Insert a new task into the thread pool. Returns true on success. | |
40 bool InsertTask(Task task); | |
41 | |
42 private: | |
43 class TaskQueueEntry { | |
44 public: | |
45 explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {} | |
46 | |
47 Task task() { return task_; } | |
48 TaskQueueEntry* next() { return next_; } | |
49 void set_next(TaskQueueEntry* value) { next_ = value; } | |
50 | |
51 private: | |
52 Task task_; | |
53 TaskQueueEntry* next_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(TaskQueueEntry); | |
56 }; | |
13 | 57 |
14 | 58 |
15 class TaskQueueEntry { | 59 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(); | 60 void ThreadTerminated(); |
66 | 61 |
67 private: | |
68 Task WaitForTask(); | |
69 | |
70 static void Main(uword args); | 62 static void Main(uword args); |
71 | 63 |
72 TaskQueue queue_; | 64 dart::Monitor monitor_; // Monitor protecting all shared state. |
73 // TODO(sgjesse): Move the monitor in TaskQueue to ThreadPool and | 65 |
74 // obtain it for updating terminate_. | 66 int initial_size_; // Initial number of threads to start. |
75 dart::Monitor monitor_; | 67 bool terminate_; // Set to true when the thread pool is terminating. |
76 bool terminate_; | 68 DrainFlag drain_flag_; // Queue handling before termination. |
77 int size_; // Number of threads. | 69 int size_; // Current number of threads. |
Mads Ager (google)
2012/01/24 13:44:27
size_ -> number_of_threads_?
Søren Gjesse
2012/01/24 14:38:50
Done.
| |
70 | |
71 // The task queue is a single linked list. Link direction is from tail | |
72 // to head. New entries are inserted at the tail and entries are | |
73 // removed from the head. | |
74 TaskQueueEntry* head_; | |
75 TaskQueueEntry* tail_; | |
78 TaskHandler task_handler_; | 76 TaskHandler task_handler_; |
79 | 77 |
80 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | 78 DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
81 }; | 79 }; |
82 | 80 |
83 #endif // BIN_THREAD_POOL_H_ | 81 #endif // BIN_THREAD_POOL_H_ |
OLD | NEW |