Rename t1 to basic_test, add static tests.
[binutils.git] / gold / gold-threads.cc
blobad9506cc9947f65a1c406139907a8603c329a291
1 // gold-threads.cc -- thread support for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #ifdef ENABLE_THREADS
26 #include <pthread.h>
27 #endif
29 #include "gold-threads.h"
31 namespace gold
34 // Class Lock_impl.
36 class Lock_impl
38 public:
39 Lock_impl();
40 ~Lock_impl();
42 void acquire();
44 void release();
46 private:
47 // This class can not be copied.
48 Lock_impl(const Lock_impl&);
49 Lock_impl& operator=(const Lock_impl&);
51 friend class Condvar_impl;
53 #ifdef ENABLE_THREADS
54 pthread_mutex_t mutex_;
55 #else
56 bool acquired_;
57 #endif
60 #ifdef ENABLE_THREADS
62 Lock_impl::Lock_impl()
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
72 if (pthread_mutex_init (&this->mutex_, &attr) != 0)
73 gold_fatal(_("pthread_mutex_init failed"), true);
75 if (pthread_mutexattr_destroy(&attr) != 0)
76 gold_fatal(_("pthread_mutexattr_destroy failed"), true);
79 Lock_impl::~Lock_impl()
81 if (pthread_mutex_destroy(&this->mutex_) != 0)
82 gold_fatal(_("pthread_mutex_destroy failed"), true);
85 void
86 Lock_impl::acquire()
88 if (pthread_mutex_lock(&this->mutex_) != 0)
89 gold_fatal(_("pthread_mutex_lock failed"), true);
92 void
93 Lock_impl::release()
95 if (pthread_mutex_unlock(&this->mutex_) != 0)
96 gold_fatal(_("pthread_mutex_unlock failed"), true);
99 #else // !defined(ENABLE_THREADS)
101 Lock_impl::Lock_impl()
102 : acquired_(false)
106 Lock_impl::~Lock_impl()
108 gold_assert(!this->acquired_);
111 void
112 Lock_impl::acquire()
114 gold_assert(!this->acquired_);
115 this->acquired_ = true;
118 void
119 Lock_impl::release()
121 gold_assert(this->acquired_);
122 this->acquired_ = false;
125 #endif // !defined(ENABLE_THREADS)
127 // Methods for Lock class.
129 Lock::Lock()
131 this->lock_ = new Lock_impl;
134 Lock::~Lock()
136 delete this->lock_;
139 void
140 Lock::acquire()
142 this->lock_->acquire();
145 void
146 Lock::release()
148 this->lock_->release();
151 // Class Condvar_impl.
153 class Condvar_impl
155 public:
156 Condvar_impl();
157 ~Condvar_impl();
159 void wait(Lock_impl*);
160 void signal();
162 private:
163 // This class can not be copied.
164 Condvar_impl(const Condvar_impl&);
165 Condvar_impl& operator=(const Condvar_impl&);
167 #ifdef ENABLE_THREADS
168 pthread_cond_t cond_;
169 #endif
172 #ifdef ENABLE_THREADS
174 Condvar_impl::Condvar_impl()
176 if (pthread_cond_init(&this->cond_, NULL) != 0)
177 gold_fatal(_("pthread_cond_init failed"), true);
180 Condvar_impl::~Condvar_impl()
182 if (pthread_cond_destroy(&this->cond_) != 0)
183 gold_fatal(_("pthread_cond_destroy failed"), true);
186 void
187 Condvar_impl::wait(Lock_impl* li)
189 if (pthread_cond_wait(&this->cond_, &li->mutex_) != 0)
190 gold_fatal(_("pthread_cond_wait failed"), true);
193 void
194 Condvar_impl::signal()
196 if (pthread_cond_signal(&this->cond_) != 0)
197 gold_fatal(_("pthread_cond_signal failed"), true);
200 #else // !defined(ENABLE_THREADS)
202 Condvar_impl::Condvar_impl()
206 Condvar_impl::~Condvar_impl()
210 void
211 Condvar_impl::wait(Lock_impl* li)
213 gold_assert(li->acquired_);
216 void
217 Condvar_impl::signal()
221 #endif // !defined(ENABLE_THREADS)
223 // Methods for Condvar class.
225 Condvar::Condvar(Lock& lock)
226 : lock_(lock)
228 this->condvar_ = new Condvar_impl;
231 Condvar::~Condvar()
233 delete this->condvar_;
236 void
237 Condvar::wait()
239 this->condvar_->wait(this->lock_.get_impl());
242 void
243 Condvar::signal()
245 this->condvar_->signal();
248 } // End namespace gold.