Initial CVS checkin of gold
[deliverable/binutils-gdb.git] / gold / gold-threads.cc
CommitLineData
bae7f79e
ILT
1// gold-threads.cc -- thread support for gold
2
3#include <cassert>
4
5#include "gold.h"
6
7#ifdef ENABLE_THREADS
8#include <pthread.h>
9#endif
10
11#include "gold-threads.h"
12
13namespace gold
14{
15
16// Class Lock_impl.
17
18class Lock_impl
19{
20 public:
21 Lock_impl();
22 ~Lock_impl();
23
24 void acquire();
25
26 void release();
27
28private:
29 // This class can not be copied.
30 Lock_impl(const Lock_impl&);
31 Lock_impl& operator=(const Lock_impl&);
32
33 friend class Condvar_impl;
34
35#ifdef ENABLE_THREADS
36 pthread_mutex_t mutex_;
37#else
38 bool acquired_;
39#endif
40};
41
42#ifdef ENABLE_THREADS
43
44Lock_impl::Lock_impl()
45{
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
53
54 if (pthread_mutex_init (&this->mutex_, &attr) != 0)
55 gold_fatal(_("pthread_mutex_init failed"), true);
56
57 if (pthread_mutexattr_destroy(&attr) != 0)
58 gold_fatal(_("pthread_mutexattr_destroy failed"), true);
59}
60
61Lock_impl::~Lock_impl()
62{
63 if (pthread_mutex_destroy(&this->mutex_) != 0)
64 gold_fatal(_("pthread_mutex_destroy failed"), true);
65}
66
67void
68Lock_impl::acquire()
69{
70 if (pthread_mutex_lock(&this->mutex_) != 0)
71 gold_fatal(_("pthread_mutex_lock failed"), true);
72}
73
74void
75Lock_impl::release()
76{
77 if (pthread_mutex_unlock(&this->mutex_) != 0)
78 gold_fatal(_("pthread_mutex_unlock failed"), true);
79}
80
81#else // !defined(ENABLE_THREADS)
82
83Lock_impl::Lock_impl()
84 : acquired_(false)
85{
86}
87
88Lock_impl::~Lock_impl()
89{
90 assert(!this->acquired_);
91}
92
93void
94Lock_impl::acquire()
95{
96 assert(!this->acquired_);
97 this->acquired_ = true;
98}
99
100void
101Lock_impl::release()
102{
103 assert(this->acquired_);
104 this->acquired_ = false;
105}
106
107#endif // !defined(ENABLE_THREADS)
108
109// Methods for Lock class.
110
111Lock::Lock()
112{
113 this->lock_ = new Lock_impl;
114}
115
116Lock::~Lock()
117{
118 delete this->lock_;
119}
120
121void
122Lock::acquire()
123{
124 this->lock_->acquire();
125}
126
127void
128Lock::release()
129{
130 this->lock_->release();
131}
132
133// Class Condvar_impl.
134
135class Condvar_impl
136{
137 public:
138 Condvar_impl();
139 ~Condvar_impl();
140
141 void wait(Lock_impl*);
142 void signal();
143
144 private:
145 // This class can not be copied.
146 Condvar_impl(const Condvar_impl&);
147 Condvar_impl& operator=(const Condvar_impl&);
148
149#ifdef ENABLE_THREADS
150 pthread_cond_t cond_;
151#endif
152};
153
154#ifdef ENABLE_THREADS
155
156Condvar_impl::Condvar_impl()
157{
158 if (pthread_cond_init(&this->cond_, NULL) != 0)
159 gold_fatal(_("pthread_cond_init failed"), true);
160}
161
162Condvar_impl::~Condvar_impl()
163{
164 if (pthread_cond_destroy(&this->cond_) != 0)
165 gold_fatal(_("pthread_cond_destroy failed"), true);
166}
167
168void
169Condvar_impl::wait(Lock_impl* li)
170{
171 if (pthread_cond_wait(&this->cond_, &li->mutex_) != 0)
172 gold_fatal(_("pthread_cond_wait failed"), true);
173}
174
175void
176Condvar_impl::signal()
177{
178 if (pthread_cond_signal(&this->cond_) != 0)
179 gold_fatal(_("pthread_cond_signal failed"), true);
180}
181
182#else // !defined(ENABLE_THREADS)
183
184Condvar_impl::Condvar_impl()
185{
186}
187
188Condvar_impl::~Condvar_impl()
189{
190}
191
192void
193Condvar_impl::wait(Lock_impl* li)
194{
195 assert(li->acquired_);
196}
197
198void
199Condvar_impl::signal()
200{
201}
202
203#endif // !defined(ENABLE_THREADS)
204
205// Methods for Condvar class.
206
207Condvar::Condvar(Lock& lock)
208 : lock_(lock)
209{
210 this->condvar_ = new Condvar_impl;
211}
212
213Condvar::~Condvar()
214{
215 delete this->condvar_;
216}
217
218void
219Condvar::wait()
220{
221 this->condvar_->wait(this->lock_.get_impl());
222}
223
224void
225Condvar::signal()
226{
227 this->condvar_->signal();
228}
229
230} // End namespace gold.
This page took 0.031781 seconds and 4 git commands to generate.