1 // workqueue.h -- the work queue for gold -*- C++ -*-
3 // After processing the command line, everything the linker does is
4 // driven from a work queue. This permits us to parallelize the
5 // linker where possible.
8 // A simple locking implementation to ensure proper task ordering.
9 // Task_read_token, Task_write_token
10 // Lock a Task_token for read or write.
12 // Task locking using RAII.
14 // An abstract class for jobs to run.
16 #ifndef GOLD_WORKQUEUE_H
17 #define GOLD_WORKQUEUE_H
19 #include "gold-threads.h"
29 // Some tasks require access to shared data structures, such as the
30 // symbol table. Some tasks must be executed in a particular error,
31 // such as reading input file symbol tables--if we see foo.o -llib, we
32 // have to read the symbols for foo.o before we read the ones for
33 // -llib. To implement this safely and efficiently, we use tokens.
34 // Task_tokens support shared read/exclusive write access to some
35 // resource. Alternatively, they support blockers: blockers implement
36 // the requirement that some set of tasks must complete before another
37 // set of tasks can start. In such a case we increment the block
38 // count when we create the task, and decrement it when the task
39 // completes. Task_tokens are only manipulated by the main thread, so
40 // they do not themselves require any locking.
49 // A read/write token uses these methods.
64 add_writer(const Task
*);
67 remove_writer(const Task
*);
70 has_write_lock(const Task
*);
72 // A blocker token uses these methods.
77 // Returns true if block count drops to zero.
85 // It makes no sense to copy these.
86 Task_token(const Task_token
&);
87 Task_token
& operator=(const Task_token
&);
94 // In order to support tokens more reliably, we provide objects which
95 // handle them using RAII.
100 Task_read_token(Task_token
& token
)
102 { this->token_
.add_reader(); }
105 { this->token_
.remove_reader(); }
108 Task_read_token(const Task_read_token
&);
109 Task_read_token
& operator=(const Task_read_token
&);
114 class Task_write_token
117 Task_write_token(Task_token
& token
, const Task
* task
)
118 : token_(token
), task_(task
)
119 { this->token_
.add_writer(this->task_
); }
122 { this->token_
.remove_writer(this->task_
); }
125 Task_write_token(const Task_write_token
&);
126 Task_write_token
& operator=(const Task_write_token
&);
132 class Task_block_token
135 // The blocker count must be incremented when the task is created.
136 // This object is created when the task is run. When we unblock the
137 // last task, we notify the workqueue.
138 Task_block_token(Task_token
& token
, Workqueue
* workqueue
);
142 Task_block_token(const Task_block_token
&);
143 Task_block_token
& operator=(const Task_block_token
&);
146 Workqueue
* workqueue_
;
149 // An object which implements an RAII lock for any object which
150 // supports lock and unlock methods.
152 template<typename Obj
>
156 Task_lock_obj(Obj
& obj
)
158 { this->obj_
.lock(); }
161 { this->obj_
.unlock(); }
164 Task_lock_obj(const Task_lock_obj
&);
165 Task_lock_obj
& operator=(const Task_lock_obj
&);
170 // An abstract class used to lock Task_tokens using RAII. A typical
171 // implementation would simply have a set of members of type
172 // Task_read_token, Task_write_token, and Task_block_token.
180 virtual ~Task_locker()
184 // A version of Task_locker which may be used for a single read lock.
186 class Task_locker_read
: public Task_locker
189 Task_locker_read(Task_token
& token
)
194 Task_locker_read(const Task_locker_read
&);
195 Task_locker_read
& operator=(const Task_locker_read
&);
197 Task_read_token read_token_
;
200 // A version of Task_locker which may be used for a single write lock.
202 class Task_locker_write
: public Task_locker
205 Task_locker_write(Task_token
& token
, const Task
* task
)
206 : write_token_(token
, task
)
210 Task_locker_write(const Task_locker_write
&);
211 Task_locker_write
& operator=(const Task_locker_write
&);
213 Task_write_token write_token_
;
216 // A version of Task_locker which may be used for a single blocker
219 class Task_locker_block
: public Task_locker
222 Task_locker_block(Task_token
& token
, Workqueue
* workqueue
)
223 : block_token_(token
, workqueue
)
227 Task_locker_block(const Task_locker_block
&);
228 Task_locker_block
& operator=(const Task_locker_block
&);
230 Task_block_token block_token_
;
233 // A version of Task_locker which may be used to hold a lock on any
234 // object which supports lock() and unlock() methods.
236 template<typename Obj
>
237 class Task_locker_obj
: public Task_locker
240 Task_locker_obj(Obj
& obj
)
245 Task_locker_obj(const Task_locker_obj
&);
246 Task_locker_obj
& operator=(const Task_locker_obj
&);
248 Task_lock_obj
<Obj
> obj_lock_
;
251 // The superclass for tasks to be placed on the workqueue. Each
252 // specific task class will inherit from this one.
262 // Type returned by Is_runnable.
263 enum Is_runnable_type
267 // Task is waiting for a block to clear.
269 // Task is not waiting for a block, but is not runnable--i.e., is
270 // waiting for a lock.
274 // Return whether the task can be run now. This method is only
275 // called from the main thread.
276 virtual Is_runnable_type
277 is_runnable(Workqueue
*) = 0;
279 // Return a pointer to a Task_locker which locks all the resources
280 // required by the task. We delete the pointer when the task is
281 // complete. This method can return NULL if no locks are required.
282 // This method is only called from the main thread.
284 locks(Workqueue
*) = 0;
293 class Workqueue_runner
;
298 Workqueue(const General_options
&);
301 // Add a new task to the work queue.
305 // Process all the tasks on the work queue.
309 // A complete set of blocking tasks has completed.
314 // This class can not be copied.
315 Workqueue(const Workqueue
&);
316 Workqueue
& operator=(const Workqueue
&);
318 typedef std::list
<Task
*> Task_list
;
323 friend class Workqueue_runner
;
325 // Find a runnable task.
326 Task
* find_runnable(Task_list
&, bool*);
328 // Add a lock to the completed queue.
329 void completed(Task
*, Task_locker
*);
331 // Clear the completed queue.
332 bool clear_completed();
334 // How to run a task. Only accessed from main thread.
335 Workqueue_runner
* runner_
;
337 // Lock for access to tasks_ members.
339 // List of tasks to execute at each link level.
342 // Lock for access to completed_ and running_ members.
343 Lock completed_lock_
;
344 // List of Task_locker objects for main thread to free.
345 std::list
<Task_locker
*> completed_
;
346 // Number of tasks currently running.
348 // Condition variable signalled when a new entry is added to completed_.
349 Condvar completed_condvar_
;
351 // Number of blocker tokens which were fully cleared. Only accessed
353 int cleared_blockers_
;
356 } // End namespace gold.
358 #endif // !defined(GOLD_WORKQUEUE_H)