xref: /titanic_50/usr/src/lib/libc/inc/mtlib.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #ifndef	_MTLIB_H
28*7c478bd9Sstevel@tonic-gate #define	_MTLIB_H
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <thread.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
35*7c478bd9Sstevel@tonic-gate extern "C" {
36*7c478bd9Sstevel@tonic-gate #endif
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /* these are private to the library */
39*7c478bd9Sstevel@tonic-gate extern	int	primary_link_map;
40*7c478bd9Sstevel@tonic-gate extern	int	_private_mutex_init(mutex_t *, int, void *);
41*7c478bd9Sstevel@tonic-gate extern	int	_private_mutex_destroy(mutex_t *);
42*7c478bd9Sstevel@tonic-gate extern	int	_private_mutex_lock(mutex_t *);
43*7c478bd9Sstevel@tonic-gate extern	int	_private_mutex_trylock(mutex_t *);
44*7c478bd9Sstevel@tonic-gate extern	int	_private_mutex_unlock(mutex_t *);
45*7c478bd9Sstevel@tonic-gate #define	rmutex_lock	_private_mutex_lock
46*7c478bd9Sstevel@tonic-gate #define	rmutex_trylock	_private_mutex_trylock
47*7c478bd9Sstevel@tonic-gate #define	rmutex_unlock	_private_mutex_unlock
48*7c478bd9Sstevel@tonic-gate extern	void	lmutex_lock(mutex_t *);
49*7c478bd9Sstevel@tonic-gate extern	void	lmutex_unlock(mutex_t *);
50*7c478bd9Sstevel@tonic-gate extern	int	__rwlock_init(rwlock_t *, int, void *);
51*7c478bd9Sstevel@tonic-gate extern	int	__rwlock_destroy(rwlock_t *);
52*7c478bd9Sstevel@tonic-gate extern	int	__rw_rdlock(rwlock_t *);
53*7c478bd9Sstevel@tonic-gate extern	int	__rw_wrlock(rwlock_t *);
54*7c478bd9Sstevel@tonic-gate extern	int	__rw_tryrdlock(rwlock_t *);
55*7c478bd9Sstevel@tonic-gate extern	int	__rw_trywrlock(rwlock_t *);
56*7c478bd9Sstevel@tonic-gate extern	int	__rw_unlock(rwlock_t *);
57*7c478bd9Sstevel@tonic-gate extern	void	lrw_rdlock(rwlock_t *);
58*7c478bd9Sstevel@tonic-gate extern	void	lrw_wrlock(rwlock_t *);
59*7c478bd9Sstevel@tonic-gate extern	void	lrw_unlock(rwlock_t *);
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /* the rest are public functions */
62*7c478bd9Sstevel@tonic-gate extern	int	_mutex_init(mutex_t *, int, void *);
63*7c478bd9Sstevel@tonic-gate extern	int	_mutex_destroy(mutex_t *);
64*7c478bd9Sstevel@tonic-gate extern	int	_mutex_lock(mutex_t *);
65*7c478bd9Sstevel@tonic-gate extern	int	_mutex_trylock(mutex_t *);
66*7c478bd9Sstevel@tonic-gate extern	int	_mutex_unlock(mutex_t *);
67*7c478bd9Sstevel@tonic-gate extern	int	__mutex_init(mutex_t *, int, void *);
68*7c478bd9Sstevel@tonic-gate extern	int	__mutex_destroy(mutex_t *);
69*7c478bd9Sstevel@tonic-gate extern	int	__mutex_lock(mutex_t *);
70*7c478bd9Sstevel@tonic-gate extern	int	__mutex_trylock(mutex_t *);
71*7c478bd9Sstevel@tonic-gate extern	int	__mutex_unlock(mutex_t *);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate extern	int	_cond_init(cond_t *, int, void *);
74*7c478bd9Sstevel@tonic-gate extern	int	_cond_destroy(cond_t *);
75*7c478bd9Sstevel@tonic-gate extern	int	_cond_wait(cond_t *, mutex_t *);
76*7c478bd9Sstevel@tonic-gate extern	int	_cond_timedwait(cond_t *, mutex_t *, const timespec_t *);
77*7c478bd9Sstevel@tonic-gate extern	int	_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
78*7c478bd9Sstevel@tonic-gate extern	int	_cond_signal(cond_t *);
79*7c478bd9Sstevel@tonic-gate extern	int	_cond_broadcast(cond_t *);
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate extern	int	_rwlock_init(rwlock_t *, int, void *);
82*7c478bd9Sstevel@tonic-gate extern	int	_rwlock_destroy(rwlock_t *);
83*7c478bd9Sstevel@tonic-gate extern	int	_rw_rdlock(rwlock_t *);
84*7c478bd9Sstevel@tonic-gate extern	int	_rw_wrlock(rwlock_t *);
85*7c478bd9Sstevel@tonic-gate extern	int	_rw_tryrdlock(rwlock_t *);
86*7c478bd9Sstevel@tonic-gate extern	int	_rw_trywrlock(rwlock_t *);
87*7c478bd9Sstevel@tonic-gate extern	int	_rw_unlock(rwlock_t *);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate extern	int	_thr_main(void);
90*7c478bd9Sstevel@tonic-gate extern	thread_t _thr_self(void);
91*7c478bd9Sstevel@tonic-gate extern	void	_thr_exit(void *);
92*7c478bd9Sstevel@tonic-gate extern	size_t	_thr_min_stack(void);
93*7c478bd9Sstevel@tonic-gate extern	int	_thr_kill(thread_t, int);
94*7c478bd9Sstevel@tonic-gate extern	int	_thr_keycreate(thread_key_t *, void (*)(void *));
95*7c478bd9Sstevel@tonic-gate extern	int	_thr_setspecific(thread_key_t, void *);
96*7c478bd9Sstevel@tonic-gate extern	int	_thr_getspecific(thread_key_t, void **);
97*7c478bd9Sstevel@tonic-gate extern	void	*_pthread_getspecific(thread_key_t);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG)
100*7c478bd9Sstevel@tonic-gate extern	void	assert_no_libc_locks_held(void);
101*7c478bd9Sstevel@tonic-gate #else
102*7c478bd9Sstevel@tonic-gate #define	assert_no_libc_locks_held()
103*7c478bd9Sstevel@tonic-gate #endif
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate #define	_FWRITE _fwrite_unlocked
106*7c478bd9Sstevel@tonic-gate #define	FILENO(s) _fileno_unlocked(s)
107*7c478bd9Sstevel@tonic-gate #define	FEOF(s) _feof_unlocked(s)
108*7c478bd9Sstevel@tonic-gate #define	FERROR(s) _ferror_unlocked(s)
109*7c478bd9Sstevel@tonic-gate #define	CLEARERR(s) _clearerr_unlocked(s)
110*7c478bd9Sstevel@tonic-gate #define	GETC(s) _getc_unlocked(s)
111*7c478bd9Sstevel@tonic-gate #define	UNGETC(c, s) _ungetc_unlocked(c, s)
112*7c478bd9Sstevel@tonic-gate #define	PUTC(c, s) _putc_unlocked(c, s)
113*7c478bd9Sstevel@tonic-gate #define	GETWC(s) getwc(s)
114*7c478bd9Sstevel@tonic-gate #define	PUTWC(c, s) putwc(c, s)
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate /*
117*7c478bd9Sstevel@tonic-gate  * Cheap check to tell if stdio needs to lock for MT progs.
118*7c478bd9Sstevel@tonic-gate  * Referenced directly in port/stdio/flush.c and FLOCKFILE and
119*7c478bd9Sstevel@tonic-gate  * FUNLOCKFILE macros.  __threaded gets set to 1 when the first
120*7c478bd9Sstevel@tonic-gate  * thread (beyond the main thread) is created in _thrp_create().
121*7c478bd9Sstevel@tonic-gate  */
122*7c478bd9Sstevel@tonic-gate extern	int	__threaded;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate #define	FILELOCKING(iop)	(GET_IONOLOCK(iop) == 0)
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate #define	FLOCKFILE(lk, iop) \
127*7c478bd9Sstevel@tonic-gate 	{ \
128*7c478bd9Sstevel@tonic-gate 		if (__threaded && FILELOCKING(iop)) \
129*7c478bd9Sstevel@tonic-gate 			lk = _flockget((iop)); \
130*7c478bd9Sstevel@tonic-gate 		else \
131*7c478bd9Sstevel@tonic-gate 			lk = NULL; \
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate #define	FUNLOCKFILE(lk) \
135*7c478bd9Sstevel@tonic-gate 	{ \
136*7c478bd9Sstevel@tonic-gate 		if (lk != NULL) \
137*7c478bd9Sstevel@tonic-gate 			_flockrel(lk); \
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate #define	FLOCKRETURN(iop, ret) \
141*7c478bd9Sstevel@tonic-gate 	{	int r; \
142*7c478bd9Sstevel@tonic-gate 		rmutex_t *lk; \
143*7c478bd9Sstevel@tonic-gate 		FLOCKFILE(lk, iop); \
144*7c478bd9Sstevel@tonic-gate 		r = (ret); \
145*7c478bd9Sstevel@tonic-gate 		FUNLOCKFILE(lk); \
146*7c478bd9Sstevel@tonic-gate 		return (r); \
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate #endif
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate #endif	/* _MTLIB_H */
154