summaryrefslogtreecommitdiff
path: root/gold/workqueue-threads.cc
blob: 3c30b325399cb6f8a22cb03ef07bff6d27e7fde0 (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
// workqueue-threads.cc -- the threaded workqueue for gold

// Copyright 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.

// This file holds the workqueue implementation which may be used when
// using threads.

#include "gold.h"

#ifdef ENABLE_THREADS

#include <cstring>
#include <pthread.h>

#include "debug.h"
#include "gold-threads.h"
#include "workqueue.h"
#include "workqueue-internal.h"

namespace gold
{

// Class Workqueue_thread represents a single thread.  Creating an
// instance of this spawns a new thread.

class Workqueue_thread
{
 public:
  Workqueue_thread(Workqueue_runner_threadpool*);

  ~Workqueue_thread();

 private:
  // This class can not be copied.
  Workqueue_thread(const Workqueue_thread&);
  Workqueue_thread& operator=(const Workqueue_thread&);

  // Check for error from a pthread function.
  void
  check(const char* function, int err) const;

  // A function to pass to pthread_create.  This is called with a
  // pointer to an instance of this object.
  static void*
  thread_body(void*);

  // The main loop of the thread.
  void
  run();

  // A pointer to the threadpool that this thread is part of.
  Workqueue_runner_threadpool* threadpool_;
  // The thread ID.
  pthread_t tid_;
};

// Create the thread in the constructor.

Workqueue_thread::Workqueue_thread(Workqueue_runner_threadpool* threadpool)
  : threadpool_(threadpool)
{
  pthread_attr_t attr;
  int err = pthread_attr_init(&attr);
  this->check("pthread_attr_init", err);

  err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  this->check("pthread_attr_setdetachstate", err);

  err = pthread_create(&this->tid_, &attr, &Workqueue_thread::thread_body,
		       reinterpret_cast<void*>(this));
  this->check("pthread_create", err);

  err = pthread_attr_destroy(&attr);
  this->check("pthread_attr_destroy", err);
}

// The destructor will be called when the thread is exiting.

Workqueue_thread::~Workqueue_thread()
{
}

// Check for an error.

void
Workqueue_thread::check(const char* function, int err) const
{
  if (err != 0)
    gold_fatal(_("%s failed: %s"), function, strerror(err));
}

// Passed to pthread_create.

extern "C"
void*
Workqueue_thread::thread_body(void* arg)
{
  Workqueue_thread* pwt = reinterpret_cast<Workqueue_thread*>(arg);
  pwt->run();

  // Delete the thread object as we exit.
  delete pwt;

  return NULL;
}

// This is the main loop of a worker thread.  It picks up a new Task
// and runs it.

void
Workqueue_thread::run()
{
  Workqueue_runner_threadpool* threadpool = this->threadpool_;
  Workqueue* workqueue = threadpool->get_workqueue();

  while (true)
    {
      Task* t;
      Task_locker* tl;
      if (!threadpool->get_next(&t, &tl))
	return;

      gold_debug(DEBUG_TASK, "running   task %s", t->name().c_str());

      t->run(workqueue);
      threadpool->thread_completed(t, tl);
    }
}

// Class Workqueue_runner_threadpool.

// Constructor.

Workqueue_runner_threadpool::Workqueue_runner_threadpool(Workqueue* workqueue)
  : Workqueue_runner(workqueue),
    desired_thread_count_(0),
    lock_(),
    actual_thread_count_(0),
    running_thread_count_(0),
    task_queue_(),
    task_queue_condvar_(this->lock_)
{
}

// Destructor.

Workqueue_runner_threadpool::~Workqueue_runner_threadpool()
{
  // Tell the threads to exit.
  Hold_lock hl(this->lock_);
  this->desired_thread_count_ = 0;
  this->task_queue_condvar_.broadcast();
}

// Run a task.  This doesn't actually run the task: it pushes it on
// the queue of tasks to run.  This is always called in the main
// thread.

void
Workqueue_runner_threadpool::run(Task* t, Task_locker* tl)
{
  Hold_lock hl(this->lock_);

  // This is where we create threads as needed, subject to the limit
  // of the desired thread count.
  gold_assert(this->desired_thread_count_ > 0);
  gold_assert(this->actual_thread_count_ >= this->running_thread_count_);
  if (this->actual_thread_count_ == this->running_thread_count_
      && this->actual_thread_count_ < this->desired_thread_count_)
    {
      // Note that threads delete themselves when they exit, so we
      // don't keep pointers to them.
      new Workqueue_thread(this);
      ++this->actual_thread_count_;
    }

  this->task_queue_.push(std::make_pair(t, tl));
  this->task_queue_condvar_.signal();
}

// Set the thread count.  This is only called in the main thread, and
// is only called when there are no threads running.

void
Workqueue_runner_threadpool::set_thread_count(int thread_count)
{
  gold_assert(this->running_thread_count_ <= 1);
  gold_assert(thread_count > 0);
  this->desired_thread_count_ = thread_count;
}

// Get the next task to run.  This is always called by an instance of
// Workqueue_thread, and is never called in the main thread.  It
// returns false if the calling thread should exit.

bool
Workqueue_runner_threadpool::get_next(Task** pt, Task_locker** ptl)
{
  Hold_lock hl(this->lock_);

  // This is where we destroy threads, by telling them to exit.
  gold_assert(this->actual_thread_count_ > this->running_thread_count_);
  if (this->actual_thread_count_ > this->desired_thread_count_)
    {
      --this->actual_thread_count_;
      return false;
    }

  while (this->task_queue_.empty() && this->desired_thread_count_ > 0)
    {
      // Wait for a new task to become available.
      this->task_queue_condvar_.wait();
    }

  // Check whether we are exiting.
  if (this->desired_thread_count_ == 0)
    {
      gold_assert(this->actual_thread_count_ > 0);
      --this->actual_thread_count_;
      return false;
    }

  *pt = this->task_queue_.front().first;
  *ptl = this->task_queue_.front().second;
  this->task_queue_.pop();

  ++this->running_thread_count_;

  return true;
}

// This is called when a thread completes its task.

void
Workqueue_runner_threadpool::thread_completed(Task* t, Task_locker* tl)
{
  {
    Hold_lock hl(this->lock_);
    gold_assert(this->actual_thread_count_ > 0);
    gold_assert(this->running_thread_count_ > 0);
    --this->running_thread_count_;
  }

  this->completed(t, tl);
}

} // End namespace gold.

#endif // defined(ENABLE_THREADS)