Add Elf_file interface which can be used by both Sized_relobj and
[binutils.git] / gold / gold-threads.cc
blobc6e1170488fdf277e0acf8bd998bc8ecb56705ab
1 // gold-threads.cc -- thread support for gold
3 #include <cassert>
5 #include "gold.h"
7 #ifdef ENABLE_THREADS
8 #include <pthread.h>
9 #endif
11 #include "gold-threads.h"
13 namespace gold
16 // Class Lock_impl.
18 class Lock_impl
20 public:
21 Lock_impl();
22 ~Lock_impl();
24 void acquire();
26 void release();
28 private:
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;
35 #ifdef ENABLE_THREADS
36 pthread_mutex_t mutex_;
37 #else
38 bool acquired_;
39 #endif
42 #ifdef ENABLE_THREADS
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);
52 #endif
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);
67 void
68 Lock_impl::acquire()
70 if (pthread_mutex_lock(&this->mutex_) != 0)
71 gold_fatal(_("pthread_mutex_lock failed"), true);
74 void
75 Lock_impl::release()
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()
84 : acquired_(false)
88 Lock_impl::~Lock_impl()
90 assert(!this->acquired_);
93 void
94 Lock_impl::acquire()
96 assert(!this->acquired_);
97 this->acquired_ = true;
100 void
101 Lock_impl::release()
103 assert(this->acquired_);
104 this->acquired_ = false;
107 #endif // !defined(ENABLE_THREADS)
109 // Methods for Lock class.
111 Lock::Lock()
113 this->lock_ = new Lock_impl;
116 Lock::~Lock()
118 delete this->lock_;
121 void
122 Lock::acquire()
124 this->lock_->acquire();
127 void
128 Lock::release()
130 this->lock_->release();
133 // Class Condvar_impl.
135 class Condvar_impl
137 public:
138 Condvar_impl();
139 ~Condvar_impl();
141 void wait(Lock_impl*);
142 void signal();
144 private:
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_;
151 #endif
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);
168 void
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);
175 void
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()
192 void
193 Condvar_impl::wait(Lock_impl* li)
195 assert(li->acquired_);
198 void
199 Condvar_impl::signal()
203 #endif // !defined(ENABLE_THREADS)
205 // Methods for Condvar class.
207 Condvar::Condvar(Lock& lock)
208 : lock_(lock)
210 this->condvar_ = new Condvar_impl;
213 Condvar::~Condvar()
215 delete this->condvar_;
218 void
219 Condvar::wait()
221 this->condvar_->wait(this->lock_.get_impl());
224 void
225 Condvar::signal()
227 this->condvar_->signal();
230 } // End namespace gold.