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