]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | namespace gold | |
14 | { | |
15 | ||
16 | // Class Lock_impl. | |
17 | ||
18 | class Lock_impl | |
19 | { | |
20 | public: | |
21 | Lock_impl(); | |
22 | ~Lock_impl(); | |
23 | ||
24 | void acquire(); | |
25 | ||
26 | void release(); | |
27 | ||
28 | private: | |
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 | ||
44 | Lock_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 | ||
61 | Lock_impl::~Lock_impl() | |
62 | { | |
63 | if (pthread_mutex_destroy(&this->mutex_) != 0) | |
64 | gold_fatal(_("pthread_mutex_destroy failed"), true); | |
65 | } | |
66 | ||
67 | void | |
68 | Lock_impl::acquire() | |
69 | { | |
70 | if (pthread_mutex_lock(&this->mutex_) != 0) | |
71 | gold_fatal(_("pthread_mutex_lock failed"), true); | |
72 | } | |
73 | ||
74 | void | |
75 | Lock_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 | ||
83 | Lock_impl::Lock_impl() | |
84 | : acquired_(false) | |
85 | { | |
86 | } | |
87 | ||
88 | Lock_impl::~Lock_impl() | |
89 | { | |
90 | assert(!this->acquired_); | |
91 | } | |
92 | ||
93 | void | |
94 | Lock_impl::acquire() | |
95 | { | |
96 | assert(!this->acquired_); | |
97 | this->acquired_ = true; | |
98 | } | |
99 | ||
100 | void | |
101 | Lock_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 | ||
111 | Lock::Lock() | |
112 | { | |
113 | this->lock_ = new Lock_impl; | |
114 | } | |
115 | ||
116 | Lock::~Lock() | |
117 | { | |
118 | delete this->lock_; | |
119 | } | |
120 | ||
121 | void | |
122 | Lock::acquire() | |
123 | { | |
124 | this->lock_->acquire(); | |
125 | } | |
126 | ||
127 | void | |
128 | Lock::release() | |
129 | { | |
130 | this->lock_->release(); | |
131 | } | |
132 | ||
133 | // Class Condvar_impl. | |
134 | ||
135 | class 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 | ||
156 | Condvar_impl::Condvar_impl() | |
157 | { | |
158 | if (pthread_cond_init(&this->cond_, NULL) != 0) | |
159 | gold_fatal(_("pthread_cond_init failed"), true); | |
160 | } | |
161 | ||
162 | Condvar_impl::~Condvar_impl() | |
163 | { | |
164 | if (pthread_cond_destroy(&this->cond_) != 0) | |
165 | gold_fatal(_("pthread_cond_destroy failed"), true); | |
166 | } | |
167 | ||
168 | void | |
169 | Condvar_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 | ||
175 | void | |
176 | Condvar_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 | ||
184 | Condvar_impl::Condvar_impl() | |
185 | { | |
186 | } | |
187 | ||
188 | Condvar_impl::~Condvar_impl() | |
189 | { | |
190 | } | |
191 | ||
192 | void | |
193 | Condvar_impl::wait(Lock_impl* li) | |
194 | { | |
195 | assert(li->acquired_); | |
196 | } | |
197 | ||
198 | void | |
199 | Condvar_impl::signal() | |
200 | { | |
201 | } | |
202 | ||
203 | #endif // !defined(ENABLE_THREADS) | |
204 | ||
205 | // Methods for Condvar class. | |
206 | ||
207 | Condvar::Condvar(Lock& lock) | |
208 | : lock_(lock) | |
209 | { | |
210 | this->condvar_ = new Condvar_impl; | |
211 | } | |
212 | ||
213 | Condvar::~Condvar() | |
214 | { | |
215 | delete this->condvar_; | |
216 | } | |
217 | ||
218 | void | |
219 | Condvar::wait() | |
220 | { | |
221 | this->condvar_->wait(this->lock_.get_impl()); | |
222 | } | |
223 | ||
224 | void | |
225 | Condvar::signal() | |
226 | { | |
227 | this->condvar_->signal(); | |
228 | } | |
229 | ||
230 | } // End namespace gold. |