1 // gold-threads.cc -- thread support for gold
3 // Copyright 2006, 2007 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.
31 #include "parameters.h"
32 #include "gold-threads.h"
37 class Condvar_impl_nothreads;
39 // The non-threaded version of Lock_impl.
41 class Lock_impl_nothreads : public Lock_impl
48 ~Lock_impl_nothreads()
49 { gold_assert(!this->acquired_); }
54 gold_assert(!this->acquired_);
55 this->acquired_ = true;
61 gold_assert(this->acquired_);
62 this->acquired_ = false;
66 friend class Condvar_impl_nothreads;
73 class Condvar_impl_threads;
75 // The threaded version of Lock_impl.
77 class Lock_impl_threads : public Lock_impl
88 // This class can not be copied.
89 Lock_impl_threads(const Lock_impl_threads&);
90 Lock_impl_threads& operator=(const Lock_impl_threads&);
92 friend class Condvar_impl_threads;
94 pthread_mutex_t mutex_;
97 Lock_impl_threads::Lock_impl_threads()
99 pthread_mutexattr_t attr;
100 int err = pthread_mutexattr_init(&attr);
102 gold_fatal(_("pthead_mutextattr_init failed: %s"), strerror(err));
103 #ifdef PTHREAD_MUTEXT_ADAPTIVE_NP
104 err = pthread_mutextattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);
106 gold_fatal(_("pthread_mutextattr_settype failed: %s"), strerror(err));
109 err = pthread_mutex_init (&this->mutex_, &attr);
111 gold_fatal(_("pthread_mutex_init failed: %s"), strerror(err));
113 err = pthread_mutexattr_destroy(&attr);
115 gold_fatal(_("pthread_mutexattr_destroy failed: %s"), strerror(err));
118 Lock_impl_threads::~Lock_impl_threads()
120 int err = pthread_mutex_destroy(&this->mutex_);
122 gold_fatal(_("pthread_mutex_destroy failed: %s"), strerror(err));
126 Lock_impl_threads::acquire()
128 int err = pthread_mutex_lock(&this->mutex_);
130 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(err));
134 Lock_impl_threads::release()
136 int err = pthread_mutex_unlock(&this->mutex_);
138 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(err));
141 #endif // defined(ENABLE_THREADS)
147 if (!parameters->threads())
148 this->lock_ = new Lock_impl_nothreads;
151 #ifdef ENABLE_THREADS
152 this->lock_ = new Lock_impl_threads;
164 // The non-threaded version of Condvar_impl.
166 class Condvar_impl_nothreads : public Condvar_impl
169 Condvar_impl_nothreads()
172 ~Condvar_impl_nothreads()
177 { gold_assert(static_cast<Lock_impl_nothreads*>(li)->acquired_); }
188 #ifdef ENABLE_THREADS
190 // The threaded version of Condvar_impl.
192 class Condvar_impl_threads : public Condvar_impl
195 Condvar_impl_threads();
196 ~Condvar_impl_threads();
208 // This class can not be copied.
209 Condvar_impl_threads(const Condvar_impl_threads&);
210 Condvar_impl_threads& operator=(const Condvar_impl_threads&);
212 pthread_cond_t cond_;
215 Condvar_impl_threads::Condvar_impl_threads()
217 int err = pthread_cond_init(&this->cond_, NULL);
219 gold_fatal(_("pthread_cond_init failed: %s"), strerror(err));
222 Condvar_impl_threads::~Condvar_impl_threads()
224 int err = pthread_cond_destroy(&this->cond_);
226 gold_fatal(_("pthread_cond_destroy failed: %s"), strerror(err));
230 Condvar_impl_threads::wait(Lock_impl* li)
232 Lock_impl_threads* lit = static_cast<Lock_impl_threads*>(li);
233 int err = pthread_cond_wait(&this->cond_, &lit->mutex_);
235 gold_fatal(_("pthread_cond_wait failed: %s"), strerror(err));
239 Condvar_impl_threads::signal()
241 int err = pthread_cond_signal(&this->cond_);
243 gold_fatal(_("pthread_cond_signal failed: %s"), strerror(err));
247 Condvar_impl_threads::broadcast()
249 int err = pthread_cond_broadcast(&this->cond_);
251 gold_fatal(_("pthread_cond_broadcast failed: %s"), strerror(err));
254 #endif // defined(ENABLE_THREADS)
256 // Methods for Condvar class.
258 Condvar::Condvar(Lock& lock)
261 if (!parameters->threads())
262 this->condvar_ = new Condvar_impl_nothreads;
265 #ifdef ENABLE_THREADS
266 this->condvar_ = new Condvar_impl_threads;
275 delete this->condvar_;
278 } // End namespace gold.