xref: /illumos-gate/usr/src/lib/libc/inc/mtlib.h (revision a574db85)
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 /*
23*a574db85Sraf  * 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	int	_private_mutex_init(mutex_t *, int, void *);
417c478bd9Sstevel@tonic-gate extern	int	_private_mutex_destroy(mutex_t *);
427c478bd9Sstevel@tonic-gate extern	int	_private_mutex_lock(mutex_t *);
437c478bd9Sstevel@tonic-gate extern	int	_private_mutex_trylock(mutex_t *);
447c478bd9Sstevel@tonic-gate extern	int	_private_mutex_unlock(mutex_t *);
457c478bd9Sstevel@tonic-gate extern	void	lmutex_lock(mutex_t *);
467c478bd9Sstevel@tonic-gate extern	void	lmutex_unlock(mutex_t *);
477c478bd9Sstevel@tonic-gate extern	int	__rwlock_init(rwlock_t *, int, void *);
487c478bd9Sstevel@tonic-gate extern	int	__rwlock_destroy(rwlock_t *);
497c478bd9Sstevel@tonic-gate extern	int	__rw_rdlock(rwlock_t *);
507c478bd9Sstevel@tonic-gate extern	int	__rw_wrlock(rwlock_t *);
517c478bd9Sstevel@tonic-gate extern	int	__rw_tryrdlock(rwlock_t *);
527c478bd9Sstevel@tonic-gate extern	int	__rw_trywrlock(rwlock_t *);
537c478bd9Sstevel@tonic-gate extern	int	__rw_unlock(rwlock_t *);
547c478bd9Sstevel@tonic-gate extern	void	lrw_rdlock(rwlock_t *);
557c478bd9Sstevel@tonic-gate extern	void	lrw_wrlock(rwlock_t *);
567c478bd9Sstevel@tonic-gate extern	void	lrw_unlock(rwlock_t *);
57f841f6adSraf extern	void	sig_mutex_lock(mutex_t *);
58f841f6adSraf extern	void	sig_mutex_unlock(mutex_t *);
59f841f6adSraf extern	int	sig_mutex_trylock(mutex_t *);
60f841f6adSraf extern	int	sig_cond_wait(cond_t *, mutex_t *);
61f841f6adSraf extern	int	sig_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
62*a574db85Sraf extern	void	cancel_safe_mutex_lock(mutex_t *);
63*a574db85Sraf extern	void	cancel_safe_mutex_unlock(mutex_t *);
64*a574db85Sraf extern	int	cancel_safe_mutex_trylock(mutex_t *);
65*a574db85Sraf extern	int	cancel_active(void);
66f841f6adSraf 
67f841f6adSraf /* the private libc thread-safe allocator */
68f841f6adSraf extern	void	*lmalloc(size_t);
69f841f6adSraf extern	void	lfree(void *, size_t);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /* the rest are public functions */
727c478bd9Sstevel@tonic-gate extern	int	_mutex_init(mutex_t *, int, void *);
737c478bd9Sstevel@tonic-gate extern	int	_mutex_destroy(mutex_t *);
747c478bd9Sstevel@tonic-gate extern	int	_mutex_lock(mutex_t *);
757c478bd9Sstevel@tonic-gate extern	int	_mutex_trylock(mutex_t *);
767c478bd9Sstevel@tonic-gate extern	int	_mutex_unlock(mutex_t *);
777c478bd9Sstevel@tonic-gate extern	int	__mutex_init(mutex_t *, int, void *);
787c478bd9Sstevel@tonic-gate extern	int	__mutex_destroy(mutex_t *);
797c478bd9Sstevel@tonic-gate extern	int	__mutex_lock(mutex_t *);
807c478bd9Sstevel@tonic-gate extern	int	__mutex_trylock(mutex_t *);
817c478bd9Sstevel@tonic-gate extern	int	__mutex_unlock(mutex_t *);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate extern	int	_cond_init(cond_t *, int, void *);
847c478bd9Sstevel@tonic-gate extern	int	_cond_destroy(cond_t *);
85*a574db85Sraf extern	int	__cond_wait(cond_t *, mutex_t *);
86*a574db85Sraf extern	int	__cond_timedwait(cond_t *, mutex_t *, const timespec_t *);
87*a574db85Sraf extern	int	__cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
887c478bd9Sstevel@tonic-gate extern	int	_cond_signal(cond_t *);
897c478bd9Sstevel@tonic-gate extern	int	_cond_broadcast(cond_t *);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate extern	int	_rwlock_init(rwlock_t *, int, void *);
927c478bd9Sstevel@tonic-gate extern	int	_rwlock_destroy(rwlock_t *);
937c478bd9Sstevel@tonic-gate extern	int	_rw_rdlock(rwlock_t *);
947c478bd9Sstevel@tonic-gate extern	int	_rw_wrlock(rwlock_t *);
957c478bd9Sstevel@tonic-gate extern	int	_rw_tryrdlock(rwlock_t *);
967c478bd9Sstevel@tonic-gate extern	int	_rw_trywrlock(rwlock_t *);
977c478bd9Sstevel@tonic-gate extern	int	_rw_unlock(rwlock_t *);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate extern	int	_thr_main(void);
1007c478bd9Sstevel@tonic-gate extern	thread_t _thr_self(void);
101657b1f3dSraf extern	int	_thrp_cancelled(void);
1027c478bd9Sstevel@tonic-gate extern	void	_thr_exit(void *);
1037c478bd9Sstevel@tonic-gate extern	size_t	_thr_min_stack(void);
1047c478bd9Sstevel@tonic-gate extern	int	_thr_kill(thread_t, int);
105f841f6adSraf extern	int	_thr_create(void *, size_t, void *(*)(void *), void *, long,
106f841f6adSraf 			thread_t *);
1077c478bd9Sstevel@tonic-gate extern	int	_thr_keycreate(thread_key_t *, void (*)(void *));
108cb620785Sraf extern	int	_thr_keycreate_once(thread_key_t *, void (*)(void *));
1097c478bd9Sstevel@tonic-gate extern	int	_thr_setspecific(thread_key_t, void *);
1107c478bd9Sstevel@tonic-gate extern	int	_thr_getspecific(thread_key_t, void **);
1117c478bd9Sstevel@tonic-gate extern	void	*_pthread_getspecific(thread_key_t);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #if defined(THREAD_DEBUG)
1147c478bd9Sstevel@tonic-gate extern	void	assert_no_libc_locks_held(void);
1157c478bd9Sstevel@tonic-gate #else
1167c478bd9Sstevel@tonic-gate #define	assert_no_libc_locks_held()
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #define	_FWRITE _fwrite_unlocked
12059e63b6eSraf #define	FILENO(s) _fileno(s)
12159e63b6eSraf #define	FERROR(s) ferror(s)
1227c478bd9Sstevel@tonic-gate #define	GETC(s) _getc_unlocked(s)
1237c478bd9Sstevel@tonic-gate #define	UNGETC(c, s) _ungetc_unlocked(c, s)
1247c478bd9Sstevel@tonic-gate #define	PUTC(c, s) _putc_unlocked(c, s)
1257c478bd9Sstevel@tonic-gate #define	GETWC(s) getwc(s)
1267c478bd9Sstevel@tonic-gate #define	PUTWC(c, s) putwc(c, s)
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Cheap check to tell if stdio needs to lock for MT progs.
1307c478bd9Sstevel@tonic-gate  * Referenced directly in port/stdio/flush.c and FLOCKFILE and
1311d2738a5Sraf  * FUNLOCKFILE macros.  __libc_threaded gets set to 1 when the first
1327c478bd9Sstevel@tonic-gate  * thread (beyond the main thread) is created in _thrp_create().
1337c478bd9Sstevel@tonic-gate  */
1341d2738a5Sraf extern	int	__libc_threaded;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	FILELOCKING(iop)	(GET_IONOLOCK(iop) == 0)
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	FLOCKFILE(lk, iop) \
1397c478bd9Sstevel@tonic-gate 	{ \
1401d2738a5Sraf 		if (__libc_threaded && FILELOCKING(iop)) \
1417c478bd9Sstevel@tonic-gate 			lk = _flockget((iop)); \
1427c478bd9Sstevel@tonic-gate 		else \
1437c478bd9Sstevel@tonic-gate 			lk = NULL; \
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #define	FUNLOCKFILE(lk) \
1477c478bd9Sstevel@tonic-gate 	{ \
1487c478bd9Sstevel@tonic-gate 		if (lk != NULL) \
1497c478bd9Sstevel@tonic-gate 			_flockrel(lk); \
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #define	FLOCKRETURN(iop, ret) \
1537c478bd9Sstevel@tonic-gate 	{	int r; \
1547c478bd9Sstevel@tonic-gate 		rmutex_t *lk; \
1557c478bd9Sstevel@tonic-gate 		FLOCKFILE(lk, iop); \
1567c478bd9Sstevel@tonic-gate 		r = (ret); \
1577c478bd9Sstevel@tonic-gate 		FUNLOCKFILE(lk); \
1587c478bd9Sstevel@tonic-gate 		return (r); \
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate #endif
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate #endif	/* _MTLIB_H */
166