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