1 // gold-threads.cc -- thread support for gold
9 #include "gold-threads.h"
27 // This class can not be copied.
28 Lock_impl(const Lock_impl&);
29 Lock_impl& operator=(const Lock_impl&);
31 friend class Condvar_impl;
34 pthread_mutex_t mutex_;
42 Lock_impl::Lock_impl()
44 pthread_mutexattr_t attr;
45 if (pthread_mutexattr_init(&attr) != 0)
46 gold_fatal(_("pthead_mutextattr_init failed"), true);
47 #ifdef PTHREAD_MUTEXT_ADAPTIVE_NP
48 if (pthread_mutextattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP) != 0)
49 gold_fatal(_("pthread_mutextattr_settype failed"), true);
52 if (pthread_mutex_init (&this->mutex_, &attr) != 0)
53 gold_fatal(_("pthread_mutex_init failed"), true);
55 if (pthread_mutexattr_destroy(&attr) != 0)
56 gold_fatal(_("pthread_mutexattr_destroy failed"), true);
59 Lock_impl::~Lock_impl()
61 if (pthread_mutex_destroy(&this->mutex_) != 0)
62 gold_fatal(_("pthread_mutex_destroy failed"), true);
68 if (pthread_mutex_lock(&this->mutex_) != 0)
69 gold_fatal(_("pthread_mutex_lock failed"), true);
75 if (pthread_mutex_unlock(&this->mutex_) != 0)
76 gold_fatal(_("pthread_mutex_unlock failed"), true);
79 #else // !defined(ENABLE_THREADS)
81 Lock_impl::Lock_impl()
86 Lock_impl::~Lock_impl()
88 gold_assert(!this->acquired_);
94 gold_assert(!this->acquired_);
95 this->acquired_ = true;
101 gold_assert(this->acquired_);
102 this->acquired_ = false;
105 #endif // !defined(ENABLE_THREADS)
107 // Methods for Lock class.
111 this->lock_ = new Lock_impl;
122 this->lock_->acquire();
128 this->lock_->release();
131 // Class Condvar_impl.
139 void wait(Lock_impl*);
143 // This class can not be copied.
144 Condvar_impl(const Condvar_impl&);
145 Condvar_impl& operator=(const Condvar_impl&);
147 #ifdef ENABLE_THREADS
148 pthread_cond_t cond_;
152 #ifdef ENABLE_THREADS
154 Condvar_impl::Condvar_impl()
156 if (pthread_cond_init(&this->cond_, NULL) != 0)
157 gold_fatal(_("pthread_cond_init failed"), true);
160 Condvar_impl::~Condvar_impl()
162 if (pthread_cond_destroy(&this->cond_) != 0)
163 gold_fatal(_("pthread_cond_destroy failed"), true);
167 Condvar_impl::wait(Lock_impl* li)
169 if (pthread_cond_wait(&this->cond_, &li->mutex_) != 0)
170 gold_fatal(_("pthread_cond_wait failed"), true);
174 Condvar_impl::signal()
176 if (pthread_cond_signal(&this->cond_) != 0)
177 gold_fatal(_("pthread_cond_signal failed"), true);
180 #else // !defined(ENABLE_THREADS)
182 Condvar_impl::Condvar_impl()
186 Condvar_impl::~Condvar_impl()
191 Condvar_impl::wait(Lock_impl* li)
193 gold_assert(li->acquired_);
197 Condvar_impl::signal()
201 #endif // !defined(ENABLE_THREADS)
203 // Methods for Condvar class.
205 Condvar::Condvar(Lock& lock)
208 this->condvar_ = new Condvar_impl;
213 delete this->condvar_;
219 this->condvar_->wait(this->lock_.get_impl());
225 this->condvar_->signal();
228 } // End namespace gold.