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.
29 #include "gold-threads.h"
47 // This class can not be copied.
48 Lock_impl(const Lock_impl&);
49 Lock_impl& operator=(const Lock_impl&);
51 friend class Condvar_impl;
54 pthread_mutex_t mutex_;
62 Lock_impl::Lock_impl()
64 pthread_mutexattr_t attr;
65 if (pthread_mutexattr_init(&attr) != 0)
66 gold_fatal(_("pthead_mutextattr_init failed"), true);
67 #ifdef PTHREAD_MUTEXT_ADAPTIVE_NP
68 if (pthread_mutextattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP) != 0)
69 gold_fatal(_("pthread_mutextattr_settype failed"), true);
72 if (pthread_mutex_init (&this->mutex_, &attr) != 0)
73 gold_fatal(_("pthread_mutex_init failed"), true);
75 if (pthread_mutexattr_destroy(&attr) != 0)
76 gold_fatal(_("pthread_mutexattr_destroy failed"), true);
79 Lock_impl::~Lock_impl()
81 if (pthread_mutex_destroy(&this->mutex_) != 0)
82 gold_fatal(_("pthread_mutex_destroy failed"), true);
88 if (pthread_mutex_lock(&this->mutex_) != 0)
89 gold_fatal(_("pthread_mutex_lock failed"), true);
95 if (pthread_mutex_unlock(&this->mutex_) != 0)
96 gold_fatal(_("pthread_mutex_unlock failed"), true);
99 #else // !defined(ENABLE_THREADS)
101 Lock_impl::Lock_impl()
106 Lock_impl::~Lock_impl()
108 gold_assert(!this->acquired_);
114 gold_assert(!this->acquired_);
115 this->acquired_ = true;
121 gold_assert(this->acquired_);
122 this->acquired_ = false;
125 #endif // !defined(ENABLE_THREADS)
127 // Methods for Lock class.
131 this->lock_ = new Lock_impl;
142 this->lock_->acquire();
148 this->lock_->release();
151 // Class Condvar_impl.
159 void wait(Lock_impl*);
163 // This class can not be copied.
164 Condvar_impl(const Condvar_impl&);
165 Condvar_impl& operator=(const Condvar_impl&);
167 #ifdef ENABLE_THREADS
168 pthread_cond_t cond_;
172 #ifdef ENABLE_THREADS
174 Condvar_impl::Condvar_impl()
176 if (pthread_cond_init(&this->cond_, NULL) != 0)
177 gold_fatal(_("pthread_cond_init failed"), true);
180 Condvar_impl::~Condvar_impl()
182 if (pthread_cond_destroy(&this->cond_) != 0)
183 gold_fatal(_("pthread_cond_destroy failed"), true);
187 Condvar_impl::wait(Lock_impl* li)
189 if (pthread_cond_wait(&this->cond_, &li->mutex_) != 0)
190 gold_fatal(_("pthread_cond_wait failed"), true);
194 Condvar_impl::signal()
196 if (pthread_cond_signal(&this->cond_) != 0)
197 gold_fatal(_("pthread_cond_signal failed"), true);
200 #else // !defined(ENABLE_THREADS)
202 Condvar_impl::Condvar_impl()
206 Condvar_impl::~Condvar_impl()
211 Condvar_impl::wait(Lock_impl* li)
213 gold_assert(li->acquired_);
217 Condvar_impl::signal()
221 #endif // !defined(ENABLE_THREADS)
223 // Methods for Condvar class.
225 Condvar::Condvar(Lock& lock)
228 this->condvar_ = new Condvar_impl;
233 delete this->condvar_;
239 this->condvar_->wait(this->lock_.get_impl());
245 this->condvar_->signal();
248 } // End namespace gold.