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