1 /*- 2 * Copyright (c) 1997,98 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by J.T. Conklin. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the NetBSD 19 * Foundation, Inc. and its contributors. 20 * 4. Neither the name of The NetBSD Foundation nor the names of its 21 * contributors may be used to endorse or promote products derived 22 * from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 /* 38 * Requirements: 39 * 40 * 1. The thread safe mechanism should be lightweight so the library can 41 * be used by non-threaded applications without unreasonable overhead. 42 * 43 * 2. There should be no dependency on a thread engine for non-threaded 44 * applications. 45 * 46 * 3. There should be no dependency on any particular thread engine. 47 * 48 * 4. The library should be able to be compiled without support for thread 49 * safety. 50 * 51 * 52 * Rationale: 53 * 54 * One approach for thread safety is to provide discrete versions of the 55 * library: one thread safe, the other not. The disadvantage of this is 56 * that libc is rather large, and two copies of a library which are 99%+ 57 * identical is not an efficent use of resources. 58 * 59 * Another approach is to provide a single thread safe library. However, 60 * it should not add significant run time or code size overhead to non- 61 * threaded applications. 62 * 63 * Since the NetBSD C library is used in other projects, it should be 64 * easy to replace the mutual exclusion primitives with ones provided by 65 * another system. Similarly, it should also be easy to remove all 66 * support for thread safety completely if the target environment does 67 * not support threads. 68 * 69 * 70 * Implementation Details: 71 * 72 * The mutex primitives used by the library (mutex_t, mutex_lock, etc.) 73 * are macros which expand to the cooresponding primitives provided by 74 * the thread engine or to nothing. The latter is used so that code is 75 * not unreasonably cluttered with #ifdefs when all thread safe support 76 * is removed. 77 * 78 * The mutex macros can be directly mapped to the mutex primitives from 79 * pthreads, however it should be reasonably easy to wrap another mutex 80 * implementation so it presents a similar interface. 81 * 82 * Stub implementations of the mutex functions are provided with *weak* 83 * linkage. These functions simply return success. When linked with a 84 * thread library (i.e. -lpthread), the functions will override the 85 * stubs. 86 */ 87 88 /* FIXME: Using _REENT during integration testing. It should be changed 89 to _REENTRANT once pthread engine is available */ 90 91 #ifdef _REENT 92 93 #include <pthread.h> 94 95 #define mutex_t pthread_mutex_t 96 #define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 97 98 #define mutex_lock(m) pthread_mutex_lock(m) 99 #define mutex_unlock(m) pthread_mutex_unlock(m) 100 101 #define rwlock_t pthread_rwlock_t 102 #define RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER 103 104 #define rwlock_rdlock(l) pthread_rwlock_rdlock(l) 105 #define rwlock_wrlock(l) pthread_rwlock_wrlock(l) 106 #define rwlock_unlock(l) pthread_rwlock_unlock(l) 107 108 #define FLOCKFILE(fp) flockfile(fp) 109 #define FUNLOCKFILE(fp) funlockfile(fp) 110 111 #define 112 113 #else 114 115 #define mutex_lock(m) do { } while(0) 116 #define mutex_unlock(m) do { } while(0) 117 118 #define rwlock_rdlock(l) do { } while(0) 119 #define rwlock_wrlock(l) do { } while(0) 120 #define rwlock_unlock(l) do { } while(0) 121 122 #define FLOCKFILE(fp) do { } while(0) 123 #define FUNLOCKFILE(fp) do { } while(0) 124 125 #endif 126