186d7f5d3SJohn Marino /*- 286d7f5d3SJohn Marino * Copyright (c) 1997,98 The NetBSD Foundation, Inc. 386d7f5d3SJohn Marino * All rights reserved. 486d7f5d3SJohn Marino * 586d7f5d3SJohn Marino * This code is derived from software contributed to The NetBSD Foundation 686d7f5d3SJohn Marino * by J.T. Conklin. 786d7f5d3SJohn Marino * 886d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without 986d7f5d3SJohn Marino * modification, are permitted provided that the following conditions 1086d7f5d3SJohn Marino * are met: 1186d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright 1286d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer. 1386d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright 1486d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the 1586d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution. 1686d7f5d3SJohn Marino * 3. All advertising materials mentioning features or use of this software 1786d7f5d3SJohn Marino * must display the following acknowledgement: 1886d7f5d3SJohn Marino * This product includes software developed by the NetBSD 1986d7f5d3SJohn Marino * Foundation, Inc. and its contributors. 2086d7f5d3SJohn Marino * 4. Neither the name of The NetBSD Foundation nor the names of its 2186d7f5d3SJohn Marino * contributors may be used to endorse or promote products derived 2286d7f5d3SJohn Marino * from this software without specific prior written permission. 2386d7f5d3SJohn Marino * 2486d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2586d7f5d3SJohn Marino * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2686d7f5d3SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2786d7f5d3SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2886d7f5d3SJohn Marino * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2986d7f5d3SJohn Marino * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3086d7f5d3SJohn Marino * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3186d7f5d3SJohn Marino * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3286d7f5d3SJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3386d7f5d3SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3486d7f5d3SJohn Marino * POSSIBILITY OF SUCH DAMAGE. 3586d7f5d3SJohn Marino * 3686d7f5d3SJohn Marino * $FreeBSD: src/lib/libc/include/reentrant.h,v 1.3 2004/02/25 21:03:45 green Exp $ 3786d7f5d3SJohn Marino */ 3886d7f5d3SJohn Marino 3986d7f5d3SJohn Marino /* 4086d7f5d3SJohn Marino * Requirements: 4186d7f5d3SJohn Marino * 4286d7f5d3SJohn Marino * 1. The thread safe mechanism should be lightweight so the library can 4386d7f5d3SJohn Marino * be used by non-threaded applications without unreasonable overhead. 4486d7f5d3SJohn Marino * 4586d7f5d3SJohn Marino * 2. There should be no dependency on a thread engine for non-threaded 4686d7f5d3SJohn Marino * applications. 4786d7f5d3SJohn Marino * 4886d7f5d3SJohn Marino * 3. There should be no dependency on any particular thread engine. 4986d7f5d3SJohn Marino * 5086d7f5d3SJohn Marino * 4. The library should be able to be compiled without support for thread 5186d7f5d3SJohn Marino * safety. 5286d7f5d3SJohn Marino * 5386d7f5d3SJohn Marino * 5486d7f5d3SJohn Marino * Rationale: 5586d7f5d3SJohn Marino * 5686d7f5d3SJohn Marino * One approach for thread safety is to provide discrete versions of the 5786d7f5d3SJohn Marino * library: one thread safe, the other not. The disadvantage of this is 5886d7f5d3SJohn Marino * that libc is rather large, and two copies of a library which are 99%+ 5986d7f5d3SJohn Marino * identical is not an efficent use of resources. 6086d7f5d3SJohn Marino * 6186d7f5d3SJohn Marino * Another approach is to provide a single thread safe library. However, 6286d7f5d3SJohn Marino * it should not add significant run time or code size overhead to non- 6386d7f5d3SJohn Marino * threaded applications. 6486d7f5d3SJohn Marino * 6586d7f5d3SJohn Marino * Since the NetBSD C library is used in other projects, it should be 6686d7f5d3SJohn Marino * easy to replace the mutual exclusion primitives with ones provided by 6786d7f5d3SJohn Marino * another system. Similarly, it should also be easy to remove all 6886d7f5d3SJohn Marino * support for thread safety completely if the target environment does 6986d7f5d3SJohn Marino * not support threads. 7086d7f5d3SJohn Marino * 7186d7f5d3SJohn Marino * 7286d7f5d3SJohn Marino * Implementation Details: 7386d7f5d3SJohn Marino * 7486d7f5d3SJohn Marino * The mutex primitives used by the library (mutex_t, mutex_lock, etc.) 7586d7f5d3SJohn Marino * are macros which expand to the cooresponding primitives provided by 7686d7f5d3SJohn Marino * the thread engine or to nothing. The latter is used so that code is 7786d7f5d3SJohn Marino * not unreasonably cluttered with #ifdefs when all thread safe support 7886d7f5d3SJohn Marino * is removed. 7986d7f5d3SJohn Marino * 8086d7f5d3SJohn Marino * The mutex macros can be directly mapped to the mutex primitives from 8186d7f5d3SJohn Marino * pthreads, however it should be reasonably easy to wrap another mutex 8286d7f5d3SJohn Marino * implementation so it presents a similar interface. 8386d7f5d3SJohn Marino * 8486d7f5d3SJohn Marino * Stub implementations of the mutex functions are provided with *weak* 8586d7f5d3SJohn Marino * linkage. These functions simply return success. When linked with a 8686d7f5d3SJohn Marino * thread library (i.e. -lpthread), the functions will override the 8786d7f5d3SJohn Marino * stubs. 8886d7f5d3SJohn Marino */ 8986d7f5d3SJohn Marino 9086d7f5d3SJohn Marino #include <pthread.h> 9186d7f5d3SJohn Marino #include <pthread_np.h> 9286d7f5d3SJohn Marino #include "libc_private.h" 9386d7f5d3SJohn Marino 9486d7f5d3SJohn Marino #define mutex_t pthread_mutex_t 9586d7f5d3SJohn Marino #define cond_t pthread_cond_t 9686d7f5d3SJohn Marino #define rwlock_t pthread_rwlock_t 9786d7f5d3SJohn Marino #define once_t pthread_once_t 9886d7f5d3SJohn Marino 9986d7f5d3SJohn Marino #define thread_key_t pthread_key_t 10086d7f5d3SJohn Marino #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 10186d7f5d3SJohn Marino #define RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER 10286d7f5d3SJohn Marino #define ONCE_INITIALIZER PTHREAD_ONCE_INIT 10386d7f5d3SJohn Marino 10486d7f5d3SJohn Marino #define mutex_init(m, a) _pthread_mutex_init(m, a) 10586d7f5d3SJohn Marino #define mutex_lock(m) if (__isthreaded) \ 10686d7f5d3SJohn Marino _pthread_mutex_lock(m) 10786d7f5d3SJohn Marino #define mutex_unlock(m) if (__isthreaded) \ 10886d7f5d3SJohn Marino _pthread_mutex_unlock(m) 10986d7f5d3SJohn Marino #define mutex_trylock(m) (__isthreaded ? 0 : _pthread_mutex_trylock(m)) 11086d7f5d3SJohn Marino 11186d7f5d3SJohn Marino #define cond_init(c, a, p) _pthread_cond_init(c, a) 11286d7f5d3SJohn Marino #define cond_signal(m) if (__isthreaded) \ 11386d7f5d3SJohn Marino _pthread_cond_signal(m) 11486d7f5d3SJohn Marino #define cond_broadcast(m) if (__isthreaded) \ 11586d7f5d3SJohn Marino _pthread_cond_broadcast(m) 11686d7f5d3SJohn Marino #define cond_wait(c, m) if (__isthreaded) \ 11786d7f5d3SJohn Marino _pthread_cond_wait(c, m) 11886d7f5d3SJohn Marino 11986d7f5d3SJohn Marino #define rwlock_init(l, a) _pthread_rwlock_init(l, a) 12086d7f5d3SJohn Marino #define rwlock_rdlock(l) if (__isthreaded) \ 12186d7f5d3SJohn Marino _pthread_rwlock_rdlock(l) 12286d7f5d3SJohn Marino #define rwlock_wrlock(l) if (__isthreaded) \ 12386d7f5d3SJohn Marino _pthread_rwlock_wrlock(l) 12486d7f5d3SJohn Marino #define rwlock_unlock(l) if (__isthreaded) \ 12586d7f5d3SJohn Marino _pthread_rwlock_unlock(l) 12686d7f5d3SJohn Marino 12786d7f5d3SJohn Marino #define thr_keycreate(k, d) _pthread_key_create(k, d) 12886d7f5d3SJohn Marino #define thr_setspecific(k, p) _pthread_setspecific(k, p) 12986d7f5d3SJohn Marino #define thr_getspecific(k) _pthread_getspecific(k) 13086d7f5d3SJohn Marino #define thr_sigsetmask(f, n, o) _pthread_sigmask(f, n, o) 13186d7f5d3SJohn Marino 13286d7f5d3SJohn Marino #define thr_once(o, i) _pthread_once(o, i) 13386d7f5d3SJohn Marino #define thr_self() _pthread_self() 13486d7f5d3SJohn Marino #define thr_exit(x) _pthread_exit(x) 13586d7f5d3SJohn Marino #define thr_main() _pthread_main_np() 136