1 // workqueue.cc -- the workqueue for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
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.
27 #include "workqueue.h"
28 #include "workqueue-internal.h"
35 // Add T to the end of the list.
38 Task_list::push_back(Task* t)
40 gold_assert(t->list_next() == NULL);
41 if (this->head_ == NULL)
48 this->tail_->set_list_next(t);
53 // Add T to the front of the list.
56 Task_list::push_front(Task* t)
58 gold_assert(t->list_next() == NULL);
59 if (this->head_ == NULL)
66 t->set_list_next(this->head_);
71 // Remove and return the first Task waiting for this lock to be
75 Task_list::pop_front()
77 Task* ret = this->head_;
80 if (ret == this->tail_)
82 gold_assert(ret->list_next() == NULL);
88 this->head_ = ret->list_next();
89 gold_assert(this->head_ != NULL);
90 ret->clear_list_next();
96 // The simple single-threaded implementation of Workqueue_threader.
98 class Workqueue_threader_single : public Workqueue_threader
101 Workqueue_threader_single(Workqueue* workqueue)
102 : Workqueue_threader(workqueue)
104 ~Workqueue_threader_single()
108 set_thread_count(int thread_count)
109 { gold_assert(thread_count > 0); }
112 should_cancel_thread()
116 // Workqueue methods.
118 Workqueue::Workqueue(const General_options& options)
124 condvar_(this->lock_),
127 bool threads = options.threads();
128 #ifndef ENABLE_THREADS
132 this->threader_ = new Workqueue_threader_single(this);
135 #ifdef ENABLE_THREADS
136 this->threader_ = new Workqueue_threader_threadpool(this);
143 Workqueue::~Workqueue()
147 // Add a task to the end of a specific queue, or put it on the list
148 // waiting for a Token.
151 Workqueue::add_to_queue(Task_list* queue, Task* t, bool front)
153 Hold_lock hl(this->lock_);
155 Task_token* token = t->is_runnable();
159 token->add_waiting_front(t);
161 token->add_waiting(t);
167 queue->push_front(t);
170 // Tell any waiting thread that there is work to do.
171 this->condvar_.signal();
175 // Add a task to the queue.
178 Workqueue::queue(Task* t)
180 this->add_to_queue(&this->tasks_, t, false);
183 // Queue a task which should run soon.
186 Workqueue::queue_soon(Task* t)
188 t->set_should_run_soon();
189 this->add_to_queue(&this->first_tasks_, t, false);
192 // Queue a task which should run next.
195 Workqueue::queue_next(Task* t)
197 t->set_should_run_soon();
198 this->add_to_queue(&this->first_tasks_, t, true);
201 // Return whether to cancel the current thread.
204 Workqueue::should_cancel_thread()
206 return this->threader_->should_cancel_thread();
209 // Find a runnable task in TASKS. Return NULL if none could be found.
210 // If we find a Task waiting for a Token, add it to the list for that
211 // Token. The workqueue lock must be held when this is called.
214 Workqueue::find_runnable_in_list(Task_list* tasks)
217 while ((t = tasks->pop_front()) != NULL)
219 Task_token* token = t->is_runnable();
224 token->add_waiting(t);
228 // We couldn't find any runnable task.
232 // Find a runnable task. Return NULL if none could be found. The
233 // workqueue lock must be held when this is called.
236 Workqueue::find_runnable()
238 Task* t = this->find_runnable_in_list(&this->first_tasks_);
240 t = this->find_runnable_in_list(&this->tasks_);
244 // Find a runnable a task, and wait until we find one. Return NULL if
245 // we should exit. The workqueue lock must be held when this is
249 Workqueue::find_runnable_or_wait(int thread_number)
251 Task* t = this->find_runnable();
255 if (this->running_ == 0
256 && this->first_tasks_.empty()
257 && this->tasks_.empty())
259 // Kick all the threads to make them exit.
260 this->condvar_.broadcast();
262 gold_assert(this->waiting_ == 0);
266 if (this->should_cancel_thread())
269 gold_debug(DEBUG_TASK, "%3d sleeping", thread_number);
271 this->condvar_.wait();
273 gold_debug(DEBUG_TASK, "%3d awake", thread_number);
275 t = this->find_runnable();
281 // Find and run tasks. If we can't find a runnable task, wait for one
282 // to become available. If we run a task, and it frees up another
283 // runnable task, then run that one too. This returns true if we
284 // should look for another task, false if we are cancelling this
288 Workqueue::find_and_run_task(int thread_number)
294 Hold_lock hl(this->lock_);
296 // Find a runnable task.
297 t = this->find_runnable_or_wait(thread_number);
302 // Get the locks for the task. This must be called while we are
303 // still holding the Workqueue lock.
311 gold_debug(DEBUG_TASK, "%3d running task %s", thread_number,
316 gold_debug(DEBUG_TASK, "%3d completed task %s", thread_number,
321 Hold_lock hl(this->lock_);
325 // Release the locks for the task. This must be done with the
326 // workqueue lock held. Get the next Task to run if any.
327 next = this->release_locks(t, &tl);
330 next = this->find_runnable();
332 // If we have another Task to run, get the Locks. This must
333 // be called while we are still holding the Workqueue lock.
343 // We are done with this task.
352 // Handle the return value of release_locks, and get tasks ready to
355 // 1) If T is not runnable, queue it on the appropriate token.
357 // 2) Otherwise, T is runnable. If *PRET is not NULL, then we have
358 // already decided which Task to run next. Add T to the list of
359 // runnable tasks, and signal another thread.
361 // 3) Otherwise, *PRET is NULL. If IS_BLOCKER is false, then T was
362 // waiting on a write lock. We can grab that lock now, so we run T
365 // 4) Otherwise, IS_BLOCKER is true. If we should run T soon, then
368 // 5) Otherwise, check whether there are other tasks to run. If there
369 // are, then we generally get a better ordering if we run those tasks
370 // now, before T. A typical example is tasks waiting on the Dirsearch
371 // blocker. We don't want to run those tasks right away just because
372 // the Dirsearch was unblocked.
374 // 6) Otherwise, there are no other tasks to run, so we might as well
377 // This function must be called with the Workqueue lock held.
379 // Return true if we set *PRET to T, false otherwise.
382 Workqueue::return_or_queue(Task* t, bool is_blocker, Task** pret)
384 Task_token* token = t->is_runnable();
388 token->add_waiting(t);
393 bool should_queue = false;
394 bool should_return = false;
398 else if (!is_blocker)
399 should_return = true;
400 else if (t->should_run_soon())
401 should_return = true;
402 else if (!this->first_tasks_.empty() || !this->tasks_.empty())
405 should_return = true;
409 gold_assert(*pret == NULL);
413 else if (should_queue)
415 if (t->should_run_soon())
416 this->first_tasks_.push_back(t);
418 this->tasks_.push_back(t);
419 this->condvar_.signal();
426 // Release the locks associated with a Task. Return the first
427 // runnable Task that we find. If we find more runnable tasks, add
428 // them to the run queue and signal any other threads. This must be
429 // called with the Workqueue lock held.
432 Workqueue::release_locks(Task* t, Task_locker* tl)
435 for (Task_locker::iterator p = tl->begin(); p != tl->end(); ++p)
437 Task_token* token = *p;
438 if (token->is_blocker())
440 if (token->remove_blocker())
442 // The token has been unblocked. Every waiting Task may
445 while ((t = token->remove_first_waiting()) != NULL)
448 this->return_or_queue(t, true, &ret);
454 token->remove_writer(t);
456 // One more waiting Task may now be runnable. If we are
457 // going to run it next, we can stop. Otherwise we need to
458 // move all the Tasks to the runnable queue, to avoid a
459 // potential deadlock if the locking status changes before
460 // we run the next thread.
462 while ((t = token->remove_first_waiting()) != NULL)
465 if (this->return_or_queue(t, false, &ret))
473 // Process all the tasks on the workqueue. Keep going until the
474 // workqueue is empty, or until we have been told to exit. This
475 // function is called by all threads.
478 Workqueue::process(int thread_number)
480 while (this->find_and_run_task(thread_number))
484 // Set the number of threads to use for the workqueue, if we are using
488 Workqueue::set_thread_count(int threads)
490 Hold_lock hl(this->lock_);
492 this->threader_->set_thread_count(threads);
493 // Wake up all the threads, since something has changed.
494 this->condvar_.broadcast();
497 // Add a new blocker to an existing Task_token.
500 Workqueue::add_blocker(Task_token* token)
502 Hold_lock hl(this->lock_);
503 token->add_blocker();
506 } // End namespace gold.