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 | 9 |
10 #include "platform/globals.h" | 10 #include "platform/globals.h" |
11 | 11 |
12 // Declare the OS-specific types ahead of defining the generic classes. | 12 // Declare the OS-specific types ahead of defining the generic classes. |
13 #if defined(TARGET_OS_LINUX) | 13 #if defined(TARGET_OS_LINUX) |
14 #include "bin/thread_pool_linux.h" | 14 #include "bin/thread_pool_linux.h" |
15 #elif defined(TARGET_OS_MACOS) | 15 #elif defined(TARGET_OS_MACOS) |
16 #include "bin/thread_pool_macos.h" | 16 #include "bin/thread_pool_macos.h" |
17 #elif defined(TARGET_OS_WINDOWS) | 17 #elif defined(TARGET_OS_WINDOWS) |
18 #include "bin/thread_pool_win.h" | 18 #include "bin/thread_pool_win.h" |
19 #else | 19 #else |
20 #error Unknown target os. | 20 #error Unknown target os. |
21 #endif | 21 #endif |
22 | 22 |
23 | 23 |
24 typedef int Task; | 24 typedef void* Task; |
25 | 25 |
26 | 26 |
27 class TaskQueueEntry { | 27 class TaskQueueEntry { |
28 public: | 28 public: |
29 explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {} | 29 explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {} |
30 | 30 |
31 Task task() { return task_; } | 31 Task task() { return task_; } |
32 | 32 |
33 TaskQueueEntry* next() { return next_; } | 33 TaskQueueEntry* next() { return next_; } |
34 void set_next(TaskQueueEntry* value) { next_ = value; } | 34 void set_next(TaskQueueEntry* value) { next_ = value; } |
35 | 35 |
36 private: | 36 private: |
37 Task task_; | 37 Task task_; |
38 TaskQueueEntry* next_; | 38 TaskQueueEntry* next_; |
39 }; | 39 }; |
40 | 40 |
41 | 41 |
42 // The task queue is a single linked list. Link direction is from tail | 42 // The task queue is a single linked list. Link direction is from tail |
43 // to head. New entried are inserted at the tail and entries are | 43 // to head. New entried are inserted at the tail and entries are |
44 // removed from the head. | 44 // removed from the head. |
45 class TaskQueue { | 45 class TaskQueue { |
46 public: | 46 public: |
47 TaskQueue(); | 47 TaskQueue(); |
48 | 48 |
49 void Insert(TaskQueueEntry* task); | 49 void Insert(TaskQueueEntry* task); |
50 TaskQueueEntry* Remove(); | 50 TaskQueueEntry* Remove(); |
| 51 void Shutdown(); |
51 | 52 |
52 private: | 53 private: |
| 54 bool terminate_; |
53 TaskQueueEntry* head_; | 55 TaskQueueEntry* head_; |
54 TaskQueueEntry* tail_; | 56 TaskQueueEntry* tail_; |
55 TaskQueueData data_; | 57 TaskQueueData data_; |
56 | 58 |
57 DISALLOW_COPY_AND_ASSIGN(TaskQueue); | 59 DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
58 }; | 60 }; |
59 | 61 |
60 | 62 |
61 class ThreadPool { | 63 class ThreadPool { |
62 public: | 64 public: |
63 explicit ThreadPool(int initial_size = 4) : size_(initial_size) {} | 65 typedef void* (*TaskHandler)(void* args); |
| 66 |
| 67 ThreadPool(TaskHandler task_handler, int initial_size = 4) |
| 68 : terminate_(false), |
| 69 size_(initial_size), |
| 70 task_handler_(task_handler) {} |
64 | 71 |
65 void Start(); | 72 void Start(); |
66 void Shutdown(); | 73 void Shutdown(); |
67 | 74 |
68 void InsertTask(Task task); | 75 void InsertTask(Task task); |
69 | 76 |
70 private: | 77 private: |
71 Task WaitForTask(); | 78 Task WaitForTask(); |
72 | 79 |
73 static void* Main(void* args); | 80 static void* Main(void* args); |
74 | 81 |
75 TaskQueue queue; | 82 TaskQueue queue_; |
| 83 // TODO(sgjesse): Move the monitor in TaskQueue to ThreadPool and |
| 84 // obtain it for updating terminate_. |
| 85 bool terminate_; |
76 int size_; // Number of threads. | 86 int size_; // Number of threads. |
| 87 TaskHandler task_handler_; |
77 ThreadPoolData data_; | 88 ThreadPoolData data_; |
78 | 89 |
79 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | 90 DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
80 }; | 91 }; |
81 | 92 |
82 #endif // BIN_THREAD_POOL_H_ | 93 #endif // BIN_THREAD_POOL_H_ |
OLD | NEW |