2009-06-12 Tristan Gingold <gingold@adacore.com>
[binutils.git] / gold / gold-threads.cc
blobb99cf27584746d906f82a465da1e5bb100c0b30b
1 // gold-threads.cc -- thread support for gold
3 // Copyright 2006, 2007, 2008 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 #include "gold.h"
25 #include <cstring>
27 #ifdef ENABLE_THREADS
28 #include <pthread.h>
29 #endif
31 #include "options.h"
32 #include "parameters.h"
33 #include "gold-threads.h"
35 namespace gold
38 class Condvar_impl_nothreads;
40 // The non-threaded version of Lock_impl.
42 class Lock_impl_nothreads : public Lock_impl
44 public:
45 Lock_impl_nothreads()
46 : acquired_(false)
47 { }
49 ~Lock_impl_nothreads()
50 { gold_assert(!this->acquired_); }
52 void
53 acquire()
55 gold_assert(!this->acquired_);
56 this->acquired_ = true;
59 void
60 release()
62 gold_assert(this->acquired_);
63 this->acquired_ = false;
66 private:
67 friend class Condvar_impl_nothreads;
69 bool acquired_;
72 #ifdef ENABLE_THREADS
74 class Condvar_impl_threads;
76 // The threaded version of Lock_impl.
78 class Lock_impl_threads : public Lock_impl
80 public:
81 Lock_impl_threads();
82 ~Lock_impl_threads();
84 void acquire();
86 void release();
88 private:
89 // This class can not be copied.
90 Lock_impl_threads(const Lock_impl_threads&);
91 Lock_impl_threads& operator=(const Lock_impl_threads&);
93 friend class Condvar_impl_threads;
95 pthread_mutex_t mutex_;
98 Lock_impl_threads::Lock_impl_threads()
100 pthread_mutexattr_t attr;
101 int err = pthread_mutexattr_init(&attr);
102 if (err != 0)
103 gold_fatal(_("pthead_mutextattr_init failed: %s"), strerror(err));
104 #ifdef PTHREAD_MUTEXT_ADAPTIVE_NP
105 err = pthread_mutextattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
106 if (err != 0)
107 gold_fatal(_("pthread_mutextattr_settype failed: %s"), strerror(err));
108 #endif
110 err = pthread_mutex_init (&this->mutex_, &attr);
111 if (err != 0)
112 gold_fatal(_("pthread_mutex_init failed: %s"), strerror(err));
114 err = pthread_mutexattr_destroy(&attr);
115 if (err != 0)
116 gold_fatal(_("pthread_mutexattr_destroy failed: %s"), strerror(err));
119 Lock_impl_threads::~Lock_impl_threads()
121 int err = pthread_mutex_destroy(&this->mutex_);
122 if (err != 0)
123 gold_fatal(_("pthread_mutex_destroy failed: %s"), strerror(err));
126 void
127 Lock_impl_threads::acquire()
129 int err = pthread_mutex_lock(&this->mutex_);
130 if (err != 0)
131 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(err));
134 void
135 Lock_impl_threads::release()
137 int err = pthread_mutex_unlock(&this->mutex_);
138 if (err != 0)
139 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(err));
142 #endif // defined(ENABLE_THREADS)
144 // Class Lock.
146 Lock::Lock()
148 if (!parameters->options().threads())
149 this->lock_ = new Lock_impl_nothreads;
150 else
152 #ifdef ENABLE_THREADS
153 this->lock_ = new Lock_impl_threads;
154 #else
155 gold_unreachable();
156 #endif
160 Lock::~Lock()
162 delete this->lock_;
165 // The non-threaded version of Condvar_impl.
167 class Condvar_impl_nothreads : public Condvar_impl
169 public:
170 Condvar_impl_nothreads()
173 ~Condvar_impl_nothreads()
176 void
177 wait(Lock_impl* li)
178 { gold_assert(static_cast<Lock_impl_nothreads*>(li)->acquired_); }
180 void
181 signal()
184 void
185 broadcast()
189 #ifdef ENABLE_THREADS
191 // The threaded version of Condvar_impl.
193 class Condvar_impl_threads : public Condvar_impl
195 public:
196 Condvar_impl_threads();
197 ~Condvar_impl_threads();
199 void
200 wait(Lock_impl*);
202 void
203 signal();
205 void
206 broadcast();
208 private:
209 // This class can not be copied.
210 Condvar_impl_threads(const Condvar_impl_threads&);
211 Condvar_impl_threads& operator=(const Condvar_impl_threads&);
213 pthread_cond_t cond_;
216 Condvar_impl_threads::Condvar_impl_threads()
218 int err = pthread_cond_init(&this->cond_, NULL);
219 if (err != 0)
220 gold_fatal(_("pthread_cond_init failed: %s"), strerror(err));
223 Condvar_impl_threads::~Condvar_impl_threads()
225 int err = pthread_cond_destroy(&this->cond_);
226 if (err != 0)
227 gold_fatal(_("pthread_cond_destroy failed: %s"), strerror(err));
230 void
231 Condvar_impl_threads::wait(Lock_impl* li)
233 Lock_impl_threads* lit = static_cast<Lock_impl_threads*>(li);
234 int err = pthread_cond_wait(&this->cond_, &lit->mutex_);
235 if (err != 0)
236 gold_fatal(_("pthread_cond_wait failed: %s"), strerror(err));
239 void
240 Condvar_impl_threads::signal()
242 int err = pthread_cond_signal(&this->cond_);
243 if (err != 0)
244 gold_fatal(_("pthread_cond_signal failed: %s"), strerror(err));
247 void
248 Condvar_impl_threads::broadcast()
250 int err = pthread_cond_broadcast(&this->cond_);
251 if (err != 0)
252 gold_fatal(_("pthread_cond_broadcast failed: %s"), strerror(err));
255 #endif // defined(ENABLE_THREADS)
257 // Methods for Condvar class.
259 Condvar::Condvar(Lock& lock)
260 : lock_(lock)
262 if (!parameters->options().threads())
263 this->condvar_ = new Condvar_impl_nothreads;
264 else
266 #ifdef ENABLE_THREADS
267 this->condvar_ = new Condvar_impl_threads;
268 #else
269 gold_unreachable();
270 #endif
274 Condvar::~Condvar()
276 delete this->condvar_;
279 #ifdef ENABLE_THREADS
281 // Class Initialize_lock_once. This exists to hold a pthread_once_t
282 // structure for Initialize_lock.
284 class Initialize_lock_once
286 public:
287 Initialize_lock_once()
288 : once_(PTHREAD_ONCE_INIT)
291 // Return a pointer to the pthread_once_t variable.
292 pthread_once_t*
293 once_control()
294 { return &this->once_; }
296 private:
297 pthread_once_t once_;
300 #endif // !defined(ENABLE_THREADS)
302 #ifdef ENABLE_THREADS
304 // A single lock which controls access to initialize_lock_pointer.
305 // This is used because we can't pass parameters to functions passed
306 // to pthread_once.
308 static pthread_mutex_t initialize_lock_control = PTHREAD_MUTEX_INITIALIZER;
310 // A pointer to a pointer to the lock which we need to initialize
311 // once. Access to this is controlled by initialize_lock_control.
313 static Lock** initialize_lock_pointer;
315 // A routine passed to pthread_once which initializes the lock which
316 // initialize_lock_pointer points to.
318 extern "C"
321 static void
322 initialize_lock_once()
324 *initialize_lock_pointer = new Lock();
329 #endif // !defined(ENABLE_THREADS)
331 // Class Initialize_lock.
333 Initialize_lock::Initialize_lock(Lock** pplock)
334 : pplock_(pplock)
336 #ifndef ENABLE_THREADS
337 this->once_ = NULL;
338 #else
339 this->once_ = new Initialize_lock_once();
340 #endif
343 // Initialize the lock.
345 bool
346 Initialize_lock::initialize()
348 // If the lock has already been initialized, we don't need to do
349 // anything. Note that this assumes that the pointer value will be
350 // set completely or not at all. I hope this is always safe. We
351 // want to do this for efficiency.
352 if (*this->pplock_ != NULL)
353 return true;
355 // We can't initialize the lock until we have read the options.
356 if (!parameters->options_valid())
357 return false;
359 // If the user did not use --threads, then we can initialize
360 // directly.
361 if (!parameters->options().threads())
363 *this->pplock_ = new Lock();
364 return true;
367 #ifndef ENABLE_THREADS
369 // If there is no threads support, we don't need to use
370 // pthread_once.
371 *this->pplock_ = new Lock();
373 #else // !defined(ENABLE_THREADS)
375 // Since we can't pass parameters to routines called by
376 // pthread_once, we use a static variable: initialize_lock_pointer.
377 // That in turns means that we need to use a mutex to control access
378 // to initialize_lock_pointer.
380 int err = pthread_mutex_lock(&initialize_lock_control);
381 if (err != 0)
382 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(err));
384 initialize_lock_pointer = this->pplock_;
386 err = pthread_once(this->once_->once_control(), initialize_lock_once);
387 if (err != 0)
388 gold_fatal(_("pthread_once failed: %s"), strerror(err));
390 initialize_lock_pointer = NULL;
392 err = pthread_mutex_unlock(&initialize_lock_control);
393 if (err != 0)
394 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(err));
396 gold_assert(*this->pplock_ != NULL);
398 #endif // !defined(ENABLE_THREADS)
400 return true;
403 } // End namespace gold.