aboutsummaryrefslogtreecommitdiff
path: root/gold/workqueue.cc
blob: 716f93db7851437a932e04f6dcb4f63286d001b6 (plain)
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
// workqueue.cc -- the workqueue for gold

#include "gold.h"

#include <cassert>

#include "workqueue.h"

namespace gold
{

// Task_token methods.

Task_token::Task_token()
  : is_blocker_(false), readers_(0), writer_(NULL)
{
}

Task_token::~Task_token()
{
  assert(this->readers_ == 0 && this->writer_ == NULL);
}

bool
Task_token::is_readable() const
{
  assert(!this->is_blocker_);
  return this->writer_ == NULL;
}

void
Task_token::add_reader()
{
  assert(!this->is_blocker_);
  assert(this->is_readable());
  ++this->readers_;
}

void
Task_token::remove_reader()
{
  assert(!this->is_blocker_);
  assert(this->readers_ > 0);
  --this->readers_;
}

bool
Task_token::is_writable() const
{
  assert(!this->is_blocker_);
  return this->writer_ == NULL && this->readers_ == 0;
}

void
Task_token::add_writer(const Task* t)
{
  assert(!this->is_blocker_);
  assert(this->is_writable());
  this->writer_ = t;
}

void
Task_token::remove_writer(const Task* t)
{
  assert(!this->is_blocker_);
  assert(this->writer_ == t);
  this->writer_ = NULL;
}

bool
Task_token::has_write_lock(const Task* t)
{
  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
    assert(this->is_blocker_);
  ++this->readers_;
}

bool
Task_token::remove_blocker()
{
  assert(this->is_blocker_ && this->readers_ > 0);
  --this->readers_;
  return this->readers_ == 0;
}

bool
Task_token::is_blocked() const
{
  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.
  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 Workqueue_runner abstract class.

class Workqueue_runner
{
 public:
  Workqueue_runner(Workqueue* workqueue)
    : workqueue_(workqueue)
  { }
  virtual ~Workqueue_runner()
  { }

  // Run a task.  This is always called in the main thread.
  virtual void run(Task*, Task_locker*) = 0;

 protected:
  // This is called by an implementation when a task is completed.
  void completed(Task* t, Task_locker* tl)
  { this->workqueue_->completed(t, tl); }

  Workqueue* get_workqueue() const
  { return this->workqueue_; }

 private:
  Workqueue* workqueue_;
};

// 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
Workqueue_runner_single::run(Task* t, Task_locker* tl)
{
  t->run(this->get_workqueue());
  this->completed(t, tl);
}

// Workqueue methods.

Workqueue::Workqueue(const General_options&)
  : tasks_lock_(),
    tasks_(),
    completed_lock_(),
    completed_(),
    running_(0),
    completed_condvar_(this->completed_lock_),
    cleared_blockers_(0)
{
  // At some point we will select the specific implementation of
  // Workqueue_runner to use based on the command line options.
  this->runner_ = new Workqueue_runner_single(this);
}

Workqueue::~Workqueue()
{
  assert(this->tasks_.empty());
  assert(this->completed_.empty());
  assert(this->running_ == 0);
}

void
Workqueue::queue(Task* t)
{
  Hold_lock hl(this->tasks_lock_);
  this->tasks_.push_back(t);
}

// 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;
  while (true)
    {
      Task* t = tasks.front();
      tasks.pop_front();

      Task::Is_runnable_type is_runnable = t->is_runnable(this);
      if (is_runnable == Task::IS_RUNNABLE)
	return t;

      if (is_runnable != Task::IS_BLOCKED)
	*all_blocked = false;

      tasks.push_back(t);

      if (t == tlast)
	{
	  // We couldn't find any runnable task.  If there are any
	  // completed tasks, free their locks and try again.

	  {
	    Hold_lock hl2(this->completed_lock_);

	    if (!this->clear_completed())
	      {
		// There had better be some tasks running, or we will
		// never find a runnable task.
		assert(this->running_ > 0);

		// We couldn't find any runnable tasks, and we
		// couldn't release any locks.
		return NULL;
	      }
	  }

	  // We're going around again, so recompute ALL_BLOCKED.
	  *all_blocked = true;
	}
    }
}

// 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;

      {
	Hold_lock hl(this->tasks_lock_);

	if (this->tasks_.empty())
	  {
	    t = NULL;
	    empty = true;
	    all_blocked = false;
	  }
	else
	  {
	    t = this->find_runnable(this->tasks_, &all_blocked);
	    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 at this level.
      // If T == NULL && !empty, then there tasks waiting to be
      // run at this level, 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.
	    assert(this->running_ > 0 || !this->completed_.empty());

	    if (all_blocked)
	      {
		this->cleared_blockers_ = 0;
		this->clear_completed();
		while (this->cleared_blockers_ == 0)
		  {
		    assert(this->running_ > 0);
		    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)
{
  ++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)
{
  {
    Hold_lock hl(this->completed_lock_);
    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_;
}

} // End namespace gold.