xref: /illumos-gate/usr/src/lib/libc/inc/mtlib.h (revision d7269947)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51d2738a5Sraf  * Common Development and Distribution License (the "License").
61d2738a5Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2159e63b6eSraf 
227c478bd9Sstevel@tonic-gate /*
23a574db85Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef	_MTLIB_H
287c478bd9Sstevel@tonic-gate #define	_MTLIB_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <thread.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
337c478bd9Sstevel@tonic-gate extern "C" {
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* these are private to the library */
377c478bd9Sstevel@tonic-gate extern	int	primary_link_map;
387c478bd9Sstevel@tonic-gate extern	void	lmutex_lock(mutex_t *);
397c478bd9Sstevel@tonic-gate extern	void	lmutex_unlock(mutex_t *);
407c478bd9Sstevel@tonic-gate extern	void	lrw_rdlock(rwlock_t *);
417c478bd9Sstevel@tonic-gate extern	void	lrw_wrlock(rwlock_t *);
427c478bd9Sstevel@tonic-gate extern	void	lrw_unlock(rwlock_t *);
43f841f6adSraf extern	void	sig_mutex_lock(mutex_t *);
44f841f6adSraf extern	void	sig_mutex_unlock(mutex_t *);
45f841f6adSraf extern	int	sig_mutex_trylock(mutex_t *);
46f841f6adSraf extern	int	sig_cond_wait(cond_t *, mutex_t *);
47f841f6adSraf extern	int	sig_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
48a574db85Sraf extern	void	cancel_safe_mutex_lock(mutex_t *);
49a574db85Sraf extern	void	cancel_safe_mutex_unlock(mutex_t *);
50a574db85Sraf extern	int	cancel_safe_mutex_trylock(mutex_t *);
51a574db85Sraf extern	int	cancel_active(void);
527257d1b4Sraf extern	int	_thrp_cancelled(void);
53f841f6adSraf 
54f841f6adSraf /* the private libc thread-safe allocator */
55f841f6adSraf extern	void	*lmalloc(size_t);
56f841f6adSraf extern	void	lfree(void *, size_t);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG)
597c478bd9Sstevel@tonic-gate extern	void	assert_no_libc_locks_held(void);
607c478bd9Sstevel@tonic-gate #else
617c478bd9Sstevel@tonic-gate #define	assert_no_libc_locks_held()
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	_FWRITE _fwrite_unlocked
6559e63b6eSraf #define	FILENO(s) _fileno(s)
6659e63b6eSraf #define	FERROR(s) ferror(s)
67*d7269947SRobert Mustacchi #define	GETC(s) _getc_internal(s)
687c478bd9Sstevel@tonic-gate #define	UNGETC(c, s) _ungetc_unlocked(c, s)
69*d7269947SRobert Mustacchi #define	PUTC(c, s) _putc_internal(c, s)
707c478bd9Sstevel@tonic-gate #define	GETWC(s) getwc(s)
717c478bd9Sstevel@tonic-gate #define	PUTWC(c, s) putwc(c, s)
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Cheap check to tell if stdio needs to lock for MT progs.
757c478bd9Sstevel@tonic-gate  * Referenced directly in port/stdio/flush.c and FLOCKFILE and
761d2738a5Sraf  * FUNLOCKFILE macros.  __libc_threaded gets set to 1 when the first
777c478bd9Sstevel@tonic-gate  * thread (beyond the main thread) is created in _thrp_create().
787c478bd9Sstevel@tonic-gate  */
791d2738a5Sraf extern	int	__libc_threaded;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	FILELOCKING(iop)	(GET_IONOLOCK(iop) == 0)
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	FLOCKFILE(lk, iop) \
847c478bd9Sstevel@tonic-gate 	{ \
851d2738a5Sraf 		if (__libc_threaded && FILELOCKING(iop)) \
867c478bd9Sstevel@tonic-gate 			lk = _flockget((iop)); \
877c478bd9Sstevel@tonic-gate 		else \
887c478bd9Sstevel@tonic-gate 			lk = NULL; \
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	FUNLOCKFILE(lk) \
927c478bd9Sstevel@tonic-gate 	{ \
937c478bd9Sstevel@tonic-gate 		if (lk != NULL) \
947c478bd9Sstevel@tonic-gate 			_flockrel(lk); \
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	FLOCKRETURN(iop, ret) \
987c478bd9Sstevel@tonic-gate 	{	int r; \
997c478bd9Sstevel@tonic-gate 		rmutex_t *lk; \
1007c478bd9Sstevel@tonic-gate 		FLOCKFILE(lk, iop); \
1017c478bd9Sstevel@tonic-gate 		r = (ret); \
1027c478bd9Sstevel@tonic-gate 		FUNLOCKFILE(lk); \
1037c478bd9Sstevel@tonic-gate 		return (r); \
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate #endif
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #endif	/* _MTLIB_H */
111