Add infrastructure for threading support.
[deliverable/binutils-gdb.git] / gold / gold-threads.cc
1 // gold-threads.cc -- thread support for gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <cstring>
27
28 #ifdef ENABLE_THREADS
29 #include <pthread.h>
30 #endif
31
32 #include "gold-threads.h"
33
34 namespace gold
35 {
36
37 // Class Lock_impl.
38
39 class Lock_impl
40 {
41 public:
42 Lock_impl();
43 ~Lock_impl();
44
45 void acquire();
46
47 void release();
48
49 private:
50 // This class can not be copied.
51 Lock_impl(const Lock_impl&);
52 Lock_impl& operator=(const Lock_impl&);
53
54 friend class Condvar_impl;
55
56 #ifdef ENABLE_THREADS
57 pthread_mutex_t mutex_;
58 #else
59 bool acquired_;
60 #endif
61 };
62
63 #ifdef ENABLE_THREADS
64
65 Lock_impl::Lock_impl()
66 {
67 pthread_mutexattr_t attr;
68 if (pthread_mutexattr_init(&attr) != 0)
69 gold_fatal(_("pthead_mutextattr_init failed: %s"), strerror(errno));
70 #ifdef PTHREAD_MUTEXT_ADAPTIVE_NP
71 if (pthread_mutextattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP) != 0)
72 gold_fatal(_("pthread_mutextattr_settype failed: %s"), strerror(errno));
73 #endif
74
75 if (pthread_mutex_init (&this->mutex_, &attr) != 0)
76 gold_fatal(_("pthread_mutex_init failed: %s"), strerror(errno));
77
78 if (pthread_mutexattr_destroy(&attr) != 0)
79 gold_fatal(_("pthread_mutexattr_destroy failed: %s"), strerror(errno));
80 }
81
82 Lock_impl::~Lock_impl()
83 {
84 if (pthread_mutex_destroy(&this->mutex_) != 0)
85 gold_fatal(_("pthread_mutex_destroy failed: %s"), strerror(errno));
86 }
87
88 void
89 Lock_impl::acquire()
90 {
91 if (pthread_mutex_lock(&this->mutex_) != 0)
92 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(errno));
93 }
94
95 void
96 Lock_impl::release()
97 {
98 if (pthread_mutex_unlock(&this->mutex_) != 0)
99 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(errno));
100 }
101
102 #else // !defined(ENABLE_THREADS)
103
104 Lock_impl::Lock_impl()
105 : acquired_(false)
106 {
107 }
108
109 Lock_impl::~Lock_impl()
110 {
111 gold_assert(!this->acquired_);
112 }
113
114 void
115 Lock_impl::acquire()
116 {
117 gold_assert(!this->acquired_);
118 this->acquired_ = true;
119 }
120
121 void
122 Lock_impl::release()
123 {
124 gold_assert(this->acquired_);
125 this->acquired_ = false;
126 }
127
128 #endif // !defined(ENABLE_THREADS)
129
130 // Methods for Lock class.
131
132 Lock::Lock()
133 {
134 this->lock_ = new Lock_impl;
135 }
136
137 Lock::~Lock()
138 {
139 delete this->lock_;
140 }
141
142 void
143 Lock::acquire()
144 {
145 this->lock_->acquire();
146 }
147
148 void
149 Lock::release()
150 {
151 this->lock_->release();
152 }
153
154 // Class Condvar_impl.
155
156 class Condvar_impl
157 {
158 public:
159 Condvar_impl();
160 ~Condvar_impl();
161
162 void wait(Lock_impl*);
163 void signal();
164
165 private:
166 // This class can not be copied.
167 Condvar_impl(const Condvar_impl&);
168 Condvar_impl& operator=(const Condvar_impl&);
169
170 #ifdef ENABLE_THREADS
171 pthread_cond_t cond_;
172 #endif
173 };
174
175 #ifdef ENABLE_THREADS
176
177 Condvar_impl::Condvar_impl()
178 {
179 if (pthread_cond_init(&this->cond_, NULL) != 0)
180 gold_fatal(_("pthread_cond_init failed: %s"), strerror(errno));
181 }
182
183 Condvar_impl::~Condvar_impl()
184 {
185 if (pthread_cond_destroy(&this->cond_) != 0)
186 gold_fatal(_("pthread_cond_destroy failed: %s"), strerror(errno));
187 }
188
189 void
190 Condvar_impl::wait(Lock_impl* li)
191 {
192 if (pthread_cond_wait(&this->cond_, &li->mutex_) != 0)
193 gold_fatal(_("pthread_cond_wait failed: %s"), strerror(errno));
194 }
195
196 void
197 Condvar_impl::signal()
198 {
199 if (pthread_cond_signal(&this->cond_) != 0)
200 gold_fatal(_("pthread_cond_signal failed: %s"), strerror(errno));
201 }
202
203 #else // !defined(ENABLE_THREADS)
204
205 Condvar_impl::Condvar_impl()
206 {
207 }
208
209 Condvar_impl::~Condvar_impl()
210 {
211 }
212
213 void
214 Condvar_impl::wait(Lock_impl* li)
215 {
216 gold_assert(li->acquired_);
217 }
218
219 void
220 Condvar_impl::signal()
221 {
222 }
223
224 #endif // !defined(ENABLE_THREADS)
225
226 // Methods for Condvar class.
227
228 Condvar::Condvar(Lock& lock)
229 : lock_(lock)
230 {
231 this->condvar_ = new Condvar_impl;
232 }
233
234 Condvar::~Condvar()
235 {
236 delete this->condvar_;
237 }
238
239 void
240 Condvar::wait()
241 {
242 this->condvar_->wait(this->lock_.get_impl());
243 }
244
245 void
246 Condvar::signal()
247 {
248 this->condvar_->signal();
249 }
250
251 } // End namespace gold.
This page took 0.038744 seconds and 5 git commands to generate.