1 // workqueue.h -- the work queue for gold -*- C++ -*-
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.
23 // After processing the command line, everything the linker does is
24 // driven from a work queue. This permits us to parallelize the
25 // linker where possible.
28 // A simple locking implementation to ensure proper task ordering.
29 // Task_read_token, Task_write_token
30 // Lock a Task_token for read or write.
32 // Task locking using RAII.
34 // An abstract class for jobs to run.
36 #ifndef GOLD_WORKQUEUE_H
37 #define GOLD_WORKQUEUE_H
39 #include "gold-threads.h"
45 class General_options
;
49 // Some tasks require access to shared data structures, such as the
50 // symbol table. Some tasks must be executed in a particular error,
51 // such as reading input file symbol tables--if we see foo.o -llib, we
52 // have to read the symbols for foo.o before we read the ones for
53 // -llib. To implement this safely and efficiently, we use tokens.
54 // Task_tokens support shared read/exclusive write access to some
55 // resource. Alternatively, they support blockers: blockers implement
56 // the requirement that some set of tasks must complete before another
57 // set of tasks can start. In such a case we increment the block
58 // count when we create the task, and decrement it when the task
59 // completes. Task_tokens are only manipulated by the main thread, so
60 // they do not themselves require any locking.
69 // A read/write token uses these methods.
84 add_writer(const Task
*);
87 remove_writer(const Task
*);
90 has_write_lock(const Task
*);
92 // A blocker token uses these methods.
97 // Returns true if block count drops to zero.
105 // It makes no sense to copy these.
106 Task_token(const Task_token
&);
107 Task_token
& operator=(const Task_token
&);
114 // In order to support tokens more reliably, we provide objects which
115 // handle them using RAII.
117 class Task_read_token
120 Task_read_token(Task_token
& token
)
122 { this->token_
.add_reader(); }
125 { this->token_
.remove_reader(); }
128 Task_read_token(const Task_read_token
&);
129 Task_read_token
& operator=(const Task_read_token
&);
134 class Task_write_token
137 Task_write_token(Task_token
& token
, const Task
* task
)
138 : token_(token
), task_(task
)
139 { this->token_
.add_writer(this->task_
); }
142 { this->token_
.remove_writer(this->task_
); }
145 Task_write_token(const Task_write_token
&);
146 Task_write_token
& operator=(const Task_write_token
&);
152 class Task_block_token
155 // The blocker count must be incremented when the task is created.
156 // This object is created when the task is run. When we unblock the
157 // last task, we notify the workqueue.
158 Task_block_token(Task_token
& token
, Workqueue
* workqueue
);
162 Task_block_token(const Task_block_token
&);
163 Task_block_token
& operator=(const Task_block_token
&);
166 Workqueue
* workqueue_
;
169 // An object which implements an RAII lock for any object which
170 // supports lock and unlock methods.
172 template<typename Obj
>
176 Task_lock_obj(Obj
& obj
)
178 { this->obj_
.lock(); }
181 { this->obj_
.unlock(); }
184 Task_lock_obj(const Task_lock_obj
&);
185 Task_lock_obj
& operator=(const Task_lock_obj
&);
190 // An abstract class used to lock Task_tokens using RAII. A typical
191 // implementation would simply have a set of members of type
192 // Task_read_token, Task_write_token, and Task_block_token.
200 virtual ~Task_locker()
204 // A version of Task_locker which may be used for a single read lock.
206 class Task_locker_read
: public Task_locker
209 Task_locker_read(Task_token
& token
)
214 Task_locker_read(const Task_locker_read
&);
215 Task_locker_read
& operator=(const Task_locker_read
&);
217 Task_read_token read_token_
;
220 // A version of Task_locker which may be used for a single write lock.
222 class Task_locker_write
: public Task_locker
225 Task_locker_write(Task_token
& token
, const Task
* task
)
226 : write_token_(token
, task
)
230 Task_locker_write(const Task_locker_write
&);
231 Task_locker_write
& operator=(const Task_locker_write
&);
233 Task_write_token write_token_
;
236 // A version of Task_locker which may be used for a single blocker
239 class Task_locker_block
: public Task_locker
242 Task_locker_block(Task_token
& token
, Workqueue
* workqueue
)
243 : block_token_(token
, workqueue
)
247 Task_locker_block(const Task_locker_block
&);
248 Task_locker_block
& operator=(const Task_locker_block
&);
250 Task_block_token block_token_
;
253 // A version of Task_locker which may be used to hold a lock on any
254 // object which supports lock() and unlock() methods.
256 template<typename Obj
>
257 class Task_locker_obj
: public Task_locker
260 Task_locker_obj(Obj
& obj
)
265 Task_locker_obj(const Task_locker_obj
&);
266 Task_locker_obj
& operator=(const Task_locker_obj
&);
268 Task_lock_obj
<Obj
> obj_lock_
;
271 // The superclass for tasks to be placed on the workqueue. Each
272 // specific task class will inherit from this one.
282 // Type returned by Is_runnable.
283 enum Is_runnable_type
287 // Task is waiting for a block to clear.
289 // Task is not waiting for a block, but is not runnable--i.e., is
290 // waiting for a lock.
294 // Return whether the task can be run now. This method is only
295 // called from the main thread.
296 virtual Is_runnable_type
297 is_runnable(Workqueue
*) = 0;
299 // Return a pointer to a Task_locker which locks all the resources
300 // required by the task. We delete the pointer when the task is
301 // complete. This method can return NULL if no locks are required.
302 // This method is only called from the main thread.
304 locks(Workqueue
*) = 0;
312 Task
& operator=(const Task
&);
315 // A simple task which waits for a blocker and then runs a function.
317 class Task_function_runner
320 virtual ~Task_function_runner()
327 class Task_function
: public Task
330 // Both points should be allocated using new, and will be deleted
331 // after the task runs.
332 Task_function(Task_function_runner
* runner
, Task_token
* blocker
)
333 : runner_(runner
), blocker_(blocker
)
338 delete this->runner_
;
339 delete this->blocker_
;
342 // The standard task methods.
344 // Wait until the task is unblocked.
346 is_runnable(Workqueue
*)
347 { return this->blocker_
->is_blocked() ? IS_BLOCKED
: IS_RUNNABLE
; }
349 // This type of task does not normally hold any locks.
356 run(Workqueue
* workqueue
)
357 { this->runner_
->run(workqueue
); }
360 Task_function(const Task_function
&);
361 Task_function
& operator=(const Task_function
&);
363 Task_function_runner
* runner_
;
364 Task_token
* blocker_
;
369 class Workqueue_runner
;
374 Workqueue(const General_options
&);
377 // Add a new task to the work queue.
381 // Add a new task to the front of the work queue. It will be the
382 // next task to run if it is ready.
386 // Process all the tasks on the work queue.
390 // A complete set of blocking tasks has completed.
395 // This class can not be copied.
396 Workqueue(const Workqueue
&);
397 Workqueue
& operator=(const Workqueue
&);
399 typedef std::list
<Task
*> Task_list
;
404 friend class Workqueue_runner
;
406 // Find a runnable task.
407 Task
* find_runnable(Task_list
&, bool*);
409 // Add a lock to the completed queue.
410 void completed(Task
*, Task_locker
*);
412 // Clear the completed queue.
413 bool clear_completed();
415 // How to run a task. Only accessed from main thread.
416 Workqueue_runner
* runner_
;
418 // Lock for access to tasks_ members.
420 // List of tasks to execute at each link level.
423 // Lock for access to completed_ and running_ members.
424 Lock completed_lock_
;
425 // List of Task_locker objects for main thread to free.
426 std::list
<Task_locker
*> completed_
;
427 // Number of tasks currently running.
429 // Condition variable signalled when a new entry is added to completed_.
430 Condvar completed_condvar_
;
432 // Number of blocker tokens which were fully cleared. Only accessed
434 int cleared_blockers_
;
437 } // End namespace gold.
439 #endif // !defined(GOLD_WORKQUEUE_H)