add this momentarily, will move them to the attic soon
[binutils.git] / gold / gold-threads.cc
blob537373969ada98f81204e8a81a0e5350ae063927
1 // gold-threads.cc -- thread support for gold
3 #include "gold.h"
5 #ifdef ENABLE_THREADS
6 #include <pthread.h>
7 #endif
9 #include "gold-threads.h"
11 namespace gold
14 // Class Lock_impl.
16 class Lock_impl
18 public:
19 Lock_impl();
20 ~Lock_impl();
22 void acquire();
24 void release();
26 private:
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;
33 #ifdef ENABLE_THREADS
34 pthread_mutex_t mutex_;
35 #else
36 bool acquired_;
37 #endif
40 #ifdef ENABLE_THREADS
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);
50 #endif
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);
65 void
66 Lock_impl::acquire()
68 if (pthread_mutex_lock(&this->mutex_) != 0)
69 gold_fatal(_("pthread_mutex_lock failed"), true);
72 void
73 Lock_impl::release()
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()
82 : acquired_(false)
86 Lock_impl::~Lock_impl()
88 gold_assert(!this->acquired_);
91 void
92 Lock_impl::acquire()
94 gold_assert(!this->acquired_);
95 this->acquired_ = true;
98 void
99 Lock_impl::release()
101 gold_assert(this->acquired_);
102 this->acquired_ = false;
105 #endif // !defined(ENABLE_THREADS)
107 // Methods for Lock class.
109 Lock::Lock()
111 this->lock_ = new Lock_impl;
114 Lock::~Lock()
116 delete this->lock_;
119 void
120 Lock::acquire()
122 this->lock_->acquire();
125 void
126 Lock::release()
128 this->lock_->release();
131 // Class Condvar_impl.
133 class Condvar_impl
135 public:
136 Condvar_impl();
137 ~Condvar_impl();
139 void wait(Lock_impl*);
140 void signal();
142 private:
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_;
149 #endif
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);
166 void
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);
173 void
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()
190 void
191 Condvar_impl::wait(Lock_impl* li)
193 gold_assert(li->acquired_);
196 void
197 Condvar_impl::signal()
201 #endif // !defined(ENABLE_THREADS)
203 // Methods for Condvar class.
205 Condvar::Condvar(Lock& lock)
206 : lock_(lock)
208 this->condvar_ = new Condvar_impl;
211 Condvar::~Condvar()
213 delete this->condvar_;
216 void
217 Condvar::wait()
219 this->condvar_->wait(this->lock_.get_impl());
222 void
223 Condvar::signal()
225 this->condvar_->signal();
228 } // End namespace gold.