1 // gold-threads.cc -- thread support for gold
11 #include "gold-threads.h"
29 // This class can not be copied.
30 Lock_impl(const Lock_impl
&);
31 Lock_impl
& operator=(const Lock_impl
&);
33 friend class Condvar_impl
;
36 pthread_mutex_t mutex_
;
44 Lock_impl::Lock_impl()
46 pthread_mutexattr_t attr
;
47 if (pthread_mutexattr_init(&attr
) != 0)
48 gold_fatal(_("pthead_mutextattr_init failed"), true);
49 #ifdef PTHREAD_MUTEXT_ADAPTIVE_NP
50 if (pthread_mutextattr_settype(&attr
, PTHREAD_MUTEX_ADAPTIVE_NP
) != 0)
51 gold_fatal(_("pthread_mutextattr_settype failed"), true);
54 if (pthread_mutex_init (&this->mutex_
, &attr
) != 0)
55 gold_fatal(_("pthread_mutex_init failed"), true);
57 if (pthread_mutexattr_destroy(&attr
) != 0)
58 gold_fatal(_("pthread_mutexattr_destroy failed"), true);
61 Lock_impl::~Lock_impl()
63 if (pthread_mutex_destroy(&this->mutex_
) != 0)
64 gold_fatal(_("pthread_mutex_destroy failed"), true);
70 if (pthread_mutex_lock(&this->mutex_
) != 0)
71 gold_fatal(_("pthread_mutex_lock failed"), true);
77 if (pthread_mutex_unlock(&this->mutex_
) != 0)
78 gold_fatal(_("pthread_mutex_unlock failed"), true);
81 #else // !defined(ENABLE_THREADS)
83 Lock_impl::Lock_impl()
88 Lock_impl::~Lock_impl()
90 assert(!this->acquired_
);
96 assert(!this->acquired_
);
97 this->acquired_
= true;
103 assert(this->acquired_
);
104 this->acquired_
= false;
107 #endif // !defined(ENABLE_THREADS)
109 // Methods for Lock class.
113 this->lock_
= new Lock_impl
;
124 this->lock_
->acquire();
130 this->lock_
->release();
133 // Class Condvar_impl.
141 void wait(Lock_impl
*);
145 // This class can not be copied.
146 Condvar_impl(const Condvar_impl
&);
147 Condvar_impl
& operator=(const Condvar_impl
&);
149 #ifdef ENABLE_THREADS
150 pthread_cond_t cond_
;
154 #ifdef ENABLE_THREADS
156 Condvar_impl::Condvar_impl()
158 if (pthread_cond_init(&this->cond_
, NULL
) != 0)
159 gold_fatal(_("pthread_cond_init failed"), true);
162 Condvar_impl::~Condvar_impl()
164 if (pthread_cond_destroy(&this->cond_
) != 0)
165 gold_fatal(_("pthread_cond_destroy failed"), true);
169 Condvar_impl::wait(Lock_impl
* li
)
171 if (pthread_cond_wait(&this->cond_
, &li
->mutex_
) != 0)
172 gold_fatal(_("pthread_cond_wait failed"), true);
176 Condvar_impl::signal()
178 if (pthread_cond_signal(&this->cond_
) != 0)
179 gold_fatal(_("pthread_cond_signal failed"), true);
182 #else // !defined(ENABLE_THREADS)
184 Condvar_impl::Condvar_impl()
188 Condvar_impl::~Condvar_impl()
193 Condvar_impl::wait(Lock_impl
* li
)
195 assert(li
->acquired_
);
199 Condvar_impl::signal()
203 #endif // !defined(ENABLE_THREADS)
205 // Methods for Condvar class.
207 Condvar::Condvar(Lock
& lock
)
210 this->condvar_
= new Condvar_impl
;
215 delete this->condvar_
;
221 this->condvar_
->wait(this->lock_
.get_impl());
227 this->condvar_
->signal();
230 } // End namespace gold.