binutils-gdb/gold/workqueue.cc

522 lines
12 KiB
C++
Raw Normal View History

2006-08-05 01:10:59 +02:00
// workqueue.cc -- the workqueue for gold
2014-03-05 12:46:15 +01:00
// Copyright (C) 2006-2014 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.
2006-08-05 01:10:59 +02:00
#include "gold.h"
2006-08-05 01:20:34 +02:00
2007-11-22 01:05:51 +01:00
#include "debug.h"
#include "options.h"
#include "timer.h"
2006-08-05 01:10:59 +02:00
#include "workqueue.h"
2007-11-22 01:05:51 +01:00
#include "workqueue-internal.h"
2006-08-05 01:10:59 +02:00
namespace gold
{
// Class Task_list.
2006-08-05 01:10:59 +02:00
// Add T to the end of the list.
2006-08-05 01:10:59 +02:00
inline void
Task_list::push_back(Task* t)
2006-08-05 01:10:59 +02:00
{
gold_assert(t->list_next() == NULL);
if (this->head_ == NULL)
{
this->head_ = t;
this->tail_ = t;
}
2006-08-05 01:10:59 +02:00
else
{
this->tail_->set_list_next(t);
this->tail_ = t;
}
2006-08-05 01:10:59 +02:00
}
// Add T to the front of the list.
inline void
Task_list::push_front(Task* t)
{
gold_assert(t->list_next() == NULL);
if (this->head_ == NULL)
{
this->head_ = t;
this->tail_ = t;
}
else
{
t->set_list_next(this->head_);
this->head_ = t;
}
}
// Remove and return the first Task waiting for this lock to be
// released.
2006-08-05 01:10:59 +02:00
inline Task*
Task_list::pop_front()
2006-08-05 01:10:59 +02:00
{
Task* ret = this->head_;
if (ret != NULL)
2006-08-05 01:10:59 +02:00
{
if (ret == this->tail_)
{
gold_assert(ret->list_next() == NULL);
this->head_ = NULL;
this->tail_ = NULL;
}
else
{
this->head_ = ret->list_next();
gold_assert(this->head_ != NULL);
ret->clear_list_next();
}
2006-08-05 01:10:59 +02:00
}
return ret;
2006-08-05 01:10:59 +02:00
}
// The simple single-threaded implementation of Workqueue_threader.
2006-08-05 01:10:59 +02:00
class Workqueue_threader_single : public Workqueue_threader
2006-08-05 01:10:59 +02:00
{
public:
Workqueue_threader_single(Workqueue* workqueue)
: Workqueue_threader(workqueue)
2006-08-05 01:10:59 +02:00
{ }
~Workqueue_threader_single()
2006-08-05 01:10:59 +02:00
{ }
void
set_thread_count(int thread_count)
{ gold_assert(thread_count > 0); }
bool
should_cancel_thread(int)
{ return false; }
2006-08-05 01:10:59 +02:00
};
// Workqueue methods.
Workqueue::Workqueue(const General_options& options)
: lock_(),
2007-11-22 01:05:51 +01:00
first_tasks_(),
2006-08-05 01:10:59 +02:00
tasks_(),
running_(0),
waiting_(0),
condvar_(this->lock_),
threader_(NULL)
2006-08-05 01:10:59 +02:00
{
bool threads = options.threads();
#ifndef ENABLE_THREADS
threads = false;
#endif
if (!threads)
this->threader_ = new Workqueue_threader_single(this);
else
2007-11-22 01:05:51 +01:00
{
#ifdef ENABLE_THREADS
this->threader_ = new Workqueue_threader_threadpool(this);
2007-11-22 01:05:51 +01:00
#else
gold_unreachable();
#endif
}
2006-08-05 01:10:59 +02:00
}
Workqueue::~Workqueue()
{
}
// Add a task to the end of a specific queue, or put it on the list
// waiting for a Token.
void
Workqueue::add_to_queue(Task_list* queue, Task* t, bool front)
{
Hold_lock hl(this->lock_);
Task_token* token = t->is_runnable();
if (token != NULL)
{
if (front)
token->add_waiting_front(t);
else
token->add_waiting(t);
++this->waiting_;
}
else
{
if (front)
queue->push_front(t);
else
queue->push_back(t);
// Tell any waiting thread that there is work to do.
this->condvar_.signal();
}
2006-08-05 01:10:59 +02:00
}
// Add a task to the queue.
2006-08-05 01:10:59 +02:00
void
Workqueue::queue(Task* t)
{
this->add_to_queue(&this->tasks_, t, false);
}
// Queue a task which should run soon.
void
Workqueue::queue_soon(Task* t)
{
t->set_should_run_soon();
this->add_to_queue(&this->first_tasks_, t, false);
2006-08-05 01:10:59 +02:00
}
// Queue a task which should run next.
void
Workqueue::queue_next(Task* t)
{
t->set_should_run_soon();
this->add_to_queue(&this->first_tasks_, t, true);
}
// Return whether to cancel the current thread.
2006-08-05 01:10:59 +02:00
inline bool
Workqueue::should_cancel_thread(int thread_number)
2006-08-05 01:10:59 +02:00
{
return this->threader_->should_cancel_thread(thread_number);
2006-08-05 01:10:59 +02:00
}
// Find a runnable task in TASKS. Return NULL if none could be found.
// If we find a Task waiting for a Token, add it to the list for that
// Token. The workqueue lock must be held when this is called.
2006-08-05 01:10:59 +02:00
Task*
Workqueue::find_runnable_in_list(Task_list* tasks)
2006-08-05 01:10:59 +02:00
{
2007-11-22 01:05:51 +01:00
Task* t;
while ((t = tasks->pop_front()) != NULL)
2006-08-05 01:10:59 +02:00
{
Task_token* token = t->is_runnable();
if (token == NULL)
return t;
token->add_waiting(t);
++this->waiting_;
}
// We couldn't find any runnable task.
return NULL;
}
// Find a runnable task. Return NULL if none could be found. The
// workqueue lock must be held when this is called.
2006-08-05 01:10:59 +02:00
Task*
Workqueue::find_runnable()
{
Task* t = this->find_runnable_in_list(&this->first_tasks_);
if (t == NULL)
t = this->find_runnable_in_list(&this->tasks_);
return t;
}
// Find a runnable a task, and wait until we find one. Return NULL if
// we should exit. The workqueue lock must be held when this is
// called.
Task*
Workqueue::find_runnable_or_wait(int thread_number)
{
Task* t = this->find_runnable();
while (t == NULL)
{
if (this->running_ == 0
&& this->first_tasks_.empty()
&& this->tasks_.empty())
2006-08-05 01:10:59 +02:00
{
// Kick all the threads to make them exit.
this->condvar_.broadcast();
2006-08-05 01:10:59 +02:00
gold_assert(this->waiting_ == 0);
return NULL;
2006-08-05 01:10:59 +02:00
}
2007-11-22 01:05:51 +01:00
if (this->should_cancel_thread(thread_number))
return NULL;
gold_debug(DEBUG_TASK, "%3d sleeping", thread_number);
2007-11-22 01:05:51 +01:00
this->condvar_.wait();
gold_debug(DEBUG_TASK, "%3d awake", thread_number);
t = this->find_runnable();
2006-08-05 01:10:59 +02:00
}
2007-11-22 01:05:51 +01:00
return t;
2006-08-05 01:10:59 +02:00
}
// Find and run tasks. If we can't find a runnable task, wait for one
// to become available. If we run a task, and it frees up another
// runnable task, then run that one too. This returns true if we
// should look for another task, false if we are cancelling this
// thread.
2006-08-05 01:10:59 +02:00
bool
Workqueue::find_and_run_task(int thread_number)
2006-08-05 01:10:59 +02:00
{
Task* t;
Task_locker tl;
{
Hold_lock hl(this->lock_);
// Find a runnable task.
t = this->find_runnable_or_wait(thread_number);
if (t == NULL)
return false;
// Get the locks for the task. This must be called while we are
// still holding the Workqueue lock.
t->locks(&tl);
++this->running_;
}
while (t != NULL)
2006-08-05 01:10:59 +02:00
{
gold_debug(DEBUG_TASK, "%3d running task %s", thread_number,
t->name().c_str());
2006-08-05 01:10:59 +02:00
Timer timer;
if (is_debugging_enabled(DEBUG_TASK))
timer.start();
t->run(this);
2007-11-22 01:05:51 +01:00
if (is_debugging_enabled(DEBUG_TASK))
{
Timer::TimeStats elapsed = timer.get_elapsed_time();
gold_debug(DEBUG_TASK,
"%3d completed task %s "
"(user: %ld.%06ld sys: %ld.%06ld wall: %ld.%06ld)",
thread_number, t->name().c_str(),
elapsed.user / 1000, (elapsed.user % 1000) * 1000,
elapsed.sys / 1000, (elapsed.sys % 1000) * 1000,
elapsed.wall / 1000, (elapsed.wall % 1000) * 1000);
}
2007-11-22 01:05:51 +01:00
Task* next;
2006-08-05 01:10:59 +02:00
{
Hold_lock hl(this->lock_);
2006-08-05 01:10:59 +02:00
--this->running_;
2007-11-22 01:05:51 +01:00
// Release the locks for the task. This must be done with the
// workqueue lock held. Get the next Task to run if any.
next = this->release_locks(t, &tl);
if (next == NULL)
next = this->find_runnable();
// If we have another Task to run, get the Locks. This must
// be called while we are still holding the Workqueue lock.
if (next != NULL)
2007-11-22 01:05:51 +01:00
{
tl.clear();
next->locks(&tl);
++this->running_;
2006-08-05 01:10:59 +02:00
}
}
// We are done with this task.
delete t;
2006-08-05 01:10:59 +02:00
t = next;
2006-08-05 01:10:59 +02:00
}
return true;
2006-08-05 01:10:59 +02:00
}
// Handle the return value of release_locks, and get tasks ready to
// run.
2006-08-05 01:10:59 +02:00
// 1) If T is not runnable, queue it on the appropriate token.
2007-11-22 01:05:51 +01:00
// 2) Otherwise, T is runnable. If *PRET is not NULL, then we have
// already decided which Task to run next. Add T to the list of
// runnable tasks, and signal another thread.
2006-08-05 01:10:59 +02:00
// 3) Otherwise, *PRET is NULL. If IS_BLOCKER is false, then T was
// waiting on a write lock. We can grab that lock now, so we run T
// now.
2006-08-05 01:10:59 +02:00
// 4) Otherwise, IS_BLOCKER is true. If we should run T soon, then
// run it now.
// 5) Otherwise, check whether there are other tasks to run. If there
// are, then we generally get a better ordering if we run those tasks
// now, before T. A typical example is tasks waiting on the Dirsearch
// blocker. We don't want to run those tasks right away just because
// the Dirsearch was unblocked.
// 6) Otherwise, there are no other tasks to run, so we might as well
// run this one now.
// This function must be called with the Workqueue lock held.
// Return true if we set *PRET to T, false otherwise.
bool
Workqueue::return_or_queue(Task* t, bool is_blocker, Task** pret)
2006-08-05 01:10:59 +02:00
{
Task_token* token = t->is_runnable();
2007-11-22 01:05:51 +01:00
if (token != NULL)
{
token->add_waiting(t);
++this->waiting_;
return false;
}
bool should_queue = false;
bool should_return = false;
if (*pret != NULL)
should_queue = true;
else if (!is_blocker)
should_return = true;
else if (t->should_run_soon())
should_return = true;
else if (!this->first_tasks_.empty() || !this->tasks_.empty())
should_queue = true;
else
should_return = true;
2007-11-22 01:05:51 +01:00
if (should_return)
{
gold_assert(*pret == NULL);
*pret = t;
return true;
}
else if (should_queue)
{
if (t->should_run_soon())
this->first_tasks_.push_back(t);
else
this->tasks_.push_back(t);
this->condvar_.signal();
return false;
}
gold_unreachable();
2006-08-05 01:10:59 +02:00
}
// Release the locks associated with a Task. Return the first
// runnable Task that we find. If we find more runnable tasks, add
// them to the run queue and signal any other threads. This must be
// called with the Workqueue lock held.
2006-08-05 01:10:59 +02:00
Task*
Workqueue::release_locks(Task* t, Task_locker* tl)
2006-08-05 01:10:59 +02:00
{
Task* ret = NULL;
for (Task_locker::iterator p = tl->begin(); p != tl->end(); ++p)
{
Task_token* token = *p;
if (token->is_blocker())
{
if (token->remove_blocker())
{
// The token has been unblocked. Every waiting Task may
// now be runnable.
Task* t;
while ((t = token->remove_first_waiting()) != NULL)
{
--this->waiting_;
this->return_or_queue(t, true, &ret);
}
}
}
else
{
token->remove_writer(t);
// One more waiting Task may now be runnable. If we are
// going to run it next, we can stop. Otherwise we need to
// move all the Tasks to the runnable queue, to avoid a
// potential deadlock if the locking status changes before
// we run the next thread.
Task* t;
while ((t = token->remove_first_waiting()) != NULL)
{
--this->waiting_;
if (this->return_or_queue(t, false, &ret))
break;
}
}
}
return ret;
2006-08-05 01:10:59 +02:00
}
// Process all the tasks on the workqueue. Keep going until the
// workqueue is empty, or until we have been told to exit. This
// function is called by all threads.
void
Workqueue::process(int thread_number)
{
while (this->find_and_run_task(thread_number))
;
}
// Set the number of threads to use for the workqueue, if we are using
// threads.
2007-11-22 01:05:51 +01:00
void
Workqueue::set_thread_count(int threads)
2007-11-22 01:05:51 +01:00
{
Hold_lock hl(this->lock_);
this->threader_->set_thread_count(threads);
// Wake up all the threads, since something has changed.
this->condvar_.broadcast();
2007-11-22 01:05:51 +01:00
}
* readsyms.cc (Read_symbols::incompatible_warning): New function. (Read_symbols::requeue): New function. (Read_symbols::do_read_symbols): If make_elf_object fails because the target type is not configured, and the file was searched for, issue a warning and retry with the next directory. (Add_symbols::run): If the file has an incompatible format, and it was searched for, requeue the Read_symbols task. On error, release the object. * readsyms.h (class Read_symbols): Add dirindex_ field. Add dirindex parameter to constructor. Change all callers. Declare incompatible_warning and requeue. (class Add_symbols): Add dirpath_, dirindex_, mapfile_, input_argument_ and input_group_ fields. Add them to constructor. Change all callers. (class Read_script): Add dirindex_ field. Add it to constructor. Change all callers. * archive.cc (Archive::setup): Remove input_objects parameter. Change all callers. (Archive::get_file_and_offset): Likewise. (Archive::read_all_symbols): Likewise. (Archive::read_symbols): Likewise. (Archive::get_elf_object_for_member): Remove input_objects parameter. Add punconfigured parameter. Change all callers. (Archive::add_symbols): Change return type to bool. Check return value of include_member. (Archive::include_all_members): Likewise. (Archive::include_member): Change return type to bool. Return false if first included object has incompatible target. Set included_member_ field. (Add_archive_symbols::run): If add_symbols returns false, requeue Read_symbols task. * archive.h (class Archive): Add included_member_ field. Initialize it in constructor. Add input_file and searched_for methods. Update declarations. (class Add_archive_symbols): Add dirpath_, dirindex_, and input_argument_ fields. Add them to constructor. Change all callers. * script.cc: Include "target-select.h". (class Parser_closure): Add skip_on_incompatible_target_ and found_incompatible_target_ fields. Add skip_on_incompatible_target parameter to constructor. Change all callers. Add methods skip_on_incompatible_target, clear_skip_on_incompatible_target, found_incompatible_target, and set_found_incompatible_target. (read_input_script): Add dirindex parameter. Change all callers. If parser finds an incompatible target, requeue Read_symbols task. (script_set_symbol): Clear skip_on_incompatible_target in closure. (script_add_assertion, script_parse_option): Likewise. (script_start_sections, script_add_phdr): Likewise. (script_check_output_format): New function. * script.h (read_input_script): Update declaration. * script-c.h (script_check_output_format): Declare. * yyscript.y (file_cmd): Handle OUTPUT_FORMAT. (ignore_cmd): Remove OUTPUT_FORMAT. * fileread.cc (Input_file::Input_file): Add explicit this. (Input_file::will_search_for): New function. (Input_file::open): Add pindex parameter. Change all callers. * fileread.h (class Input_file): Add input_file_argument method. Declare will_search_for. Update declarations. * object.cc (make_elf_object): Add punconfigured parameter. Change all callers. * object.h (class Object): Make input_file public. Add searched_for method. (make_elf_object): Update declaration. * dirsearch.cc (Dirsearch::find): Add pindex parameter. Use it to restart search. * dirsearch.h (class Dirsearch): Update declaration. * options.h (class General_options): Add --warn-search-mismatch. * parameters.cc (Parameters::is_compatible_target): New function. * parameters.h (class Parameters): Declare is_compatible_target. * workqueue.cc (Workqueue::add_blocker): New function. * workqueue.h (class Workqueue): Declare add_blocker.
2009-03-14 06:56:46 +01:00
// Add a new blocker to an existing Task_token.
void
Workqueue::add_blocker(Task_token* token)
{
Hold_lock hl(this->lock_);
token->add_blocker();
}
2006-08-05 01:10:59 +02:00
} // End namespace gold.