os/ossrv/genericopenlibs/openenvcore/libc/include/reentrant.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/include/reentrant.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,133 @@
     1.4 +/*-
     1.5 + * Copyright (c) 1997,98 The NetBSD Foundation, Inc.
     1.6 + * All rights reserved.
     1.7 + *
     1.8 + * This code is derived from software contributed to The NetBSD Foundation
     1.9 + * by J.T. Conklin.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + * 1. Redistributions of source code must retain the above copyright
    1.15 + *    notice, this list of conditions and the following disclaimer.
    1.16 + * 2. Redistributions in binary form must reproduce the above copyright
    1.17 + *    notice, this list of conditions and the following disclaimer in the
    1.18 + *    documentation and/or other materials provided with the distribution.
    1.19 + * 4. Neither the name of The NetBSD Foundation nor the names of its
    1.20 + *    contributors may be used to endorse or promote products derived
    1.21 + *    from this software without specific prior written permission.
    1.22 + *
    1.23 + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
    1.24 + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    1.25 + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.26 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    1.27 + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.28 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.29 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.30 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.31 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.32 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.33 + * POSSIBILITY OF SUCH DAMAGE.
    1.34 + *
    1.35 + * $FreeBSD: src/lib/libc/include/reentrant.h,v 1.3 2004/02/25 21:03:45 green Exp $
    1.36 + */
    1.37 +
    1.38 +/*
    1.39 + * Requirements:
    1.40 + * 
    1.41 + * 1. The thread safe mechanism should be lightweight so the library can
    1.42 + *    be used by non-threaded applications without unreasonable overhead.
    1.43 + * 
    1.44 + * 2. There should be no dependency on a thread engine for non-threaded
    1.45 + *    applications.
    1.46 + * 
    1.47 + * 3. There should be no dependency on any particular thread engine.
    1.48 + * 
    1.49 + * 4. The library should be able to be compiled without support for thread
    1.50 + *    safety.
    1.51 + * 
    1.52 + * 
    1.53 + * Rationale:
    1.54 + * 
    1.55 + * One approach for thread safety is to provide discrete versions of the
    1.56 + * library: one thread safe, the other not.  The disadvantage of this is
    1.57 + * that libc is rather large, and two copies of a library which are 99%+
    1.58 + * identical is not an efficent use of resources.
    1.59 + * 
    1.60 + * Another approach is to provide a single thread safe library.  However,
    1.61 + * it should not add significant run time or code size overhead to non-
    1.62 + * threaded applications.
    1.63 + * 
    1.64 + * Since the NetBSD C library is used in other projects, it should be
    1.65 + * easy to replace the mutual exclusion primitives with ones provided by
    1.66 + * another system.  Similarly, it should also be easy to remove all
    1.67 + * support for thread safety completely if the target environment does
    1.68 + * not support threads.
    1.69 + * 
    1.70 + * 
    1.71 + * Implementation Details:
    1.72 + * 
    1.73 + * The mutex primitives used by the library (mutex_t, mutex_lock, etc.)
    1.74 + * are macros which expand to the cooresponding primitives provided by
    1.75 + * the thread engine or to nothing.  The latter is used so that code is
    1.76 + * not unreasonably cluttered with #ifdefs when all thread safe support
    1.77 + * is removed.
    1.78 + * 
    1.79 + * The mutex macros can be directly mapped to the mutex primitives from
    1.80 + * pthreads, however it should be reasonably easy to wrap another mutex
    1.81 + * implementation so it presents a similar interface.
    1.82 + * 
    1.83 + * Stub implementations of the mutex functions are provided with *weak*
    1.84 + * linkage.  These functions simply return success.  When linked with a
    1.85 + * thread library (i.e. -lpthread), the functions will override the
    1.86 + * stubs.
    1.87 + */
    1.88 +
    1.89 +#include <pthread.h>
    1.90 +#ifndef __SYMBIAN32__
    1.91 +#include <pthread_np.h>
    1.92 +#endif
    1.93 +#include "libc_private.h"
    1.94 +
    1.95 +#define mutex_t			pthread_mutex_t
    1.96 +#define cond_t			pthread_cond_t
    1.97 +#define rwlock_t		pthread_rwlock_t
    1.98 +#define once_t			pthread_once_t
    1.99 +
   1.100 +#define thread_key_t		pthread_key_t
   1.101 +#define MUTEX_INITIALIZER	PTHREAD_MUTEX_INITIALIZER
   1.102 +#define RWLOCK_INITIALIZER	PTHREAD_RWLOCK_INITIALIZER
   1.103 +#define ONCE_INITIALIZER	PTHREAD_ONCE_INIT
   1.104 +
   1.105 +#define mutex_init(m, a)	_pthread_mutex_init(m, a)
   1.106 +#define mutex_lock(m)		if (__isthreaded) \
   1.107 +				_pthread_mutex_lock(m)
   1.108 +#define mutex_unlock(m)		if (__isthreaded) \
   1.109 +				_pthread_mutex_unlock(m)
   1.110 +#define mutex_trylock(m)	(__isthreaded ? 0 : _pthread_mutex_trylock(m))
   1.111 +
   1.112 +#define cond_init(c, a, p)	_pthread_cond_init(c, a)
   1.113 +#define cond_signal(m)		if (__isthreaded) \
   1.114 +				_pthread_cond_signal(m)
   1.115 +#define cond_broadcast(m)	if (__isthreaded) \
   1.116 +				_pthread_cond_broadcast(m)
   1.117 +#define cond_wait(c, m)		if (__isthreaded) \
   1.118 +				_pthread_cond_wait(c, m)
   1.119 +
   1.120 +#define rwlock_init(l, a)	_pthread_rwlock_init(l, a)
   1.121 +#define rwlock_rdlock(l)	if (__isthreaded) \
   1.122 +				_pthread_rwlock_rdlock(l)
   1.123 +#define rwlock_wrlock(l)	if (__isthreaded) \
   1.124 +				_pthread_rwlock_wrlock(l)
   1.125 +#define rwlock_unlock(l)	if (__isthreaded) \
   1.126 +				_pthread_rwlock_unlock(l)
   1.127 +
   1.128 +#define thr_keycreate(k, d)	_pthread_key_create(k, d)
   1.129 +#define thr_setspecific(k, p)	_pthread_setspecific(k, p)
   1.130 +#define thr_getspecific(k)	_pthread_getspecific(k)
   1.131 +#define thr_sigsetmask(f, n, o)	_pthread_sigmask(f, n, o)
   1.132 +
   1.133 +#define thr_once(o, i)		_pthread_once(o, i)
   1.134 +#define thr_self()		_pthread_self()
   1.135 +#define thr_exit(x)		_pthread_exit(x)
   1.136 +#define thr_main()		_pthread_main_np()