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.
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 // Remove and return the first Task waiting for this lock to be
57 Task_list::pop_front()
59 Task
* ret
= this->head_
;
62 if (ret
== this->tail_
)
64 gold_assert(ret
->list_next() == NULL
);
70 this->head_
= ret
->list_next();
71 gold_assert(this->head_
!= NULL
);
72 ret
->clear_list_next();
78 // The simple single-threaded implementation of Workqueue_threader.
80 class Workqueue_threader_single
: public Workqueue_threader
83 Workqueue_threader_single(Workqueue
* workqueue
)
84 : Workqueue_threader(workqueue
)
86 ~Workqueue_threader_single()
90 set_thread_count(int thread_count
)
91 { gold_assert(thread_count
> 0); }
94 should_cancel_thread()
100 Workqueue::Workqueue(const General_options
& options
)
106 condvar_(this->lock_
),
109 bool threads
= options
.threads();
110 #ifndef ENABLE_THREADS
114 this->threader_
= new Workqueue_threader_single(this);
117 #ifdef ENABLE_THREADS
118 this->threader_
= new Workqueue_threader_threadpool(this);
125 Workqueue::~Workqueue()
129 // Add a task to the end of a specific queue, or put it on the list
130 // waiting for a Token.
133 Workqueue::add_to_queue(Task_list
* queue
, Task
* t
)
135 Hold_lock
hl(this->lock_
);
137 Task_token
* token
= t
->is_runnable();
140 token
->add_waiting(t
);
146 // Tell any waiting thread that there is work to do.
147 this->condvar_
.signal();
151 // Add a task to the queue.
154 Workqueue::queue(Task
* t
)
156 this->add_to_queue(&this->tasks_
, t
);
159 // Add a task to the front of the queue.
162 Workqueue::queue_front(Task
* t
)
164 t
->set_should_run_soon();
165 this->add_to_queue(&this->first_tasks_
, t
);
168 // Return whether to cancel the current thread.
171 Workqueue::should_cancel_thread()
173 return this->threader_
->should_cancel_thread();
176 // Find a runnable task in TASKS. Return NULL if none could be found.
177 // If we find a Task waiting for a Token, add it to the list for that
178 // Token. The workqueue lock must be held when this is called.
181 Workqueue::find_runnable_in_list(Task_list
* tasks
)
184 while ((t
= tasks
->pop_front()) != NULL
)
186 Task_token
* token
= t
->is_runnable();
191 token
->add_waiting(t
);
195 // We couldn't find any runnable task.
199 // Find a runnable task. Return NULL if none could be found. The
200 // workqueue lock must be held when this is called.
203 Workqueue::find_runnable()
205 Task
* t
= this->find_runnable_in_list(&this->first_tasks_
);
207 t
= this->find_runnable_in_list(&this->tasks_
);
211 // Find a runnable a task, and wait until we find one. Return NULL if
212 // we should exit. The workqueue lock must be held when this is
216 Workqueue::find_runnable_or_wait(int thread_number
)
218 Task
* t
= this->find_runnable();
222 if (this->running_
== 0
223 && this->first_tasks_
.empty()
224 && this->tasks_
.empty())
226 // Kick all the threads to make them exit.
227 this->condvar_
.broadcast();
229 gold_assert(this->waiting_
== 0);
233 if (this->should_cancel_thread())
236 gold_debug(DEBUG_TASK
, "%3d sleeping", thread_number
);
238 this->condvar_
.wait();
240 gold_debug(DEBUG_TASK
, "%3d awake", thread_number
);
242 t
= this->find_runnable();
248 // Find and run tasks. If we can't find a runnable task, wait for one
249 // to become available. If we run a task, and it frees up another
250 // runnable task, then run that one too. This returns true if we
251 // should look for another task, false if we are cancelling this
255 Workqueue::find_and_run_task(int thread_number
)
261 Hold_lock
hl(this->lock_
);
263 // Find a runnable task.
264 t
= this->find_runnable_or_wait(thread_number
);
269 // Get the locks for the task. This must be called while we are
270 // still holding the Workqueue lock.
278 gold_debug(DEBUG_TASK
, "%3d running task %s", thread_number
,
283 gold_debug(DEBUG_TASK
, "%3d completed task %s", thread_number
,
288 Hold_lock
hl(this->lock_
);
292 // Release the locks for the task. This must be done with the
293 // workqueue lock held. Get the next Task to run if any.
294 next
= this->release_locks(t
, &tl
);
297 next
= this->find_runnable();
299 // If we have another Task to run, get the Locks. This must
300 // be called while we are still holding the Workqueue lock.
310 // We are done with this task.
319 // Handle the return value of release_locks, and get tasks ready to
322 // 1) If T is not runnable, queue it on the appropriate token.
324 // 2) Otherwise, T is runnable. If *PRET is not NULL, then we have
325 // already decided which Task to run next. Add T to the list of
326 // runnable tasks, and signal another thread.
328 // 3) Otherwise, *PRET is NULL. If IS_BLOCKER is false, then T was
329 // waiting on a write lock. We can grab that lock now, so we run T
332 // 4) Otherwise, IS_BLOCKER is true. If we should run T soon, then
335 // 5) Otherwise, check whether there are other tasks to run. If there
336 // are, then we generally get a better ordering if we run those tasks
337 // now, before T. A typical example is tasks waiting on the Dirsearch
338 // blocker. We don't want to run those tasks right away just because
339 // the Dirsearch was unblocked.
341 // 6) Otherwise, there are no other tasks to run, so we might as well
344 // This function must be called with the Workqueue lock held.
346 // Return true if we set *PRET to T, false otherwise.
349 Workqueue::return_or_queue(Task
* t
, bool is_blocker
, Task
** pret
)
351 Task_token
* token
= t
->is_runnable();
355 token
->add_waiting(t
);
360 bool should_queue
= false;
361 bool should_return
= false;
365 else if (!is_blocker
)
366 should_return
= true;
367 else if (t
->should_run_soon())
368 should_return
= true;
369 else if (!this->first_tasks_
.empty() || !this->tasks_
.empty())
372 should_return
= true;
376 gold_assert(*pret
== NULL
);
380 else if (should_queue
)
382 if (t
->should_run_soon())
383 this->first_tasks_
.push_back(t
);
385 this->tasks_
.push_back(t
);
386 this->condvar_
.signal();
393 // Release the locks associated with a Task. Return the first
394 // runnable Task that we find. If we find more runnable tasks, add
395 // them to the run queue and signal any other threads. This must be
396 // called with the Workqueue lock held.
399 Workqueue::release_locks(Task
* t
, Task_locker
* tl
)
402 for (Task_locker::iterator p
= tl
->begin(); p
!= tl
->end(); ++p
)
404 Task_token
* token
= *p
;
405 if (token
->is_blocker())
407 if (token
->remove_blocker())
409 // The token has been unblocked. Every waiting Task may
412 while ((t
= token
->remove_first_waiting()) != NULL
)
415 this->return_or_queue(t
, true, &ret
);
421 token
->remove_writer(t
);
423 // One more waiting Task may now be runnable. If we are
424 // going to run it next, we can stop. Otherwise we need to
425 // move all the Tasks to the runnable queue, to avoid a
426 // potential deadlock if the locking status changes before
427 // we run the next thread.
429 while ((t
= token
->remove_first_waiting()) != NULL
)
432 if (this->return_or_queue(t
, false, &ret
))
440 // Process all the tasks on the workqueue. Keep going until the
441 // workqueue is empty, or until we have been told to exit. This
442 // function is called by all threads.
445 Workqueue::process(int thread_number
)
447 while (this->find_and_run_task(thread_number
))
451 // Set the number of threads to use for the workqueue, if we are using
455 Workqueue::set_thread_count(int threads
)
457 Hold_lock
hl(this->lock_
);
459 this->threader_
->set_thread_count(threads
);
460 // Wake up all the threads, since something has changed.
461 this->condvar_
.broadcast();
464 } // End namespace gold.