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 "bin/globals.h" | 9 #include "bin/globals.h" |
10 | 10 |
11 // Declare the OS-specific types ahead of defining the generic classes. | 11 // Declare the OS-specific types ahead of defining the generic classes. |
12 #if defined(TARGET_OS_LINUX) | 12 #if defined(TARGET_OS_LINUX) |
13 #include "bin/thread_pool_linux.h" | 13 #include "bin/thread_pool_linux.h" |
14 #elif defined(TARGET_OS_MACOS) | 14 #elif defined(TARGET_OS_MACOS) |
15 #include "bin/thread_pool_macos.h" | 15 #include "bin/thread_pool_macos.h" |
16 #elif defined(TARGET_OS_WINDOWS) | 16 #elif defined(TARGET_OS_WINDOWS) |
17 #include "bin/thread_pool_win.h" | 17 #include "bin/thread_pool_win.h" |
18 #else | 18 #else |
19 #error Unknown target os. | 19 #error Unknown target os. |
20 #endif | 20 #endif |
21 | 21 |
22 | 22 |
23 typedef int Task; | 23 typedef void* Task; |
24 | 24 |
25 | 25 |
26 class TaskQueueEntry { | 26 class TaskQueueEntry { |
27 public: | 27 public: |
28 explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {} | 28 explicit TaskQueueEntry(Task task) : task_(task), next_(NULL) {} |
29 | 29 |
30 Task task() { return task_; } | 30 Task task() { return task_; } |
31 | 31 |
32 TaskQueueEntry* next() { return next_; } | 32 TaskQueueEntry* next() { return next_; } |
33 void set_next(TaskQueueEntry* value) { next_ = value; } | 33 void set_next(TaskQueueEntry* value) { next_ = value; } |
(...skipping 18 matching lines...) Expand all Loading... | |
52 TaskQueueEntry* head_; | 52 TaskQueueEntry* head_; |
53 TaskQueueEntry* tail_; | 53 TaskQueueEntry* tail_; |
54 TaskQueueData data_; | 54 TaskQueueData data_; |
55 | 55 |
56 DISALLOW_COPY_AND_ASSIGN(TaskQueue); | 56 DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
57 }; | 57 }; |
58 | 58 |
59 | 59 |
60 class ThreadPool { | 60 class ThreadPool { |
61 public: | 61 public: |
62 explicit ThreadPool(int initial_size = 4) : size_(initial_size) {} | 62 ThreadPool(void* (*task_handler)(void* args), int initial_size = 4) |
63 : size_(initial_size), task_handler_(task_handler) {} | |
63 | 64 |
64 void Start(); | 65 void Start(); |
65 void Shutdown(); | 66 void Shutdown(); |
66 | 67 |
67 void InsertTask(Task task); | 68 void InsertTask(Task task); |
68 | 69 |
69 private: | 70 private: |
70 Task WaitForTask(); | 71 Task WaitForTask(); |
71 | 72 |
72 static void* Main(void* args); | 73 static void* Main(void* args); |
73 | 74 |
74 TaskQueue queue; | 75 TaskQueue queue; |
75 int size_; // Number of threads. | 76 int size_; // Number of threads. |
77 void* (*task_handler_)(void* args); | |
Mads Ager (google)
2012/01/06 10:15:18
Let's typedef this type and the use a named type i
Søren Gjesse
2012/01/06 10:41:15
Sure - looks nicer.
| |
76 ThreadPoolData data_; | 78 ThreadPoolData data_; |
77 | 79 |
78 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | 80 DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
79 }; | 81 }; |
80 | 82 |
81 #endif // BIN_THREAD_POOL_H_ | 83 #endif // BIN_THREAD_POOL_H_ |
OLD | NEW |