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