1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
// workqueue.cc -- the workqueue for gold
// Copyright 2006, 2007 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.
#include "gold.h"
#include "debug.h"
#include "workqueue.h"
#include "workqueue-internal.h"
namespace gold
{
// Task_token methods.
Task_token::Task_token()
: is_blocker_(false), readers_(0), writer_(NULL)
{
}
Task_token::~Task_token()
{
gold_assert(this->readers_ == 0 && this->writer_ == NULL);
}
bool
Task_token::is_readable() const
{
gold_assert(!this->is_blocker_);
return this->writer_ == NULL;
}
void
Task_token::add_reader()
{
gold_assert(!this->is_blocker_);
gold_assert(this->is_readable());
++this->readers_;
}
void
Task_token::remove_reader()
{
gold_assert(!this->is_blocker_);
gold_assert(this->readers_ > 0);
--this->readers_;
}
bool
Task_token::is_writable() const
{
gold_assert(!this->is_blocker_);
return this->writer_ == NULL && this->readers_ == 0;
}
void
Task_token::add_writer(const Task* t)
{
gold_assert(!this->is_blocker_);
gold_assert(this->is_writable());
this->writer_ = t;
}
void
Task_token::remove_writer(const Task* t)
{
gold_assert(!this->is_blocker_);
gold_assert(this->writer_ == t);
this->writer_ = NULL;
}
bool
Task_token::has_write_lock(const Task* t)
{
gold_assert(!this->is_blocker_);
return this->writer_ == t;
}
// For blockers, we just use the readers_ field.
void
Task_token::add_blocker()
{
if (this->readers_ == 0 && this->writer_ == NULL)
this->is_blocker_ = true;
else
gold_assert(this->is_blocker_);
++this->readers_;
}
bool
Task_token::remove_blocker()
{
gold_assert(this->is_blocker_ && this->readers_ > 0);
--this->readers_;
return this->readers_ == 0;
}
bool
Task_token::is_blocked() const
{
gold_assert(this->is_blocker_
|| (this->readers_ == 0 && this->writer_ == NULL));
return this->readers_ > 0;
}
// The Task_block_token class.
Task_block_token::Task_block_token(Task_token& token, Workqueue* workqueue)
: token_(token), workqueue_(workqueue)
{
// We must increment the block count when the task is created and
// put on the queue. This object is created when the task is run,
// so we don't increment the block count here.
gold_assert(this->token_.is_blocked());
}
Task_block_token::~Task_block_token()
{
if (this->token_.remove_blocker())
{
// Tell the workqueue that a blocker was cleared. This is
// always called in the main thread, so no locking is required.
this->workqueue_->cleared_blocker();
}
}
// The simple single-threaded implementation of Workqueue_runner.
class Workqueue_runner_single : public Workqueue_runner
{
public:
Workqueue_runner_single(Workqueue* workqueue)
: Workqueue_runner(workqueue)
{ }
~Workqueue_runner_single()
{ }
void
run(Task*, Task_locker*);
void
set_thread_count(int);
};
void
Workqueue_runner_single::run(Task* t, Task_locker* tl)
{
t->run(this->get_workqueue());
this->completed(t, tl);
}
void
Workqueue_runner_single::set_thread_count(int thread_count)
{
gold_assert(thread_count > 0);
}
// Workqueue methods.
Workqueue::Workqueue(const General_options& options)
: tasks_lock_(),
first_tasks_(),
tasks_(),
completed_lock_(),
completed_(),
running_(0),
queued_(0),
completed_condvar_(this->completed_lock_),
cleared_blockers_(0),
desired_thread_count_(1)
{
bool threads = options.threads();
#ifndef ENABLE_THREADS
threads = false;
#endif
if (!threads)
this->runner_ = new Workqueue_runner_single(this);
else
{
#ifdef ENABLE_THREADS
this->runner_ = new Workqueue_runner_threadpool(this);
#else
gold_unreachable();
#endif
}
}
Workqueue::~Workqueue()
{
gold_assert(this->first_tasks_.empty());
gold_assert(this->tasks_.empty());
gold_assert(this->completed_.empty());
gold_assert(this->running_ == 0);
}
// Add a task to the queue.
void
Workqueue::queue(Task* t)
{
{
Hold_lock hl(this->tasks_lock_);
this->tasks_.push_back(t);
}
{
Hold_lock hl(this->completed_lock_);
++this->queued_;
}
}
// Add a task to the front of the queue.
void
Workqueue::queue_front(Task* t)
{
{
Hold_lock hl(this->tasks_lock_);
this->first_tasks_.push_front(t);
}
{
Hold_lock hl(this->completed_lock_);
++this->queued_;
}
}
// Clear the list of completed tasks. Return whether we cleared
// anything. The completed_lock_ must be held when this is called.
bool
Workqueue::clear_completed()
{
if (this->completed_.empty())
return false;
do
{
delete this->completed_.front();
this->completed_.pop_front();
}
while (!this->completed_.empty());
return true;
}
// Find a runnable task in TASKS, which is non-empty. Return NULL if
// none could be found. The tasks_lock_ must be held when this is
// called. Sets ALL_BLOCKED if all non-runnable tasks are waiting on
// a blocker.
Task*
Workqueue::find_runnable(Task_list* tasks, bool* all_blocked)
{
Task* tlast = tasks->back();
*all_blocked = true;
Task* t;
do
{
t = tasks->front();
tasks->pop_front();
Task::Is_runnable_type is_runnable = t->is_runnable(this);
if (is_runnable == Task::IS_RUNNABLE)
{
{
Hold_lock hl(this->completed_lock_);
--this->queued_;
}
return t;
}
if (is_runnable != Task::IS_BLOCKED)
*all_blocked = false;
tasks->push_back(t);
}
while (t != tlast);
// We couldn't find any runnable task.
return NULL;
}
// Process all the tasks on the workqueue. This is the main loop in
// the linker. Note that as we process tasks, new tasks will be
// added.
void
Workqueue::process()
{
while (true)
{
Task* t;
bool empty;
bool all_blocked;
// Don't start more tasks than desired.
{
Hold_lock hl(this->completed_lock_);
this->clear_completed();
while (this->running_ >= this->desired_thread_count_)
{
this->completed_condvar_.wait();
this->clear_completed();
}
}
{
Hold_lock hl(this->tasks_lock_);
bool first_empty;
bool all_blocked_first;
if (this->first_tasks_.empty())
{
t = NULL;
empty = true;
first_empty = true;
all_blocked_first = false;
}
else
{
t = this->find_runnable(&this->first_tasks_, &all_blocked_first);
empty = false;
first_empty = false;
}
if (t == NULL)
{
if (this->tasks_.empty())
all_blocked = false;
else
{
t = this->find_runnable(&this->tasks_, &all_blocked);
if (!first_empty && !all_blocked_first)
all_blocked = false;
empty = false;
}
}
}
// If T != NULL, it is a task we can run.
// If T == NULL && empty, then there are no tasks waiting to
// be run.
// If T == NULL && !empty, then there tasks waiting to be
// run, but they are waiting for something to unlock.
if (t != NULL)
this->run(t);
else if (!empty)
{
{
Hold_lock hl(this->completed_lock_);
// There must be something for us to wait for, or we won't
// be able to make progress.
gold_assert(this->running_ > 0 || !this->completed_.empty());
if (all_blocked)
{
this->cleared_blockers_ = 0;
int queued = this->queued_;
this->clear_completed();
while (this->cleared_blockers_ == 0
&& queued == this->queued_)
{
if (this->running_ <= 0)
{
this->show_queued_tasks();
gold_unreachable();
}
this->completed_condvar_.wait();
this->clear_completed();
}
}
else
{
if (this->running_ > 0)
{
// Wait for a task to finish.
this->completed_condvar_.wait();
}
this->clear_completed();
}
}
}
else
{
{
Hold_lock hl(this->completed_lock_);
// If there are no running tasks, then we are done.
if (this->running_ == 0)
{
this->clear_completed();
return;
}
// Wait for a task to finish. Then we have to loop around
// again in case it added any new tasks before finishing.
this->completed_condvar_.wait();
this->clear_completed();
}
}
}
}
// Run a task. This is always called in the main thread.
void
Workqueue::run(Task* t)
{
gold_debug(DEBUG_TASK, "starting task %s", t->name().c_str());
{
Hold_lock hl(this->completed_lock_);
++this->running_;
}
this->runner_->run(t, t->locks(this));
}
// This is called when a task is completed to put the locks on the
// list to be released. We use a list because we only want the locks
// to be released in the main thread.
void
Workqueue::completed(Task* t, Task_locker* tl)
{
gold_debug(DEBUG_TASK, "completed task %s", t->name().c_str());
{
Hold_lock hl(this->completed_lock_);
gold_assert(this->running_ > 0);
--this->running_;
this->completed_.push_back(tl);
this->completed_condvar_.signal();
}
delete t;
}
// This is called when the last task for a blocker has completed.
// This is always called in the main thread.
void
Workqueue::cleared_blocker()
{
++this->cleared_blockers_;
}
// Set the number of threads to use for the workqueue, if we are using
// threads.
void
Workqueue::set_thread_count(int threads)
{
gold_assert(threads > 0);
this->desired_thread_count_ = threads;
this->runner_->set_thread_count(threads);
}
// Dump the list of queued tasks and their current state, for
// debugging purposes.
void
Workqueue::show_queued_tasks()
{
fprintf(stderr, _("gold task queue:\n"));
Hold_lock hl(this->tasks_lock_);
for (Task_list::const_iterator p = this->tasks_.begin();
p != this->tasks_.end();
++p)
{
fprintf(stderr, " %s ", (*p)->name().c_str());
switch ((*p)->is_runnable(this))
{
case Task::IS_RUNNABLE:
fprintf(stderr, "runnable");
break;
case Task::IS_BLOCKED:
fprintf(stderr, "blocked");
break;
case Task::IS_LOCKED:
fprintf(stderr, "locked");
break;
default:
gold_unreachable();
}
putc('\n', stderr);
}
}
} // End namespace gold.
|