1 // gold-threads.cc -- thread support for gold
3 // Copyright (C) 2006-2024 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.
32 #include "parameters.h"
33 #include "gold-threads.h"
38 class Condvar_impl_nothreads
;
40 // The non-threaded version of Lock_impl.
42 class Lock_impl_nothreads
: public Lock_impl
49 ~Lock_impl_nothreads()
50 { gold_assert(!this->acquired_
); }
55 gold_assert(!this->acquired_
);
56 this->acquired_
= true;
62 gold_assert(this->acquired_
);
63 this->acquired_
= false;
67 friend class Condvar_impl_nothreads
;
74 class Condvar_impl_threads
;
76 // The threaded version of Lock_impl.
78 class Lock_impl_threads
: public Lock_impl
89 // This class can not be copied.
90 Lock_impl_threads(const Lock_impl_threads
&);
91 Lock_impl_threads
& operator=(const Lock_impl_threads
&);
93 friend class Condvar_impl_threads
;
95 pthread_mutex_t mutex_
;
98 Lock_impl_threads::Lock_impl_threads()
100 pthread_mutexattr_t attr
;
101 int err
= pthread_mutexattr_init(&attr
);
103 gold_fatal(_("pthead_mutexattr_init failed: %s"), strerror(err
));
104 #ifdef PTHREAD_MUTEX_ADAPTIVE_NP
105 err
= pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_ADAPTIVE_NP
);
107 gold_fatal(_("pthread_mutexattr_settype failed: %s"), strerror(err
));
110 err
= pthread_mutex_init(&this->mutex_
, &attr
);
112 gold_fatal(_("pthread_mutex_init failed: %s"), strerror(err
));
114 err
= pthread_mutexattr_destroy(&attr
);
116 gold_fatal(_("pthread_mutexattr_destroy failed: %s"), strerror(err
));
119 Lock_impl_threads::~Lock_impl_threads()
121 int err
= pthread_mutex_destroy(&this->mutex_
);
123 gold_fatal(_("pthread_mutex_destroy failed: %s"), strerror(err
));
127 Lock_impl_threads::acquire()
129 int err
= pthread_mutex_lock(&this->mutex_
);
131 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(err
));
135 Lock_impl_threads::release()
137 int err
= pthread_mutex_unlock(&this->mutex_
);
139 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(err
));
142 #endif // defined(ENABLE_THREADS)
148 if (!parameters
->options().threads())
149 this->lock_
= new Lock_impl_nothreads
;
152 #ifdef ENABLE_THREADS
153 this->lock_
= new Lock_impl_threads
;
165 // The non-threaded version of Condvar_impl.
167 class Condvar_impl_nothreads
: public Condvar_impl
170 Condvar_impl_nothreads()
173 ~Condvar_impl_nothreads()
178 { gold_assert(static_cast<Lock_impl_nothreads
*>(li
)->acquired_
); }
189 #ifdef ENABLE_THREADS
191 // The threaded version of Condvar_impl.
193 class Condvar_impl_threads
: public Condvar_impl
196 Condvar_impl_threads();
197 ~Condvar_impl_threads();
209 // This class can not be copied.
210 Condvar_impl_threads(const Condvar_impl_threads
&);
211 Condvar_impl_threads
& operator=(const Condvar_impl_threads
&);
213 pthread_cond_t cond_
;
216 Condvar_impl_threads::Condvar_impl_threads()
218 int err
= pthread_cond_init(&this->cond_
, NULL
);
220 gold_fatal(_("pthread_cond_init failed: %s"), strerror(err
));
223 Condvar_impl_threads::~Condvar_impl_threads()
225 int err
= pthread_cond_destroy(&this->cond_
);
227 gold_fatal(_("pthread_cond_destroy failed: %s"), strerror(err
));
231 Condvar_impl_threads::wait(Lock_impl
* li
)
233 Lock_impl_threads
* lit
= static_cast<Lock_impl_threads
*>(li
);
234 int err
= pthread_cond_wait(&this->cond_
, &lit
->mutex_
);
236 gold_fatal(_("pthread_cond_wait failed: %s"), strerror(err
));
240 Condvar_impl_threads::signal()
242 int err
= pthread_cond_signal(&this->cond_
);
244 gold_fatal(_("pthread_cond_signal failed: %s"), strerror(err
));
248 Condvar_impl_threads::broadcast()
250 int err
= pthread_cond_broadcast(&this->cond_
);
252 gold_fatal(_("pthread_cond_broadcast failed: %s"), strerror(err
));
255 #endif // defined(ENABLE_THREADS)
257 // Methods for Condvar class.
259 Condvar::Condvar(Lock
& lock
)
262 if (!parameters
->options().threads())
263 this->condvar_
= new Condvar_impl_nothreads
;
266 #ifdef ENABLE_THREADS
267 this->condvar_
= new Condvar_impl_threads
;
276 delete this->condvar_
;
279 #ifdef ENABLE_THREADS
281 // Class Once_initialize. This exists to hold a pthread_once_t
282 // structure for Once.
284 class Once_initialize
288 : once_(PTHREAD_ONCE_INIT
)
291 // Return a pointer to the pthread_once_t variable.
294 { return &this->once_
; }
297 pthread_once_t once_
;
300 #endif // defined(ENABLE_THREADS)
302 #ifdef ENABLE_THREADS
304 // A single lock which controls access to once_pointer. This is used
305 // because we can't pass parameters to functions passed to
308 static pthread_mutex_t once_pointer_control
= PTHREAD_MUTEX_INITIALIZER
;
310 // A pointer to Once structure we want to run. Access to this is
311 // controlled by once_pointer_control.
313 static Once
* once_pointer
;
315 // The argument to pass to the Once structure. Access to this is
316 // controlled by once_pointer_control.
318 static void* once_arg
;
320 // A routine passed to pthread_once which runs the Once pointer.
328 once_pointer
->internal_run(once_arg
);
333 #endif // defined(ENABLE_THREADS)
339 #if defined(ENABLE_THREADS) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
343 #ifndef ENABLE_THREADS
346 this->once_
= new Once_initialize();
350 // Run the function once.
353 Once::run_once(void* arg
)
355 #ifndef ENABLE_THREADS
357 // If there is no threads support, we don't need to use pthread_once.
359 this->internal_run(arg
);
361 #else // defined(ENABLE_THREADS)
363 if (parameters
->options_valid() && !parameters
->options().threads())
365 // If we are not using threads, we don't need to lock.
367 this->internal_run(arg
);
371 // If we have the sync builtins, use them to skip the lock if the
372 // value has already been initialized.
373 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
376 if (__sync_bool_compare_and_swap(&this->was_run_lock_
, 0, 1))
379 bool was_run
= this->was_run_
;
382 if (__sync_bool_compare_and_swap(&this->was_run_lock_
, 1, 0))
389 // Since we can't pass parameters to routines called by
390 // pthread_once, we use a static variable: once_pointer. This in
391 // turns means that we need to use a mutex to control access to
394 int err
= pthread_mutex_lock(&once_pointer_control
);
396 gold_fatal(_("pthread_mutex_lock failed: %s"), strerror(err
));
401 err
= pthread_once(this->once_
->once_control(), c_run_once
);
403 gold_fatal(_("pthread_once failed: %s"), strerror(err
));
408 err
= pthread_mutex_unlock(&once_pointer_control
);
410 gold_fatal(_("pthread_mutex_unlock failed: %s"), strerror(err
));
412 #endif // defined(ENABLE_THREADS)
415 // Actually run the function in the child class. This function will
419 Once::internal_run(void* arg
)
421 this->do_run_once(arg
);
422 this->was_run_
= true;
425 // Class Initialize_lock.
427 // Initialize the lock.
430 Initialize_lock::initialize()
432 // We can't initialize the lock until we have read the options.
433 if (!parameters
->options_valid())
437 this->run_once(NULL
);
442 // Initialize the lock exactly once.
445 Initialize_lock::do_run_once(void*)
447 *this->pplock_
= new Lock();
450 } // End namespace gold.