sl@0: /*- sl@0: * Copyright (c) 1997,98 The NetBSD Foundation, Inc. sl@0: * All rights reserved. sl@0: * sl@0: * This code is derived from software contributed to The NetBSD Foundation sl@0: * by J.T. Conklin. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 4. Neither the name of The NetBSD Foundation nor the names of its sl@0: * contributors may be used to endorse or promote products derived sl@0: * from this software without specific prior written permission. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS sl@0: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED sl@0: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS sl@0: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR sl@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF sl@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS sl@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN sl@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE sl@0: * POSSIBILITY OF SUCH DAMAGE. sl@0: * sl@0: * $FreeBSD: src/lib/libc/include/reentrant.h,v 1.3 2004/02/25 21:03:45 green Exp $ sl@0: */ sl@0: sl@0: /* sl@0: * Requirements: sl@0: * sl@0: * 1. The thread safe mechanism should be lightweight so the library can sl@0: * be used by non-threaded applications without unreasonable overhead. sl@0: * sl@0: * 2. There should be no dependency on a thread engine for non-threaded sl@0: * applications. sl@0: * sl@0: * 3. There should be no dependency on any particular thread engine. sl@0: * sl@0: * 4. The library should be able to be compiled without support for thread sl@0: * safety. sl@0: * sl@0: * sl@0: * Rationale: sl@0: * sl@0: * One approach for thread safety is to provide discrete versions of the sl@0: * library: one thread safe, the other not. The disadvantage of this is sl@0: * that libc is rather large, and two copies of a library which are 99%+ sl@0: * identical is not an efficent use of resources. sl@0: * sl@0: * Another approach is to provide a single thread safe library. However, sl@0: * it should not add significant run time or code size overhead to non- sl@0: * threaded applications. sl@0: * sl@0: * Since the NetBSD C library is used in other projects, it should be sl@0: * easy to replace the mutual exclusion primitives with ones provided by sl@0: * another system. Similarly, it should also be easy to remove all sl@0: * support for thread safety completely if the target environment does sl@0: * not support threads. sl@0: * sl@0: * sl@0: * Implementation Details: sl@0: * sl@0: * The mutex primitives used by the library (mutex_t, mutex_lock, etc.) sl@0: * are macros which expand to the cooresponding primitives provided by sl@0: * the thread engine or to nothing. The latter is used so that code is sl@0: * not unreasonably cluttered with #ifdefs when all thread safe support sl@0: * is removed. sl@0: * sl@0: * The mutex macros can be directly mapped to the mutex primitives from sl@0: * pthreads, however it should be reasonably easy to wrap another mutex sl@0: * implementation so it presents a similar interface. sl@0: * sl@0: * Stub implementations of the mutex functions are provided with *weak* sl@0: * linkage. These functions simply return success. When linked with a sl@0: * thread library (i.e. -lpthread), the functions will override the sl@0: * stubs. sl@0: */ sl@0: sl@0: #include sl@0: #ifndef __SYMBIAN32__ sl@0: #include sl@0: #endif sl@0: #include "libc_private.h" sl@0: sl@0: #define mutex_t pthread_mutex_t sl@0: #define cond_t pthread_cond_t sl@0: #define rwlock_t pthread_rwlock_t sl@0: #define once_t pthread_once_t sl@0: sl@0: #define thread_key_t pthread_key_t sl@0: #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER sl@0: #define RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER sl@0: #define ONCE_INITIALIZER PTHREAD_ONCE_INIT sl@0: sl@0: #define mutex_init(m, a) _pthread_mutex_init(m, a) sl@0: #define mutex_lock(m) if (__isthreaded) \ sl@0: _pthread_mutex_lock(m) sl@0: #define mutex_unlock(m) if (__isthreaded) \ sl@0: _pthread_mutex_unlock(m) sl@0: #define mutex_trylock(m) (__isthreaded ? 0 : _pthread_mutex_trylock(m)) sl@0: sl@0: #define cond_init(c, a, p) _pthread_cond_init(c, a) sl@0: #define cond_signal(m) if (__isthreaded) \ sl@0: _pthread_cond_signal(m) sl@0: #define cond_broadcast(m) if (__isthreaded) \ sl@0: _pthread_cond_broadcast(m) sl@0: #define cond_wait(c, m) if (__isthreaded) \ sl@0: _pthread_cond_wait(c, m) sl@0: sl@0: #define rwlock_init(l, a) _pthread_rwlock_init(l, a) sl@0: #define rwlock_rdlock(l) if (__isthreaded) \ sl@0: _pthread_rwlock_rdlock(l) sl@0: #define rwlock_wrlock(l) if (__isthreaded) \ sl@0: _pthread_rwlock_wrlock(l) sl@0: #define rwlock_unlock(l) if (__isthreaded) \ sl@0: _pthread_rwlock_unlock(l) sl@0: sl@0: #define thr_keycreate(k, d) _pthread_key_create(k, d) sl@0: #define thr_setspecific(k, p) _pthread_setspecific(k, p) sl@0: #define thr_getspecific(k) _pthread_getspecific(k) sl@0: #define thr_sigsetmask(f, n, o) _pthread_sigmask(f, n, o) sl@0: sl@0: #define thr_once(o, i) _pthread_once(o, i) sl@0: #define thr_self() _pthread_self() sl@0: #define thr_exit(x) _pthread_exit(x) sl@0: #define thr_main() _pthread_main_np()