daily update
[binutils.git] / gold / gold-threads.cc
blob4ebbdd84960804a208a7a97654ca03f2a7fae34b
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.
23 #include "gold.h"
25 #include <cstring>
27 #ifdef ENABLE_THREADS
28 #include <pthread.h>
29 #endif
31 #include "parameters.h"
32 #include "gold-threads.h"
34 namespace gold
37 class Condvar_impl_nothreads;
39 // The non-threaded version of Lock_impl.
41 class Lock_impl_nothreads : public Lock_impl
43 public:
44 Lock_impl_nothreads()
45 : acquired_(false)
46 { }
48 ~Lock_impl_nothreads()
49 { gold_assert(!this->acquired_); }
51 void
52 acquire()
54 gold_assert(!this->acquired_);
55 this->acquired_ = true;
58 void
59 release()
61 gold_assert(this->acquired_);
62 this->acquired_ = false;
65 private:
66 friend class Condvar_impl_nothreads;
68 bool acquired_;
71 #ifdef ENABLE_THREADS
73 class Condvar_impl_threads;
75 // The threaded version of Lock_impl.
77 class Lock_impl_threads : public Lock_impl
79 public:
80 Lock_impl_threads();
81 ~Lock_impl_threads();
83 void acquire();
85 void release();
87 private:
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);
101 if (err != 0)
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);
105 if (err != 0)
106 gold_fatal(_("pthread_mutextattr_settype failed: %s"), strerror(err));
107 #endif
109 err = pthread_mutex_init (&this->mutex_, &attr);
110 if (err != 0)
111 gold_fatal(_("pthread_mutex_init failed: %s"), strerror(err));
113 err = pthread_mutexattr_destroy(&attr);
114 if (err != 0)
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_);
121 if (err != 0)
122 gold_fatal(_("pthread_mutex_destroy failed: %s"), strerror(err));
125 void
126 Lock_impl_threads::acquire()
128 int err = pthread_mutex_lock(&this->mutex_);
129 if (err != 0)
130 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(err));
133 void
134 Lock_impl_threads::release()
136 int err = pthread_mutex_unlock(&this->mutex_);
137 if (err != 0)
138 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(err));
141 #endif // defined(ENABLE_THREADS)
143 // Class Lock.
145 Lock::Lock()
147 if (!parameters->threads())
148 this->lock_ = new Lock_impl_nothreads;
149 else
151 #ifdef ENABLE_THREADS
152 this->lock_ = new Lock_impl_threads;
153 #else
154 gold_unreachable();
155 #endif
159 Lock::~Lock()
161 delete this->lock_;
164 // The non-threaded version of Condvar_impl.
166 class Condvar_impl_nothreads : public Condvar_impl
168 public:
169 Condvar_impl_nothreads()
172 ~Condvar_impl_nothreads()
175 void
176 wait(Lock_impl* li)
177 { gold_assert(static_cast<Lock_impl_nothreads*>(li)->acquired_); }
179 void
180 signal()
183 void
184 broadcast()
188 #ifdef ENABLE_THREADS
190 // The threaded version of Condvar_impl.
192 class Condvar_impl_threads : public Condvar_impl
194 public:
195 Condvar_impl_threads();
196 ~Condvar_impl_threads();
198 void
199 wait(Lock_impl*);
201 void
202 signal();
204 void
205 broadcast();
207 private:
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);
218 if (err != 0)
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_);
225 if (err != 0)
226 gold_fatal(_("pthread_cond_destroy failed: %s"), strerror(err));
229 void
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_);
234 if (err != 0)
235 gold_fatal(_("pthread_cond_wait failed: %s"), strerror(err));
238 void
239 Condvar_impl_threads::signal()
241 int err = pthread_cond_signal(&this->cond_);
242 if (err != 0)
243 gold_fatal(_("pthread_cond_signal failed: %s"), strerror(err));
246 void
247 Condvar_impl_threads::broadcast()
249 int err = pthread_cond_broadcast(&this->cond_);
250 if (err != 0)
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)
259 : lock_(lock)
261 if (!parameters->threads())
262 this->condvar_ = new Condvar_impl_nothreads;
263 else
265 #ifdef ENABLE_THREADS
266 this->condvar_ = new Condvar_impl_threads;
267 #else
268 gold_unreachable();
269 #endif
273 Condvar::~Condvar()
275 delete this->condvar_;
278 } // End namespace gold.