/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1997,98 The NetBSD Foundation, Inc.4* All rights reserved.5*6* This code is derived from software contributed to The NetBSD Foundation7* by J.T. Conklin.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS19* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED20* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS22* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28* POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* Requirements:33*34* 1. The thread safe mechanism should be lightweight so the library can35* be used by non-threaded applications without unreasonable overhead.36*37* 2. There should be no dependency on a thread engine for non-threaded38* applications.39*40* 3. There should be no dependency on any particular thread engine.41*42* 4. The library should be able to be compiled without support for thread43* safety.44*45*46* Rationale:47*48* One approach for thread safety is to provide discrete versions of the49* library: one thread safe, the other not. The disadvantage of this is50* that libc is rather large, and two copies of a library which are 99%+51* identical is not an efficient use of resources.52*53* Another approach is to provide a single thread safe library. However,54* it should not add significant run time or code size overhead to non-55* threaded applications.56*57* Since the NetBSD C library is used in other projects, it should be58* easy to replace the mutual exclusion primitives with ones provided by59* another system. Similarly, it should also be easy to remove all60* support for thread safety completely if the target environment does61* not support threads.62*63*64* Implementation Details:65*66* The mutex primitives used by the library (mutex_t, mutex_lock, etc.)67* are macros which expand to the corresponding primitives provided by68* the thread engine or to nothing. The latter is used so that code is69* not unreasonably cluttered with #ifdefs when all thread safe support70* is removed.71*72* The mutex macros can be directly mapped to the mutex primitives from73* pthreads, however it should be reasonably easy to wrap another mutex74* implementation so it presents a similar interface.75*76* Stub implementations of the mutex functions are provided with *weak*77* linkage. These functions simply return success. When linked with a78* thread library (i.e. -lpthread), the functions will override the79* stubs.80*/8182#include <pthread.h>83#include <pthread_np.h>84#include "libc_private.h"8586#define mutex_t pthread_mutex_t87#define cond_t pthread_cond_t88#define rwlock_t pthread_rwlock_t89#define once_t pthread_once_t9091#define thread_key_t pthread_key_t92#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER93#define RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER94#define ONCE_INITIALIZER PTHREAD_ONCE_INIT9596#define mutex_init(m, a) _pthread_mutex_init(m, a)97#define mutex_lock(m) if (__isthreaded) \98_pthread_mutex_lock(m)99#define mutex_unlock(m) if (__isthreaded) \100_pthread_mutex_unlock(m)101#define mutex_trylock(m) (__isthreaded ? 0 : _pthread_mutex_trylock(m))102103#define cond_init(c, a, p) _pthread_cond_init(c, a)104#define cond_signal(m) if (__isthreaded) \105_pthread_cond_signal(m)106#define cond_broadcast(m) if (__isthreaded) \107_pthread_cond_broadcast(m)108#define cond_wait(c, m) if (__isthreaded) \109_pthread_cond_wait(c, m)110111#define rwlock_init(l, a) _pthread_rwlock_init(l, a)112#define rwlock_rdlock(l) if (__isthreaded) \113_pthread_rwlock_rdlock(l)114#define rwlock_wrlock(l) if (__isthreaded) \115_pthread_rwlock_wrlock(l)116#define rwlock_unlock(l) if (__isthreaded) \117_pthread_rwlock_unlock(l)118119#define thr_keycreate(k, d) _pthread_key_create(k, d)120#define thr_setspecific(k, p) _pthread_setspecific(k, p)121#define thr_getspecific(k) _pthread_getspecific(k)122#define thr_sigsetmask(f, n, o) _pthread_sigmask(f, n, o)123124#define thr_once(o, i) _pthread_once(o, i)125#define thr_self() _pthread_self()126#define thr_exit(x) _pthread_exit(x)127#define thr_main() _pthread_main_np()128129130