*** empty log message ***
[deliverable/binutils-gdb.git] / gold / gold-threads.cc
CommitLineData
bae7f79e
ILT
1// gold-threads.cc -- thread support for gold
2
6cb15b7f
ILT
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
bae7f79e
ILT
23#include "gold.h"
24
fe9a4c12
ILT
25#include <cerrno>
26#include <cstring>
27
bae7f79e
ILT
28#ifdef ENABLE_THREADS
29#include <pthread.h>
30#endif
31
32#include "gold-threads.h"
33
34namespace gold
35{
36
37// Class Lock_impl.
38
39class Lock_impl
40{
41 public:
42 Lock_impl();
43 ~Lock_impl();
44
45 void acquire();
46
47 void release();
48
49private:
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
65Lock_impl::Lock_impl()
66{
67 pthread_mutexattr_t attr;
68 if (pthread_mutexattr_init(&attr) != 0)
fe9a4c12 69 gold_fatal(_("pthead_mutextattr_init failed: %s"), strerror(errno));
bae7f79e
ILT
70#ifdef PTHREAD_MUTEXT_ADAPTIVE_NP
71 if (pthread_mutextattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP) != 0)
fe9a4c12 72 gold_fatal(_("pthread_mutextattr_settype failed: %s"), strerror(errno));
bae7f79e
ILT
73#endif
74
75 if (pthread_mutex_init (&this->mutex_, &attr) != 0)
fe9a4c12 76 gold_fatal(_("pthread_mutex_init failed: %s"), strerror(errno));
bae7f79e
ILT
77
78 if (pthread_mutexattr_destroy(&attr) != 0)
fe9a4c12 79 gold_fatal(_("pthread_mutexattr_destroy failed: %s"), strerror(errno));
bae7f79e
ILT
80}
81
82Lock_impl::~Lock_impl()
83{
84 if (pthread_mutex_destroy(&this->mutex_) != 0)
fe9a4c12 85 gold_fatal(_("pthread_mutex_destroy failed: %s"), strerror(errno));
bae7f79e
ILT
86}
87
88void
89Lock_impl::acquire()
90{
91 if (pthread_mutex_lock(&this->mutex_) != 0)
fe9a4c12 92 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(errno));
bae7f79e
ILT
93}
94
95void
96Lock_impl::release()
97{
98 if (pthread_mutex_unlock(&this->mutex_) != 0)
fe9a4c12 99 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(errno));
bae7f79e
ILT
100}
101
102#else // !defined(ENABLE_THREADS)
103
104Lock_impl::Lock_impl()
105 : acquired_(false)
106{
107}
108
109Lock_impl::~Lock_impl()
110{
a3ad94ed 111 gold_assert(!this->acquired_);
bae7f79e
ILT
112}
113
114void
115Lock_impl::acquire()
116{
a3ad94ed 117 gold_assert(!this->acquired_);
bae7f79e
ILT
118 this->acquired_ = true;
119}
120
121void
122Lock_impl::release()
123{
a3ad94ed 124 gold_assert(this->acquired_);
bae7f79e
ILT
125 this->acquired_ = false;
126}
127
128#endif // !defined(ENABLE_THREADS)
129
130// Methods for Lock class.
131
132Lock::Lock()
133{
134 this->lock_ = new Lock_impl;
135}
136
137Lock::~Lock()
138{
139 delete this->lock_;
140}
141
142void
143Lock::acquire()
144{
145 this->lock_->acquire();
146}
147
148void
149Lock::release()
150{
151 this->lock_->release();
152}
153
154// Class Condvar_impl.
155
156class 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
177Condvar_impl::Condvar_impl()
178{
179 if (pthread_cond_init(&this->cond_, NULL) != 0)
fe9a4c12 180 gold_fatal(_("pthread_cond_init failed: %s"), strerror(errno));
bae7f79e
ILT
181}
182
183Condvar_impl::~Condvar_impl()
184{
185 if (pthread_cond_destroy(&this->cond_) != 0)
fe9a4c12 186 gold_fatal(_("pthread_cond_destroy failed: %s"), strerror(errno));
bae7f79e
ILT
187}
188
189void
190Condvar_impl::wait(Lock_impl* li)
191{
192 if (pthread_cond_wait(&this->cond_, &li->mutex_) != 0)
fe9a4c12 193 gold_fatal(_("pthread_cond_wait failed: %s"), strerror(errno));
bae7f79e
ILT
194}
195
196void
197Condvar_impl::signal()
198{
199 if (pthread_cond_signal(&this->cond_) != 0)
fe9a4c12 200 gold_fatal(_("pthread_cond_signal failed: %s"), strerror(errno));
bae7f79e
ILT
201}
202
203#else // !defined(ENABLE_THREADS)
204
205Condvar_impl::Condvar_impl()
206{
207}
208
209Condvar_impl::~Condvar_impl()
210{
211}
212
213void
214Condvar_impl::wait(Lock_impl* li)
215{
a3ad94ed 216 gold_assert(li->acquired_);
bae7f79e
ILT
217}
218
219void
220Condvar_impl::signal()
221{
222}
223
224#endif // !defined(ENABLE_THREADS)
225
226// Methods for Condvar class.
227
228Condvar::Condvar(Lock& lock)
229 : lock_(lock)
230{
231 this->condvar_ = new Condvar_impl;
232}
233
234Condvar::~Condvar()
235{
236 delete this->condvar_;
237}
238
239void
240Condvar::wait()
241{
242 this->condvar_->wait(this->lock_.get_impl());
243}
244
245void
246Condvar::signal()
247{
248 this->condvar_->signal();
249}
250
251} // End namespace gold.
This page took 0.072058 seconds and 4 git commands to generate.