gas/
[binutils.git] / gold / workqueue.cc
blob2c7e88032073a656cf0259b866001019faa8069b
1 // workqueue.cc -- the workqueue for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include "workqueue.h"
27 namespace gold
30 // Task_token methods.
32 Task_token::Task_token()
33 : is_blocker_(false), readers_(0), writer_(NULL)
37 Task_token::~Task_token()
39 gold_assert(this->readers_ == 0 && this->writer_ == NULL);
42 bool
43 Task_token::is_readable() const
45 gold_assert(!this->is_blocker_);
46 return this->writer_ == NULL;
49 void
50 Task_token::add_reader()
52 gold_assert(!this->is_blocker_);
53 gold_assert(this->is_readable());
54 ++this->readers_;
57 void
58 Task_token::remove_reader()
60 gold_assert(!this->is_blocker_);
61 gold_assert(this->readers_ > 0);
62 --this->readers_;
65 bool
66 Task_token::is_writable() const
68 gold_assert(!this->is_blocker_);
69 return this->writer_ == NULL && this->readers_ == 0;
72 void
73 Task_token::add_writer(const Task* t)
75 gold_assert(!this->is_blocker_);
76 gold_assert(this->is_writable());
77 this->writer_ = t;
80 void
81 Task_token::remove_writer(const Task* t)
83 gold_assert(!this->is_blocker_);
84 gold_assert(this->writer_ == t);
85 this->writer_ = NULL;
88 bool
89 Task_token::has_write_lock(const Task* t)
91 gold_assert(!this->is_blocker_);
92 return this->writer_ == t;
95 // For blockers, we just use the readers_ field.
97 void
98 Task_token::add_blocker()
100 if (this->readers_ == 0 && this->writer_ == NULL)
101 this->is_blocker_ = true;
102 else
103 gold_assert(this->is_blocker_);
104 ++this->readers_;
107 bool
108 Task_token::remove_blocker()
110 gold_assert(this->is_blocker_ && this->readers_ > 0);
111 --this->readers_;
112 return this->readers_ == 0;
115 bool
116 Task_token::is_blocked() const
118 gold_assert(this->is_blocker_
119 || (this->readers_ == 0 && this->writer_ == NULL));
120 return this->readers_ > 0;
123 // The Task_block_token class.
125 Task_block_token::Task_block_token(Task_token& token, Workqueue* workqueue)
126 : token_(token), workqueue_(workqueue)
128 // We must increment the block count when the task is created and
129 // put on the queue. This object is created when the task is run,
130 // so we don't increment the block count here.
131 gold_assert(this->token_.is_blocked());
134 Task_block_token::~Task_block_token()
136 if (this->token_.remove_blocker())
138 // Tell the workqueue that a blocker was cleared. This is
139 // always called in the main thread, so no locking is required.
140 this->workqueue_->cleared_blocker();
144 // The Workqueue_runner abstract class.
146 class Workqueue_runner
148 public:
149 Workqueue_runner(Workqueue* workqueue)
150 : workqueue_(workqueue)
152 virtual ~Workqueue_runner()
155 // Run a task. This is always called in the main thread.
156 virtual void run(Task*, Task_locker*) = 0;
158 protected:
159 // This is called by an implementation when a task is completed.
160 void completed(Task* t, Task_locker* tl)
161 { this->workqueue_->completed(t, tl); }
163 Workqueue* get_workqueue() const
164 { return this->workqueue_; }
166 private:
167 Workqueue* workqueue_;
170 // The simple single-threaded implementation of Workqueue_runner.
172 class Workqueue_runner_single : public Workqueue_runner
174 public:
175 Workqueue_runner_single(Workqueue* workqueue)
176 : Workqueue_runner(workqueue)
178 ~Workqueue_runner_single()
181 void run(Task*, Task_locker*);
184 void
185 Workqueue_runner_single::run(Task* t, Task_locker* tl)
187 t->run(this->get_workqueue());
188 this->completed(t, tl);
191 // Workqueue methods.
193 Workqueue::Workqueue(const General_options&)
194 : tasks_lock_(),
195 tasks_(),
196 completed_lock_(),
197 completed_(),
198 running_(0),
199 completed_condvar_(this->completed_lock_),
200 cleared_blockers_(0)
202 // At some point we will select the specific implementation of
203 // Workqueue_runner to use based on the command line options.
204 this->runner_ = new Workqueue_runner_single(this);
207 Workqueue::~Workqueue()
209 gold_assert(this->tasks_.empty());
210 gold_assert(this->completed_.empty());
211 gold_assert(this->running_ == 0);
214 // Add a task to the queue.
216 void
217 Workqueue::queue(Task* t)
219 Hold_lock hl(this->tasks_lock_);
220 this->tasks_.push_back(t);
223 // Add a task to the front of the queue.
225 void
226 Workqueue::queue_front(Task* t)
228 Hold_lock hl(this->tasks_lock_);
229 this->tasks_.push_front(t);
232 // Clear the list of completed tasks. Return whether we cleared
233 // anything. The completed_lock_ must be held when this is called.
235 bool
236 Workqueue::clear_completed()
238 if (this->completed_.empty())
239 return false;
242 delete this->completed_.front();
243 this->completed_.pop_front();
245 while (!this->completed_.empty());
246 return true;
249 // Find a runnable task in TASKS, which is non-empty. Return NULL if
250 // none could be found. The tasks_lock_ must be held when this is
251 // called. Sets ALL_BLOCKED if all non-runnable tasks are waiting on
252 // a blocker.
254 Task*
255 Workqueue::find_runnable(Task_list& tasks, bool* all_blocked)
257 Task* tlast = tasks.back();
258 *all_blocked = true;
259 while (true)
261 Task* t = tasks.front();
262 tasks.pop_front();
264 Task::Is_runnable_type is_runnable = t->is_runnable(this);
265 if (is_runnable == Task::IS_RUNNABLE)
266 return t;
268 if (is_runnable != Task::IS_BLOCKED)
269 *all_blocked = false;
271 tasks.push_back(t);
273 if (t == tlast)
275 // We couldn't find any runnable task. If there are any
276 // completed tasks, free their locks and try again.
279 Hold_lock hl2(this->completed_lock_);
281 if (!this->clear_completed())
283 // There had better be some tasks running, or we will
284 // never find a runnable task.
285 gold_assert(this->running_ > 0);
287 // We couldn't find any runnable tasks, and we
288 // couldn't release any locks.
289 return NULL;
293 // We're going around again, so recompute ALL_BLOCKED.
294 *all_blocked = true;
299 // Process all the tasks on the workqueue. This is the main loop in
300 // the linker. Note that as we process tasks, new tasks will be
301 // added.
303 void
304 Workqueue::process()
306 while (true)
308 Task* t;
309 bool empty;
310 bool all_blocked;
313 Hold_lock hl(this->tasks_lock_);
315 if (this->tasks_.empty())
317 t = NULL;
318 empty = true;
319 all_blocked = false;
321 else
323 t = this->find_runnable(this->tasks_, &all_blocked);
324 empty = false;
328 // If T != NULL, it is a task we can run.
329 // If T == NULL && empty, then there are no tasks waiting to
330 // be run at this level.
331 // If T == NULL && !empty, then there tasks waiting to be
332 // run at this level, but they are waiting for something to
333 // unlock.
335 if (t != NULL)
336 this->run(t);
337 else if (!empty)
340 Hold_lock hl(this->completed_lock_);
342 // There must be something for us to wait for, or we won't
343 // be able to make progress.
344 gold_assert(this->running_ > 0 || !this->completed_.empty());
346 if (all_blocked)
348 this->cleared_blockers_ = 0;
349 this->clear_completed();
350 while (this->cleared_blockers_ == 0)
352 gold_assert(this->running_ > 0);
353 this->completed_condvar_.wait();
354 this->clear_completed();
357 else
359 if (this->running_ > 0)
361 // Wait for a task to finish.
362 this->completed_condvar_.wait();
364 this->clear_completed();
368 else
371 Hold_lock hl(this->completed_lock_);
373 // If there are no running tasks, then we are done.
374 if (this->running_ == 0)
376 this->clear_completed();
377 return;
380 // Wait for a task to finish. Then we have to loop around
381 // again in case it added any new tasks before finishing.
382 this->completed_condvar_.wait();
383 this->clear_completed();
389 // Run a task. This is always called in the main thread.
391 void
392 Workqueue::run(Task* t)
394 ++this->running_;
395 this->runner_->run(t, t->locks(this));
398 // This is called when a task is completed to put the locks on the
399 // list to be released. We use a list because we only want the locks
400 // to be released in the main thread.
402 void
403 Workqueue::completed(Task* t, Task_locker* tl)
406 Hold_lock hl(this->completed_lock_);
407 gold_assert(this->running_ > 0);
408 --this->running_;
409 this->completed_.push_back(tl);
410 this->completed_condvar_.signal();
412 delete t;
415 // This is called when the last task for a blocker has completed.
416 // This is always called in the main thread.
418 void
419 Workqueue::cleared_blocker()
421 ++this->cleared_blockers_;
424 } // End namespace gold.