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.
32 #include "gold-threads.h"
50 // This class can not be copied.
51 Lock_impl(const Lock_impl&);
52 Lock_impl& operator=(const Lock_impl&);
54 friend class Condvar_impl;
57 pthread_mutex_t mutex_;
65 Lock_impl::Lock_impl()
67 pthread_mutexattr_t attr;
68 if (pthread_mutexattr_init(&attr) != 0)
69 gold_fatal(_("pthead_mutextattr_init failed: %s"), strerror(errno));
70 #ifdef PTHREAD_MUTEXT_ADAPTIVE_NP
71 if (pthread_mutextattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP) != 0)
72 gold_fatal(_("pthread_mutextattr_settype failed: %s"), strerror(errno));
75 if (pthread_mutex_init (&this->mutex_, &attr) != 0)
76 gold_fatal(_("pthread_mutex_init failed: %s"), strerror(errno));
78 if (pthread_mutexattr_destroy(&attr) != 0)
79 gold_fatal(_("pthread_mutexattr_destroy failed: %s"), strerror(errno));
82 Lock_impl::~Lock_impl()
84 if (pthread_mutex_destroy(&this->mutex_) != 0)
85 gold_fatal(_("pthread_mutex_destroy failed: %s"), strerror(errno));
91 if (pthread_mutex_lock(&this->mutex_) != 0)
92 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(errno));
98 if (pthread_mutex_unlock(&this->mutex_) != 0)
99 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(errno));
102 #else // !defined(ENABLE_THREADS)
104 Lock_impl::Lock_impl()
109 Lock_impl::~Lock_impl()
111 gold_assert(!this->acquired_);
117 gold_assert(!this->acquired_);
118 this->acquired_ = true;
124 gold_assert(this->acquired_);
125 this->acquired_ = false;
128 #endif // !defined(ENABLE_THREADS)
130 // Methods for Lock class.
134 this->lock_ = new Lock_impl;
145 this->lock_->acquire();
151 this->lock_->release();
154 // Class Condvar_impl.
162 void wait(Lock_impl*);
166 // This class can not be copied.
167 Condvar_impl(const Condvar_impl&);
168 Condvar_impl& operator=(const Condvar_impl&);
170 #ifdef ENABLE_THREADS
171 pthread_cond_t cond_;
175 #ifdef ENABLE_THREADS
177 Condvar_impl::Condvar_impl()
179 if (pthread_cond_init(&this->cond_, NULL) != 0)
180 gold_fatal(_("pthread_cond_init failed: %s"), strerror(errno));
183 Condvar_impl::~Condvar_impl()
185 if (pthread_cond_destroy(&this->cond_) != 0)
186 gold_fatal(_("pthread_cond_destroy failed: %s"), strerror(errno));
190 Condvar_impl::wait(Lock_impl* li)
192 if (pthread_cond_wait(&this->cond_, &li->mutex_) != 0)
193 gold_fatal(_("pthread_cond_wait failed: %s"), strerror(errno));
197 Condvar_impl::signal()
199 if (pthread_cond_signal(&this->cond_) != 0)
200 gold_fatal(_("pthread_cond_signal failed: %s"), strerror(errno));
203 #else // !defined(ENABLE_THREADS)
205 Condvar_impl::Condvar_impl()
209 Condvar_impl::~Condvar_impl()
214 Condvar_impl::wait(Lock_impl* li)
216 gold_assert(li->acquired_);
220 Condvar_impl::signal()
224 #endif // !defined(ENABLE_THREADS)
226 // Methods for Condvar class.
228 Condvar::Condvar(Lock& lock)
231 this->condvar_ = new Condvar_impl;
236 delete this->condvar_;
242 this->condvar_->wait(this->lock_.get_impl());
248 this->condvar_->signal();
251 } // End namespace gold.