Commit | Line | Data |
---|---|---|
c7912668 ILT |
1 | // workqueue-internal.h -- internal work queue header for gold -*- C++ -*- |
2 | ||
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. | |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
23 | #ifndef GOLD_WORKQUEUE_INTERNAL_H | |
24 | #define GOLD_WORKQUEUE_INTERNAL_H | |
25 | ||
26 | #include <queue> | |
27 | ||
28 | #include "gold-threads.h" | |
29 | #include "workqueue.h" | |
30 | ||
31 | // This is an internal header file for different gold workqueue | |
32 | // implementations. | |
33 | ||
34 | namespace gold | |
35 | { | |
36 | ||
37 | class Workqueue_thread; | |
38 | ||
39 | // The Workqueue_runner abstract class. This is the interface used by | |
40 | // the general workqueue code to actually run a task. | |
41 | ||
42 | class Workqueue_runner | |
43 | { | |
44 | public: | |
45 | Workqueue_runner(Workqueue* workqueue) | |
46 | : workqueue_(workqueue) | |
47 | { } | |
48 | virtual ~Workqueue_runner() | |
49 | { } | |
50 | ||
51 | // Run a task. This is always called in the main thread. | |
52 | virtual void | |
53 | run(Task*, Task_locker*) = 0; | |
54 | ||
55 | // Set the number of threads to use. This is ignored when not using | |
56 | // threads. | |
57 | virtual void | |
58 | set_thread_count(int) = 0; | |
59 | ||
60 | protected: | |
61 | // This is called by an implementation when a task is completed. | |
62 | void completed(Task* t, Task_locker* tl) | |
63 | { this->workqueue_->completed(t, tl); } | |
64 | ||
65 | Workqueue* get_workqueue() const | |
66 | { return this->workqueue_; } | |
67 | ||
68 | private: | |
69 | Workqueue* workqueue_; | |
70 | }; | |
71 | ||
72 | // The threaded instantiation of Workqueue_runner. | |
73 | ||
74 | class Workqueue_runner_threadpool : public Workqueue_runner | |
75 | { | |
76 | public: | |
77 | Workqueue_runner_threadpool(Workqueue* workqueue); | |
78 | ||
79 | ~Workqueue_runner_threadpool(); | |
80 | ||
81 | void | |
82 | run(Task*, Task_locker*); | |
83 | ||
84 | void | |
85 | set_thread_count(int); | |
86 | ||
87 | private: | |
88 | // This class can not be copied. | |
89 | Workqueue_runner_threadpool(const Workqueue_runner_threadpool&); | |
90 | Workqueue_runner_threadpool& operator=(const Workqueue_runner_threadpool&); | |
91 | ||
92 | // Return the next Task and Task_locker to run. This returns false | |
93 | // if the calling thread should simply exit. | |
94 | bool | |
95 | get_next(Task**, Task_locker**); | |
96 | ||
97 | // This is called when the thread completes a task. | |
98 | void | |
99 | thread_completed(Task*, Task_locker*); | |
100 | ||
101 | // The Workqueue_thread class calls functions from this and from the | |
102 | // parent Workqueue_runner. | |
103 | friend class Workqueue_thread; | |
104 | ||
105 | // An entry on the queue of tasks to run. | |
106 | typedef std::pair<Task*, Task_locker*> Task_queue_entry; | |
107 | ||
108 | // A queue of tasks to run. | |
109 | typedef std::queue<Task_queue_entry> Task_queue; | |
110 | ||
111 | // The number of threads we want to create. This is only changed in | |
112 | // the main thread or when only one thread is running. This is set | |
113 | // to zero when all threads should exit. | |
114 | int desired_thread_count_; | |
115 | // A lock controlling access to the remaining fields. | |
116 | Lock lock_; | |
117 | // The number of threads we have created. | |
118 | int actual_thread_count_; | |
119 | // The number of threads which are running a task. | |
120 | int running_thread_count_; | |
121 | // A queue of tasks to run. | |
122 | Task_queue task_queue_; | |
123 | // A condition variable which signals when the task_queue_ changed. | |
124 | Condvar task_queue_condvar_; | |
125 | }; | |
126 | ||
127 | } // End namespace gold. | |
128 | ||
129 | #endif // !defined(GOLD_WORKQUEUE_INTERNAL_H) |