1*c2c66affSColin Finck /*-
2*c2c66affSColin Finck  * Copyright (c) 1997,98 The NetBSD Foundation, Inc.
3*c2c66affSColin Finck  * All rights reserved.
4*c2c66affSColin Finck  *
5*c2c66affSColin Finck  * This code is derived from software contributed to The NetBSD Foundation
6*c2c66affSColin Finck  * by J.T. Conklin.
7*c2c66affSColin Finck  *
8*c2c66affSColin Finck  * Redistribution and use in source and binary forms, with or without
9*c2c66affSColin Finck  * modification, are permitted provided that the following conditions
10*c2c66affSColin Finck  * are met:
11*c2c66affSColin Finck  * 1. Redistributions of source code must retain the above copyright
12*c2c66affSColin Finck  *    notice, this list of conditions and the following disclaimer.
13*c2c66affSColin Finck  * 2. Redistributions in binary form must reproduce the above copyright
14*c2c66affSColin Finck  *    notice, this list of conditions and the following disclaimer in the
15*c2c66affSColin Finck  *    documentation and/or other materials provided with the distribution.
16*c2c66affSColin Finck  * 3. All advertising materials mentioning features or use of this software
17*c2c66affSColin Finck  *    must display the following acknowledgement:
18*c2c66affSColin Finck  *        This product includes software developed by the NetBSD
19*c2c66affSColin Finck  *        Foundation, Inc. and its contributors.
20*c2c66affSColin Finck  * 4. Neither the name of The NetBSD Foundation nor the names of its
21*c2c66affSColin Finck  *    contributors may be used to endorse or promote products derived
22*c2c66affSColin Finck  *    from this software without specific prior written permission.
23*c2c66affSColin Finck  *
24*c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25*c2c66affSColin Finck  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26*c2c66affSColin Finck  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27*c2c66affSColin Finck  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28*c2c66affSColin Finck  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29*c2c66affSColin Finck  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30*c2c66affSColin Finck  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31*c2c66affSColin Finck  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32*c2c66affSColin Finck  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33*c2c66affSColin Finck  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34*c2c66affSColin Finck  * POSSIBILITY OF SUCH DAMAGE.
35*c2c66affSColin Finck  *
36*c2c66affSColin Finck  * $FreeBSD: src/lib/libc/include/reentrant.h,v 1.2 2002/11/01 09:37:17 dfr Exp $
37*c2c66affSColin Finck  */
38*c2c66affSColin Finck 
39*c2c66affSColin Finck /*
40*c2c66affSColin Finck  * Requirements:
41*c2c66affSColin Finck  *
42*c2c66affSColin Finck  * 1. The thread safe mechanism should be lightweight so the library can
43*c2c66affSColin Finck  *    be used by non-threaded applications without unreasonable overhead.
44*c2c66affSColin Finck  *
45*c2c66affSColin Finck  * 2. There should be no dependency on a thread engine for non-threaded
46*c2c66affSColin Finck  *    applications.
47*c2c66affSColin Finck  *
48*c2c66affSColin Finck  * 3. There should be no dependency on any particular thread engine.
49*c2c66affSColin Finck  *
50*c2c66affSColin Finck  * 4. The library should be able to be compiled without support for thread
51*c2c66affSColin Finck  *    safety.
52*c2c66affSColin Finck  *
53*c2c66affSColin Finck  *
54*c2c66affSColin Finck  * Rationale:
55*c2c66affSColin Finck  *
56*c2c66affSColin Finck  * One approach for thread safety is to provide discrete versions of the
57*c2c66affSColin Finck  * library: one thread safe, the other not.  The disadvantage of this is
58*c2c66affSColin Finck  * that libc is rather large, and two copies of a library which are 99%+
59*c2c66affSColin Finck  * identical is not an efficent use of resources.
60*c2c66affSColin Finck  *
61*c2c66affSColin Finck  * Another approach is to provide a single thread safe library.  However,
62*c2c66affSColin Finck  * it should not add significant run time or code size overhead to non-
63*c2c66affSColin Finck  * threaded applications.
64*c2c66affSColin Finck  *
65*c2c66affSColin Finck  * Since the NetBSD C library is used in other projects, it should be
66*c2c66affSColin Finck  * easy to replace the mutual exclusion primitives with ones provided by
67*c2c66affSColin Finck  * another system.  Similarly, it should also be easy to remove all
68*c2c66affSColin Finck  * support for thread safety completely if the target environment does
69*c2c66affSColin Finck  * not support threads.
70*c2c66affSColin Finck  *
71*c2c66affSColin Finck  *
72*c2c66affSColin Finck  * Implementation Details:
73*c2c66affSColin Finck  *
74*c2c66affSColin Finck  * The mutex primitives used by the library (mutex_t, mutex_lock, etc.)
75*c2c66affSColin Finck  * are macros which expand to the cooresponding primitives provided by
76*c2c66affSColin Finck  * the thread engine or to nothing.  The latter is used so that code is
77*c2c66affSColin Finck  * not unreasonably cluttered with #ifdefs when all thread safe support
78*c2c66affSColin Finck  * is removed.
79*c2c66affSColin Finck  *
80*c2c66affSColin Finck  * The mutex macros can be directly mapped to the mutex primitives from
81*c2c66affSColin Finck  * pthreads, however it should be reasonably easy to wrap another mutex
82*c2c66affSColin Finck  * implementation so it presents a similar interface.
83*c2c66affSColin Finck  *
84*c2c66affSColin Finck  * Stub implementations of the mutex functions are provided with *weak*
85*c2c66affSColin Finck  * linkage.  These functions simply return success.  When linked with a
86*c2c66affSColin Finck  * thread library (i.e. -lpthread), the functions will override the
87*c2c66affSColin Finck  * stubs.
88*c2c66affSColin Finck  */
89*c2c66affSColin Finck 
90*c2c66affSColin Finck /* NFSv4.1 client for Windows
91*c2c66affSColin Finck  * Copyright � 2012 The Regents of the University of Michigan
92*c2c66affSColin Finck  *
93*c2c66affSColin Finck  * Olga Kornievskaia <aglo@umich.edu>
94*c2c66affSColin Finck  * Casey Bodley <cbodley@umich.edu>
95*c2c66affSColin Finck  *
96*c2c66affSColin Finck  * This library is free software; you can redistribute it and/or modify it
97*c2c66affSColin Finck  * under the terms of the GNU Lesser General Public License as published by
98*c2c66affSColin Finck  * the Free Software Foundation; either version 2.1 of the License, or (at
99*c2c66affSColin Finck  * your option) any later version.
100*c2c66affSColin Finck  *
101*c2c66affSColin Finck  * This library is distributed in the hope that it will be useful, but
102*c2c66affSColin Finck  * without any warranty; without even the implied warranty of merchantability
103*c2c66affSColin Finck  * or fitness for a particular purpose.  See the GNU Lesser General Public
104*c2c66affSColin Finck  * License for more details.
105*c2c66affSColin Finck  *
106*c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public License
107*c2c66affSColin Finck  * along with this library; if not, write to the Free Software Foundation,
108*c2c66affSColin Finck  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA
109*c2c66affSColin Finck  */
110*c2c66affSColin Finck 
111*c2c66affSColin Finck #ifndef _REENTRANT_H
112*c2c66affSColin Finck #define _REENTRANT_H
113*c2c66affSColin Finck //#include <pthread.h>
114*c2c66affSColin Finck #include <libc_private.h>
115*c2c66affSColin Finck 
116*c2c66affSColin Finck #include <stdlib.h>
117*c2c66affSColin Finck 
118*c2c66affSColin Finck 
119*c2c66affSColin Finck #define mutex_t				CRITICAL_SECTION
120*c2c66affSColin Finck #define cond_t				CONDITION_VARIABLE
121*c2c66affSColin Finck #define rwlock_t			SRWLOCK
122*c2c66affSColin Finck 
123*c2c66affSColin Finck 
124*c2c66affSColin Finck #define thread_key_t		DWORD
125*c2c66affSColin Finck #define MUTEX_INITIALIZER	-1 /*THIS_NEEDS_HELP*/
126*c2c66affSColin Finck #define RWLOCK_INITIALIZER	-1 /*THIS_NEEDS_HELP*/
127*c2c66affSColin Finck #define mutex_init(m, a)	InitializeCriticalSection(m)
128*c2c66affSColin Finck #define mutex_lock(m)		EnterCriticalSection(m)
129*c2c66affSColin Finck #define mutex_unlock(m)		LeaveCriticalSection(m)
130*c2c66affSColin Finck #define mutex_trylock(m)	TryEnterCriticalSection(m)
131*c2c66affSColin Finck 
132*c2c66affSColin Finck #define cond_init(c, a, p)		InitializeConditionVariable(c)
133*c2c66affSColin Finck #define cond_signal(m)			WakeConditionVariable(m)
134*c2c66affSColin Finck #define cond_broadcast(m)		WakeAllConditionVariable(m)
135*c2c66affSColin Finck #define cond_wait(c, m)			SleepConditionVariableCS(c, m, INFINITE)
136*c2c66affSColin Finck #define cond_wait_timed(c, m, t) SleepConditionVariableCS(c, m, t)
137*c2c66affSColin Finck 
138*c2c66affSColin Finck #define rwlock_init(l, a)		InitializeSRWLock(l)
139*c2c66affSColin Finck #define rwlock_rdlock(l)		AcquireSRWLockShared(l)
140*c2c66affSColin Finck #define rwlock_wrlock(l)		AcquireSRWLockExclusive(l)
141*c2c66affSColin Finck /* XXX Code will have to be changed to release the right kind!!! XXX */
142*c2c66affSColin Finck #define rwlock_unlock(l)		ReleaseSRWLockExclusive(l)
143*c2c66affSColin Finck 
144*c2c66affSColin Finck #define thr_keycreate(k, d)		((*k) = TlsAlloc())
145*c2c66affSColin Finck #define thr_keydelete(k)		TlsFree(k)
146*c2c66affSColin Finck #define thr_setspecific(k, p)	TlsSetValue(k, p)
147*c2c66affSColin Finck #define thr_getspecific(k)		TlsGetValue(k)
148*c2c66affSColin Finck #define thr_sigsetmask(f, n, o)	 dunno_sigmask(f, n, o)
149*c2c66affSColin Finck 
150*c2c66affSColin Finck #define thr_self()				GetCurrentThreadId()
151*c2c66affSColin Finck #define thr_exit(x)				ExitThread(x)
152*c2c66affSColin Finck 
153*c2c66affSColin Finck /*
154*c2c66affSColin Finck #define mutex_t			pthread_mutex_t
155*c2c66affSColin Finck #define cond_t			pthread_cond_t
156*c2c66affSColin Finck #define rwlock_t		pthread_rwlock_t
157*c2c66affSColin Finck 
158*c2c66affSColin Finck #define thread_key_t		pthread_key_t
159*c2c66affSColin Finck #define MUTEX_INITIALIZER	PTHREAD_MUTEX_INITIALIZER
160*c2c66affSColin Finck #define RWLOCK_INITIALIZER	PTHREAD_RWLOCK_INITIALIZER
161*c2c66affSColin Finck #define mutex_init(m, a)	pthread_mutex_init(m, a)
162*c2c66affSColin Finck #define mutex_lock(m)		pthread_mutex_lock(m)
163*c2c66affSColin Finck #define mutex_unlock(m)		pthread_mutex_unlock(m)
164*c2c66affSColin Finck #define mutex_trylock(m)	pthread_mutex_trylock(m)
165*c2c66affSColin Finck 
166*c2c66affSColin Finck #define cond_init(c, a, p)	 pthread_cond_init(c, a)
167*c2c66affSColin Finck #define cond_signal(m)		 pthread_cond_signal(m)
168*c2c66affSColin Finck #define cond_broadcast(m)	 pthread_cond_broadcast(m)
169*c2c66affSColin Finck #define cond_wait(c, m)		 pthread_cond_wait(c, m)
170*c2c66affSColin Finck 
171*c2c66affSColin Finck #define rwlock_init(l, a)        pthread_rwlock_init(l, a)
172*c2c66affSColin Finck #define rwlock_rdlock(l)	 pthread_rwlock_rdlock(l)
173*c2c66affSColin Finck #define rwlock_wrlock(l)	 pthread_rwlock_wrlock(l)
174*c2c66affSColin Finck #define rwlock_unlock(l)	 pthread_rwlock_unlock(l)
175*c2c66affSColin Finck 
176*c2c66affSColin Finck #define thr_keycreate(k, d)	 pthread_key_create(k, d)
177*c2c66affSColin Finck #define thr_keydelete(k)	 pthread_key_delete(k)
178*c2c66affSColin Finck #define thr_setspecific(k, p)	 pthread_setspecific(k, p)
179*c2c66affSColin Finck #define thr_getspecific(k)	 pthread_getspecific(k)
180*c2c66affSColin Finck #define thr_sigsetmask(f, n, o)	 pthread_sigmask(f, n, o)
181*c2c66affSColin Finck 
182*c2c66affSColin Finck #define thr_self()		 pthread_self()
183*c2c66affSColin Finck #define thr_exit(x)		 pthread_exit(x)
184*c2c66affSColin Finck */
185*c2c66affSColin Finck #endif /* reentrant.h */
186