1 // workqueue.cc -- the workqueue for gold
3 // Copyright 2006, 2007, 2008 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.
28 #include "workqueue.h"
29 #include "workqueue-internal.h"
36 // Add T to the end of the list.
39 Task_list::push_back(Task* t)
41 gold_assert(t->list_next() == NULL);
42 if (this->head_ == NULL)
49 this->tail_->set_list_next(t);
54 // Add T to the front of the list.
57 Task_list::push_front(Task* t)
59 gold_assert(t->list_next() == NULL);
60 if (this->head_ == NULL)
67 t->set_list_next(this->head_);
72 // Remove and return the first Task waiting for this lock to be
76 Task_list::pop_front()
78 Task* ret = this->head_;
81 if (ret == this->tail_)
83 gold_assert(ret->list_next() == NULL);
89 this->head_ = ret->list_next();
90 gold_assert(this->head_ != NULL);
91 ret->clear_list_next();
97 // The simple single-threaded implementation of Workqueue_threader.
99 class Workqueue_threader_single : public Workqueue_threader
102 Workqueue_threader_single(Workqueue* workqueue)
103 : Workqueue_threader(workqueue)
105 ~Workqueue_threader_single()
109 set_thread_count(int thread_count)
110 { gold_assert(thread_count > 0); }
113 should_cancel_thread(int)
117 // Workqueue methods.
119 Workqueue::Workqueue(const General_options& options)
125 condvar_(this->lock_),
128 bool threads = options.threads();
129 #ifndef ENABLE_THREADS
133 this->threader_ = new Workqueue_threader_single(this);
136 #ifdef ENABLE_THREADS
137 this->threader_ = new Workqueue_threader_threadpool(this);
144 Workqueue::~Workqueue()
148 // Add a task to the end of a specific queue, or put it on the list
149 // waiting for a Token.
152 Workqueue::add_to_queue(Task_list* queue, Task* t, bool front)
154 Hold_lock hl(this->lock_);
156 Task_token* token = t->is_runnable();
160 token->add_waiting_front(t);
162 token->add_waiting(t);
168 queue->push_front(t);
171 // Tell any waiting thread that there is work to do.
172 this->condvar_.signal();
176 // Add a task to the queue.
179 Workqueue::queue(Task* t)
181 this->add_to_queue(&this->tasks_, t, false);
184 // Queue a task which should run soon.
187 Workqueue::queue_soon(Task* t)
189 t->set_should_run_soon();
190 this->add_to_queue(&this->first_tasks_, t, false);
193 // Queue a task which should run next.
196 Workqueue::queue_next(Task* t)
198 t->set_should_run_soon();
199 this->add_to_queue(&this->first_tasks_, t, true);
202 // Return whether to cancel the current thread.
205 Workqueue::should_cancel_thread(int thread_number)
207 return this->threader_->should_cancel_thread(thread_number);
210 // Find a runnable task in TASKS. Return NULL if none could be found.
211 // If we find a Task waiting for a Token, add it to the list for that
212 // Token. The workqueue lock must be held when this is called.
215 Workqueue::find_runnable_in_list(Task_list* tasks)
218 while ((t = tasks->pop_front()) != NULL)
220 Task_token* token = t->is_runnable();
225 token->add_waiting(t);
229 // We couldn't find any runnable task.
233 // Find a runnable task. Return NULL if none could be found. The
234 // workqueue lock must be held when this is called.
237 Workqueue::find_runnable()
239 Task* t = this->find_runnable_in_list(&this->first_tasks_);
241 t = this->find_runnable_in_list(&this->tasks_);
245 // Find a runnable a task, and wait until we find one. Return NULL if
246 // we should exit. The workqueue lock must be held when this is
250 Workqueue::find_runnable_or_wait(int thread_number)
252 Task* t = this->find_runnable();
256 if (this->running_ == 0
257 && this->first_tasks_.empty()
258 && this->tasks_.empty())
260 // Kick all the threads to make them exit.
261 this->condvar_.broadcast();
263 gold_assert(this->waiting_ == 0);
267 if (this->should_cancel_thread(thread_number))
270 gold_debug(DEBUG_TASK, "%3d sleeping", thread_number);
272 this->condvar_.wait();
274 gold_debug(DEBUG_TASK, "%3d awake", thread_number);
276 t = this->find_runnable();
282 // Find and run tasks. If we can't find a runnable task, wait for one
283 // to become available. If we run a task, and it frees up another
284 // runnable task, then run that one too. This returns true if we
285 // should look for another task, false if we are cancelling this
289 Workqueue::find_and_run_task(int thread_number)
295 Hold_lock hl(this->lock_);
297 // Find a runnable task.
298 t = this->find_runnable_or_wait(thread_number);
303 // Get the locks for the task. This must be called while we are
304 // still holding the Workqueue lock.
312 gold_debug(DEBUG_TASK, "%3d running task %s", thread_number,
316 if (is_debugging_enabled(DEBUG_TASK))
321 if (is_debugging_enabled(DEBUG_TASK))
323 Timer::TimeStats elapsed = timer.get_elapsed_time();
325 gold_debug(DEBUG_TASK,
326 "%3d completed task %s "
327 "(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)",
328 thread_number, t->name().c_str(),
329 elapsed.user / 1000, (elapsed.user % 1000) * 1000,
330 elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
331 elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
336 Hold_lock hl(this->lock_);
340 // Release the locks for the task. This must be done with the
341 // workqueue lock held. Get the next Task to run if any.
342 next = this->release_locks(t, &tl);
345 next = this->find_runnable();
347 // If we have another Task to run, get the Locks. This must
348 // be called while we are still holding the Workqueue lock.
358 // We are done with this task.
367 // Handle the return value of release_locks, and get tasks ready to
370 // 1) If T is not runnable, queue it on the appropriate token.
372 // 2) Otherwise, T is runnable. If *PRET is not NULL, then we have
373 // already decided which Task to run next. Add T to the list of
374 // runnable tasks, and signal another thread.
376 // 3) Otherwise, *PRET is NULL. If IS_BLOCKER is false, then T was
377 // waiting on a write lock. We can grab that lock now, so we run T
380 // 4) Otherwise, IS_BLOCKER is true. If we should run T soon, then
383 // 5) Otherwise, check whether there are other tasks to run. If there
384 // are, then we generally get a better ordering if we run those tasks
385 // now, before T. A typical example is tasks waiting on the Dirsearch
386 // blocker. We don't want to run those tasks right away just because
387 // the Dirsearch was unblocked.
389 // 6) Otherwise, there are no other tasks to run, so we might as well
392 // This function must be called with the Workqueue lock held.
394 // Return true if we set *PRET to T, false otherwise.
397 Workqueue::return_or_queue(Task* t, bool is_blocker, Task** pret)
399 Task_token* token = t->is_runnable();
403 token->add_waiting(t);
408 bool should_queue = false;
409 bool should_return = false;
413 else if (!is_blocker)
414 should_return = true;
415 else if (t->should_run_soon())
416 should_return = true;
417 else if (!this->first_tasks_.empty() || !this->tasks_.empty())
420 should_return = true;
424 gold_assert(*pret == NULL);
428 else if (should_queue)
430 if (t->should_run_soon())
431 this->first_tasks_.push_back(t);
433 this->tasks_.push_back(t);
434 this->condvar_.signal();
441 // Release the locks associated with a Task. Return the first
442 // runnable Task that we find. If we find more runnable tasks, add
443 // them to the run queue and signal any other threads. This must be
444 // called with the Workqueue lock held.
447 Workqueue::release_locks(Task* t, Task_locker* tl)
450 for (Task_locker::iterator p = tl->begin(); p != tl->end(); ++p)
452 Task_token* token = *p;
453 if (token->is_blocker())
455 if (token->remove_blocker())
457 // The token has been unblocked. Every waiting Task may
460 while ((t = token->remove_first_waiting()) != NULL)
463 this->return_or_queue(t, true, &ret);
469 token->remove_writer(t);
471 // One more waiting Task may now be runnable. If we are
472 // going to run it next, we can stop. Otherwise we need to
473 // move all the Tasks to the runnable queue, to avoid a
474 // potential deadlock if the locking status changes before
475 // we run the next thread.
477 while ((t = token->remove_first_waiting()) != NULL)
480 if (this->return_or_queue(t, false, &ret))
488 // Process all the tasks on the workqueue. Keep going until the
489 // workqueue is empty, or until we have been told to exit. This
490 // function is called by all threads.
493 Workqueue::process(int thread_number)
495 while (this->find_and_run_task(thread_number))
499 // Set the number of threads to use for the workqueue, if we are using
503 Workqueue::set_thread_count(int threads)
505 Hold_lock hl(this->lock_);
507 this->threader_->set_thread_count(threads);
508 // Wake up all the threads, since something has changed.
509 this->condvar_.broadcast();
512 // Add a new blocker to an existing Task_token.
515 Workqueue::add_blocker(Task_token* token)
517 Hold_lock hl(this->lock_);
518 token->add_blocker();
521 } // End namespace gold.