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