xref: /titanic_50/usr/src/lib/libc/inc/mtlib.h (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <thread.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
357c478bd9Sstevel@tonic-gate extern "C" {
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /* these are private to the library */
397c478bd9Sstevel@tonic-gate extern	int	primary_link_map;
407c478bd9Sstevel@tonic-gate extern	void	lmutex_lock(mutex_t *);
417c478bd9Sstevel@tonic-gate extern	void	lmutex_unlock(mutex_t *);
427c478bd9Sstevel@tonic-gate extern	void	lrw_rdlock(rwlock_t *);
437c478bd9Sstevel@tonic-gate extern	void	lrw_wrlock(rwlock_t *);
447c478bd9Sstevel@tonic-gate extern	void	lrw_unlock(rwlock_t *);
45f841f6adSraf extern	void	sig_mutex_lock(mutex_t *);
46f841f6adSraf extern	void	sig_mutex_unlock(mutex_t *);
47f841f6adSraf extern	int	sig_mutex_trylock(mutex_t *);
48f841f6adSraf extern	int	sig_cond_wait(cond_t *, mutex_t *);
49f841f6adSraf extern	int	sig_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
50a574db85Sraf extern	void	cancel_safe_mutex_lock(mutex_t *);
51a574db85Sraf extern	void	cancel_safe_mutex_unlock(mutex_t *);
52a574db85Sraf extern	int	cancel_safe_mutex_trylock(mutex_t *);
53a574db85Sraf extern	int	cancel_active(void);
54*7257d1b4Sraf extern	int	_thrp_cancelled(void);
55f841f6adSraf 
56f841f6adSraf /* the private libc thread-safe allocator */
57f841f6adSraf extern	void	*lmalloc(size_t);
58f841f6adSraf extern	void	lfree(void *, size_t);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG)
617c478bd9Sstevel@tonic-gate extern	void	assert_no_libc_locks_held(void);
627c478bd9Sstevel@tonic-gate #else
637c478bd9Sstevel@tonic-gate #define	assert_no_libc_locks_held()
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define	_FWRITE _fwrite_unlocked
6759e63b6eSraf #define	FILENO(s) _fileno(s)
6859e63b6eSraf #define	FERROR(s) ferror(s)
697c478bd9Sstevel@tonic-gate #define	GETC(s) _getc_unlocked(s)
707c478bd9Sstevel@tonic-gate #define	UNGETC(c, s) _ungetc_unlocked(c, s)
717c478bd9Sstevel@tonic-gate #define	PUTC(c, s) _putc_unlocked(c, s)
727c478bd9Sstevel@tonic-gate #define	GETWC(s) getwc(s)
737c478bd9Sstevel@tonic-gate #define	PUTWC(c, s) putwc(c, s)
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Cheap check to tell if stdio needs to lock for MT progs.
777c478bd9Sstevel@tonic-gate  * Referenced directly in port/stdio/flush.c and FLOCKFILE and
781d2738a5Sraf  * FUNLOCKFILE macros.  __libc_threaded gets set to 1 when the first
797c478bd9Sstevel@tonic-gate  * thread (beyond the main thread) is created in _thrp_create().
807c478bd9Sstevel@tonic-gate  */
811d2738a5Sraf extern	int	__libc_threaded;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	FILELOCKING(iop)	(GET_IONOLOCK(iop) == 0)
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	FLOCKFILE(lk, iop) \
867c478bd9Sstevel@tonic-gate 	{ \
871d2738a5Sraf 		if (__libc_threaded && FILELOCKING(iop)) \
887c478bd9Sstevel@tonic-gate 			lk = _flockget((iop)); \
897c478bd9Sstevel@tonic-gate 		else \
907c478bd9Sstevel@tonic-gate 			lk = NULL; \
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	FUNLOCKFILE(lk) \
947c478bd9Sstevel@tonic-gate 	{ \
957c478bd9Sstevel@tonic-gate 		if (lk != NULL) \
967c478bd9Sstevel@tonic-gate 			_flockrel(lk); \
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	FLOCKRETURN(iop, ret) \
1007c478bd9Sstevel@tonic-gate 	{	int r; \
1017c478bd9Sstevel@tonic-gate 		rmutex_t *lk; \
1027c478bd9Sstevel@tonic-gate 		FLOCKFILE(lk, iop); \
1037c478bd9Sstevel@tonic-gate 		r = (ret); \
1047c478bd9Sstevel@tonic-gate 		FUNLOCKFILE(lk); \
1057c478bd9Sstevel@tonic-gate 		return (r); \
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate #endif
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #endif	/* _MTLIB_H */
113