[PATCH 25/57][Arm][GAS] Add support for MVE instruction: vmvn, vqabs and vqneg
[binutils-gdb.git] / gold / workqueue.cc
blob06c48c50486ce14a552290b49d9aea381dae7d4e
1 // workqueue.cc -- the workqueue for gold
3 // Copyright (C) 2006-2019 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 "debug.h"
26 #include "options.h"
27 #include "timer.h"
28 #include "workqueue.h"
29 #include "workqueue-internal.h"
31 namespace gold
34 // Class Task_list.
36 // Add T to the end of the list.
38 inline void
39 Task_list::push_back(Task* t)
41 gold_assert(t->list_next() == NULL);
42 if (this->head_ == NULL)
44 this->head_ = t;
45 this->tail_ = t;
47 else
49 this->tail_->set_list_next(t);
50 this->tail_ = t;
54 // Add T to the front of the list.
56 inline void
57 Task_list::push_front(Task* t)
59 gold_assert(t->list_next() == NULL);
60 if (this->head_ == NULL)
62 this->head_ = t;
63 this->tail_ = t;
65 else
67 t->set_list_next(this->head_);
68 this->head_ = t;
72 // Remove and return the first Task waiting for this lock to be
73 // released.
75 inline Task*
76 Task_list::pop_front()
78 Task* ret = this->head_;
79 if (ret != NULL)
81 if (ret == this->tail_)
83 gold_assert(ret->list_next() == NULL);
84 this->head_ = NULL;
85 this->tail_ = NULL;
87 else
89 this->head_ = ret->list_next();
90 gold_assert(this->head_ != NULL);
91 ret->clear_list_next();
94 return ret;
97 // The simple single-threaded implementation of Workqueue_threader.
99 class Workqueue_threader_single : public Workqueue_threader
101 public:
102 Workqueue_threader_single(Workqueue* workqueue)
103 : Workqueue_threader(workqueue)
105 ~Workqueue_threader_single()
108 void
109 set_thread_count(int thread_count)
110 { gold_assert(thread_count > 0); }
112 bool
113 should_cancel_thread(int)
114 { return false; }
117 // Workqueue methods.
119 Workqueue::Workqueue(const General_options& options)
120 : lock_(),
121 first_tasks_(),
122 tasks_(),
123 running_(0),
124 waiting_(0),
125 condvar_(this->lock_),
126 threader_(NULL)
128 bool threads = options.threads();
129 #ifndef ENABLE_THREADS
130 threads = false;
131 #endif
132 if (!threads)
133 this->threader_ = new Workqueue_threader_single(this);
134 else
136 #ifdef ENABLE_THREADS
137 this->threader_ = new Workqueue_threader_threadpool(this);
138 #else
139 gold_unreachable();
140 #endif
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.
151 void
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();
157 if (token != NULL)
159 if (front)
160 token->add_waiting_front(t);
161 else
162 token->add_waiting(t);
163 ++this->waiting_;
165 else
167 if (front)
168 queue->push_front(t);
169 else
170 queue->push_back(t);
171 // Tell any waiting thread that there is work to do.
172 this->condvar_.signal();
176 // Add a task to the queue.
178 void
179 Workqueue::queue(Task* t)
181 this->add_to_queue(&this->tasks_, t, false);
184 // Queue a task which should run soon.
186 void
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.
195 void
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.
204 inline bool
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.
214 Task*
215 Workqueue::find_runnable_in_list(Task_list* tasks)
217 Task* t;
218 while ((t = tasks->pop_front()) != NULL)
220 Task_token* token = t->is_runnable();
222 if (token == NULL)
223 return t;
225 token->add_waiting(t);
226 ++this->waiting_;
229 // We couldn't find any runnable task.
230 return NULL;
233 // Find a runnable task. Return NULL if none could be found. The
234 // workqueue lock must be held when this is called.
236 Task*
237 Workqueue::find_runnable()
239 Task* t = this->find_runnable_in_list(&this->first_tasks_);
240 if (t == NULL)
241 t = this->find_runnable_in_list(&this->tasks_);
242 return t;
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
247 // called.
249 Task*
250 Workqueue::find_runnable_or_wait(int thread_number)
252 Task* t = this->find_runnable();
254 while (t == NULL)
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);
264 return NULL;
267 if (this->should_cancel_thread(thread_number))
268 return NULL;
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();
279 return t;
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
286 // thread.
288 bool
289 Workqueue::find_and_run_task(int thread_number)
291 Task* t;
292 Task_locker tl;
295 Hold_lock hl(this->lock_);
297 // Find a runnable task.
298 t = this->find_runnable_or_wait(thread_number);
300 if (t == NULL)
301 return false;
303 // Get the locks for the task. This must be called while we are
304 // still holding the Workqueue lock.
305 t->locks(&tl);
307 ++this->running_;
310 while (t != NULL)
312 gold_debug(DEBUG_TASK, "%3d running task %s", thread_number,
313 t->name().c_str());
315 Timer timer;
316 if (is_debugging_enabled(DEBUG_TASK))
317 timer.start();
319 t->run(this);
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);
334 Task* next;
336 Hold_lock hl(this->lock_);
338 --this->running_;
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);
344 if (next == NULL)
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.
349 if (next != NULL)
351 tl.clear();
352 next->locks(&tl);
354 ++this->running_;
358 // We are done with this task.
359 delete t;
361 t = next;
364 return true;
367 // Handle the return value of release_locks, and get tasks ready to
368 // run.
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
378 // now.
380 // 4) Otherwise, IS_BLOCKER is true. If we should run T soon, then
381 // run it now.
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
390 // run this one now.
392 // This function must be called with the Workqueue lock held.
394 // Return true if we set *PRET to T, false otherwise.
396 bool
397 Workqueue::return_or_queue(Task* t, bool is_blocker, Task** pret)
399 Task_token* token = t->is_runnable();
401 if (token != NULL)
403 token->add_waiting(t);
404 ++this->waiting_;
405 return false;
408 bool should_queue = false;
409 bool should_return = false;
411 if (*pret != NULL)
412 should_queue = true;
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())
418 should_queue = true;
419 else
420 should_return = true;
422 if (should_return)
424 gold_assert(*pret == NULL);
425 *pret = t;
426 return true;
428 else if (should_queue)
430 if (t->should_run_soon())
431 this->first_tasks_.push_back(t);
432 else
433 this->tasks_.push_back(t);
434 this->condvar_.signal();
435 return false;
438 gold_unreachable();
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.
446 Task*
447 Workqueue::release_locks(Task* t, Task_locker* tl)
449 Task* ret = NULL;
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
458 // now be runnable.
459 Task* t;
460 while ((t = token->remove_first_waiting()) != NULL)
462 --this->waiting_;
463 this->return_or_queue(t, true, &ret);
467 else
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.
476 Task* t;
477 while ((t = token->remove_first_waiting()) != NULL)
479 --this->waiting_;
480 if (this->return_or_queue(t, false, &ret))
481 break;
485 return 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.
492 void
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
500 // threads.
502 void
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.
514 void
515 Workqueue::add_blocker(Task_token* token)
517 Hold_lock hl(this->lock_);
518 token->add_blocker();
521 } // End namespace gold.