xref: /illumos-gate/usr/src/lib/libc/inc/mtlib.h (revision bc1f688b4872ace323eaddbb1a6365d054e7bf56)
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 2008 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	void	lmutex_lock(mutex_t *);
41 extern	void	lmutex_unlock(mutex_t *);
42 extern	void	lrw_rdlock(rwlock_t *);
43 extern	void	lrw_wrlock(rwlock_t *);
44 extern	void	lrw_unlock(rwlock_t *);
45 extern	void	sig_mutex_lock(mutex_t *);
46 extern	void	sig_mutex_unlock(mutex_t *);
47 extern	int	sig_mutex_trylock(mutex_t *);
48 extern	int	sig_cond_wait(cond_t *, mutex_t *);
49 extern	int	sig_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
50 extern	void	cancel_safe_mutex_lock(mutex_t *);
51 extern	void	cancel_safe_mutex_unlock(mutex_t *);
52 extern	int	cancel_safe_mutex_trylock(mutex_t *);
53 extern	int	cancel_active(void);
54 extern	int	_thrp_cancelled(void);
55 
56 /* the private libc thread-safe allocator */
57 extern	void	*lmalloc(size_t);
58 extern	void	lfree(void *, size_t);
59 
60 #if defined(THREAD_DEBUG)
61 extern	void	assert_no_libc_locks_held(void);
62 #else
63 #define	assert_no_libc_locks_held()
64 #endif
65 
66 #define	_FWRITE _fwrite_unlocked
67 #define	FILENO(s) _fileno(s)
68 #define	FERROR(s) ferror(s)
69 #define	GETC(s) _getc_unlocked(s)
70 #define	UNGETC(c, s) _ungetc_unlocked(c, s)
71 #define	PUTC(c, s) _putc_unlocked(c, s)
72 #define	GETWC(s) getwc(s)
73 #define	PUTWC(c, s) putwc(c, s)
74 
75 /*
76  * Cheap check to tell if stdio needs to lock for MT progs.
77  * Referenced directly in port/stdio/flush.c and FLOCKFILE and
78  * FUNLOCKFILE macros.  __libc_threaded gets set to 1 when the first
79  * thread (beyond the main thread) is created in _thrp_create().
80  */
81 extern	int	__libc_threaded;
82 
83 #define	FILELOCKING(iop)	(GET_IONOLOCK(iop) == 0)
84 
85 #define	FLOCKFILE(lk, iop) \
86 	{ \
87 		if (__libc_threaded && FILELOCKING(iop)) \
88 			lk = _flockget((iop)); \
89 		else \
90 			lk = NULL; \
91 	}
92 
93 #define	FUNLOCKFILE(lk) \
94 	{ \
95 		if (lk != NULL) \
96 			_flockrel(lk); \
97 	}
98 
99 #define	FLOCKRETURN(iop, ret) \
100 	{	int r; \
101 		rmutex_t *lk; \
102 		FLOCKFILE(lk, iop); \
103 		r = (ret); \
104 		FUNLOCKFILE(lk); \
105 		return (r); \
106 	}
107 
108 #ifdef	__cplusplus
109 }
110 #endif
111 
112 #endif	/* _MTLIB_H */
113