xref: /illumos-gate/usr/src/head/synch.h (revision 883492d5)
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
541efec22Sraf  * Common Development and Distribution License (the "License").
641efec22Sraf  * 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  */
2141efec22Sraf 
227c478bd9Sstevel@tonic-gate /*
2341efec22Sraf  * Copyright 2007 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	_SYNCH_H
287c478bd9Sstevel@tonic-gate #define	_SYNCH_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * synch.h:
347c478bd9Sstevel@tonic-gate  * definitions needed to use the thread synchronization interface
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifndef _ASM
387c478bd9Sstevel@tonic-gate #include <sys/machlock.h>
397c478bd9Sstevel@tonic-gate #include <sys/time_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/synch.h>
417c478bd9Sstevel@tonic-gate #endif /* _ASM */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifdef __cplusplus
447c478bd9Sstevel@tonic-gate extern "C" {
457c478bd9Sstevel@tonic-gate #endif
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #ifndef _ASM
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Semaphores
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate typedef struct _sema {
537c478bd9Sstevel@tonic-gate 	/* this structure must be the same as sem_t in <semaphore.h> */
547c478bd9Sstevel@tonic-gate 	uint32_t	count;		/* semaphore count */
557c478bd9Sstevel@tonic-gate 	uint16_t	type;
567c478bd9Sstevel@tonic-gate 	uint16_t	magic;
577c478bd9Sstevel@tonic-gate 	upad64_t	pad1[3];	/* reserved for a mutex_t */
587c478bd9Sstevel@tonic-gate 	upad64_t 	pad2[2];	/* reserved for a cond_t */
597c478bd9Sstevel@tonic-gate } sema_t;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * POSIX.1c Note:
637c478bd9Sstevel@tonic-gate  * POSIX.1c requires that <pthread.h> define the structures pthread_mutex_t
647c478bd9Sstevel@tonic-gate  * and pthread_cond_t.  These structures are identical to mutex_t (lwp_mutex_t)
657c478bd9Sstevel@tonic-gate  * and cond_t (lwp_cond_t) which are defined in <synch.h>.  A nested included
667c478bd9Sstevel@tonic-gate  * of <synch.h> (to allow a "#typedef mutex_t  pthread_mutex_t") would pull in
677c478bd9Sstevel@tonic-gate  * non-posix symbols/constants violating the namespace restrictions.  Hence,
687c478bd9Sstevel@tonic-gate  * pthread_mutex_t/pthread_cond_t have been redefined in <pthread.h> (actually
697c478bd9Sstevel@tonic-gate  * in <sys/types.h>).  Any modifications done to mutex_t/lwp_mutex_t or
707c478bd9Sstevel@tonic-gate  * cond_t/lwp_cond_t should also be done to pthread_mutex_t/pthread_cond_t.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate typedef lwp_mutex_t mutex_t;
737c478bd9Sstevel@tonic-gate typedef lwp_cond_t cond_t;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Readers/writer locks
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * NOTE: The layout of this structure should be kept in sync with the layout
797c478bd9Sstevel@tonic-gate  * of the correponding structure of pthread_rwlock_t in sys/types.h.
807c478bd9Sstevel@tonic-gate  * Also, there is an identical structure for lwp_rwlock_t in <sys/synch.h>.
817c478bd9Sstevel@tonic-gate  * Because we have to deal with C++, we cannot redefine this one as that one.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate typedef struct _rwlock {
8441efec22Sraf 	int32_t		readers;	/* rwstate word */
857c478bd9Sstevel@tonic-gate 	uint16_t	type;
867c478bd9Sstevel@tonic-gate 	uint16_t	magic;
8741efec22Sraf 	mutex_t		mutex;		/* used with process-shared rwlocks */
8841efec22Sraf 	cond_t		readercv;	/* used only to indicate ownership */
8941efec22Sraf 	cond_t		writercv;	/* used only to indicate ownership */
907c478bd9Sstevel@tonic-gate } rwlock_t;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef	__STDC__
937c478bd9Sstevel@tonic-gate int	_lwp_mutex_lock(lwp_mutex_t *);
947c478bd9Sstevel@tonic-gate int	_lwp_mutex_unlock(lwp_mutex_t *);
957c478bd9Sstevel@tonic-gate int	_lwp_mutex_trylock(lwp_mutex_t *);
967c478bd9Sstevel@tonic-gate int	_lwp_cond_wait(lwp_cond_t *, lwp_mutex_t *);
977c478bd9Sstevel@tonic-gate int	_lwp_cond_timedwait(lwp_cond_t *, lwp_mutex_t *, timespec_t *);
987c478bd9Sstevel@tonic-gate int	_lwp_cond_reltimedwait(lwp_cond_t *, lwp_mutex_t *, timespec_t *);
997c478bd9Sstevel@tonic-gate int	_lwp_cond_signal(lwp_cond_t *);
1007c478bd9Sstevel@tonic-gate int	_lwp_cond_broadcast(lwp_cond_t *);
1017c478bd9Sstevel@tonic-gate int	_lwp_sema_init(lwp_sema_t *, int);
1027c478bd9Sstevel@tonic-gate int	_lwp_sema_wait(lwp_sema_t *);
1037c478bd9Sstevel@tonic-gate int	_lwp_sema_trywait(lwp_sema_t *);
1047c478bd9Sstevel@tonic-gate int	_lwp_sema_post(lwp_sema_t *);
1057c478bd9Sstevel@tonic-gate int	cond_init(cond_t *, int, void *);
1067c478bd9Sstevel@tonic-gate int	cond_destroy(cond_t *);
1077c478bd9Sstevel@tonic-gate int	cond_wait(cond_t *, mutex_t *);
1087c478bd9Sstevel@tonic-gate int	cond_timedwait(cond_t *, mutex_t *, const timespec_t *);
1097c478bd9Sstevel@tonic-gate int	cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *);
1107c478bd9Sstevel@tonic-gate int	cond_signal(cond_t *);
1117c478bd9Sstevel@tonic-gate int	cond_broadcast(cond_t *);
1127c478bd9Sstevel@tonic-gate int	mutex_init(mutex_t *, int, void *);
1137c478bd9Sstevel@tonic-gate int	mutex_destroy(mutex_t *);
114*883492d5Sraf int	mutex_consistent(mutex_t *);
1157c478bd9Sstevel@tonic-gate int	mutex_lock(mutex_t *);
1167c478bd9Sstevel@tonic-gate int	mutex_trylock(mutex_t *);
1177c478bd9Sstevel@tonic-gate int	mutex_unlock(mutex_t *);
1187c478bd9Sstevel@tonic-gate int	rwlock_init(rwlock_t *, int, void *);
1197c478bd9Sstevel@tonic-gate int	rwlock_destroy(rwlock_t *);
1207c478bd9Sstevel@tonic-gate int	rw_rdlock(rwlock_t *);
1217c478bd9Sstevel@tonic-gate int	rw_wrlock(rwlock_t *);
1227c478bd9Sstevel@tonic-gate int	rw_unlock(rwlock_t *);
1237c478bd9Sstevel@tonic-gate int	rw_tryrdlock(rwlock_t *);
1247c478bd9Sstevel@tonic-gate int	rw_trywrlock(rwlock_t *);
1257c478bd9Sstevel@tonic-gate int	sema_init(sema_t *, unsigned int, int, void *);
1267c478bd9Sstevel@tonic-gate int	sema_destroy(sema_t *);
1277c478bd9Sstevel@tonic-gate int	sema_wait(sema_t *);
1287c478bd9Sstevel@tonic-gate int	sema_timedwait(sema_t *, const timespec_t *);
1297c478bd9Sstevel@tonic-gate int	sema_reltimedwait(sema_t *, const timespec_t *);
1307c478bd9Sstevel@tonic-gate int	sema_post(sema_t *);
1317c478bd9Sstevel@tonic-gate int	sema_trywait(sema_t *);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int	_lwp_mutex_lock();
1367c478bd9Sstevel@tonic-gate int	_lwp_mutex_unlock();
1377c478bd9Sstevel@tonic-gate int	_lwp_mutex_trylock();
1387c478bd9Sstevel@tonic-gate int	_lwp_cond_wait();
1397c478bd9Sstevel@tonic-gate int	_lwp_cond_timedwait();
1407c478bd9Sstevel@tonic-gate int	_lwp_cond_reltimedwait();
1417c478bd9Sstevel@tonic-gate int	_lwp_cond_signal();
1427c478bd9Sstevel@tonic-gate int	_lwp_cond_broadcast();
1437c478bd9Sstevel@tonic-gate int	_lwp_sema_init();
1447c478bd9Sstevel@tonic-gate int	_lwp_sema_wait();
1457c478bd9Sstevel@tonic-gate int	_lwp_sema_trywait();
1467c478bd9Sstevel@tonic-gate int	_lwp_sema_post();
1477c478bd9Sstevel@tonic-gate int	cond_init();
1487c478bd9Sstevel@tonic-gate int	cond_destroy();
1497c478bd9Sstevel@tonic-gate int	cond_wait();
1507c478bd9Sstevel@tonic-gate int	cond_timedwait();
1517c478bd9Sstevel@tonic-gate int	cond_reltimedwait();
1527c478bd9Sstevel@tonic-gate int	cond_signal();
1537c478bd9Sstevel@tonic-gate int	cond_broadcast();
1547c478bd9Sstevel@tonic-gate int	mutex_init();
1557c478bd9Sstevel@tonic-gate int	mutex_destroy();
156*883492d5Sraf int	mutex_consistent();
1577c478bd9Sstevel@tonic-gate int	mutex_lock();
1587c478bd9Sstevel@tonic-gate int	mutex_trylock();
1597c478bd9Sstevel@tonic-gate int	mutex_unlock();
1607c478bd9Sstevel@tonic-gate int	rwlock_init();
1617c478bd9Sstevel@tonic-gate int	rwlock_destroy();
1627c478bd9Sstevel@tonic-gate int	rw_rdlock();
1637c478bd9Sstevel@tonic-gate int	rw_wrlock();
1647c478bd9Sstevel@tonic-gate int	rw_unlock();
1657c478bd9Sstevel@tonic-gate int	rw_tryrdlock();
1667c478bd9Sstevel@tonic-gate int	rw_trywrlock();
1677c478bd9Sstevel@tonic-gate int	sema_init();
1687c478bd9Sstevel@tonic-gate int	sema_destroy();
1697c478bd9Sstevel@tonic-gate int	sema_wait();
1707c478bd9Sstevel@tonic-gate int	sema_timedwait();
1717c478bd9Sstevel@tonic-gate int	sema_reltimedwait();
1727c478bd9Sstevel@tonic-gate int	sema_post();
1737c478bd9Sstevel@tonic-gate int	sema_trywait();
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate #endif /* _ASM */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /* "Magic numbers" tagging synchronization object types */
1807c478bd9Sstevel@tonic-gate #define	MUTEX_MAGIC	_MUTEX_MAGIC
1817c478bd9Sstevel@tonic-gate #define	SEMA_MAGIC	_SEMA_MAGIC
1827c478bd9Sstevel@tonic-gate #define	COND_MAGIC	_COND_MAGIC
1837c478bd9Sstevel@tonic-gate #define	RWL_MAGIC	_RWL_MAGIC
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * POSIX.1c Note:
1877c478bd9Sstevel@tonic-gate  * DEFAULTMUTEX is defined same as PTHREAD_MUTEX_INITIALIZER in <pthread.h>.
1887c478bd9Sstevel@tonic-gate  * DEFAULTCV is defined same as PTHREAD_COND_INITIALIZER in <pthread.h>.
1897c478bd9Sstevel@tonic-gate  * DEFAULTRWLOCK is defined same as PTHREAD_RWLOCK_INITIALIZER in <pthread.h>.
1907c478bd9Sstevel@tonic-gate  * Any changes to these macros should be reflected in <pthread.h>
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate #define	DEFAULTMUTEX	\
1937c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_THREAD}, MUTEX_MAGIC}, \
1947c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
1957c478bd9Sstevel@tonic-gate #define	SHAREDMUTEX	\
1967c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_PROCESS}, MUTEX_MAGIC}, \
1977c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
1987c478bd9Sstevel@tonic-gate #define	RECURSIVEMUTEX	\
1997c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_THREAD|LOCK_RECURSIVE}, MUTEX_MAGIC}, \
2007c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
2017c478bd9Sstevel@tonic-gate #define	ERRORCHECKMUTEX	\
2027c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_THREAD|LOCK_ERRORCHECK}, MUTEX_MAGIC}, \
2037c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
2047c478bd9Sstevel@tonic-gate #define	RECURSIVE_ERRORCHECKMUTEX	\
2057c478bd9Sstevel@tonic-gate 	{{0, 0, 0, {USYNC_THREAD|LOCK_RECURSIVE|LOCK_ERRORCHECK}, \
2067c478bd9Sstevel@tonic-gate 	MUTEX_MAGIC}, {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0}
2077c478bd9Sstevel@tonic-gate #define	DEFAULTCV	\
2087c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0}, USYNC_THREAD, COND_MAGIC}, 0}
2097c478bd9Sstevel@tonic-gate #define	SHAREDCV	\
2107c478bd9Sstevel@tonic-gate 	{{{0, 0, 0, 0}, USYNC_PROCESS, COND_MAGIC}, 0}
2117c478bd9Sstevel@tonic-gate #define	DEFAULTSEMA	\
2127c478bd9Sstevel@tonic-gate 	{0, USYNC_THREAD, SEMA_MAGIC, {0, 0, 0}, {0, 0}}
2137c478bd9Sstevel@tonic-gate #define	SHAREDSEMA	\
2147c478bd9Sstevel@tonic-gate 	{0, USYNC_PROCESS, SEMA_MAGIC, {0, 0, 0}, {0, 0}}
2157c478bd9Sstevel@tonic-gate #define	DEFAULTRWLOCK	\
2167c478bd9Sstevel@tonic-gate 	{0, USYNC_THREAD, RWL_MAGIC, DEFAULTMUTEX, DEFAULTCV, DEFAULTCV}
2177c478bd9Sstevel@tonic-gate #define	SHAREDRWLOCK	\
2187c478bd9Sstevel@tonic-gate 	{0, USYNC_PROCESS, RWL_MAGIC, SHAREDMUTEX, SHAREDCV, SHAREDCV}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Tests on lock states.
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate #define	SEMA_HELD(x)		_sema_held(x)
2247c478bd9Sstevel@tonic-gate #define	RW_READ_HELD(x)		_rw_read_held(x)
2257c478bd9Sstevel@tonic-gate #define	RW_WRITE_HELD(x)	_rw_write_held(x)
2267c478bd9Sstevel@tonic-gate #define	RW_LOCK_HELD(x)		(RW_READ_HELD(x) || RW_WRITE_HELD(x))
2277c478bd9Sstevel@tonic-gate #define	MUTEX_HELD(x)		_mutex_held(x)
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * The following definitions are for assertions which can be checked
2317c478bd9Sstevel@tonic-gate  * statically by tools like lock_lint.  You can also define your own
2327c478bd9Sstevel@tonic-gate  * run-time test for each.  If you don't, we define them to 1 so that
2337c478bd9Sstevel@tonic-gate  * such assertions simply pass.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate #ifndef NO_LOCKS_HELD
2367c478bd9Sstevel@tonic-gate #define	NO_LOCKS_HELD	1
2377c478bd9Sstevel@tonic-gate #endif
2387c478bd9Sstevel@tonic-gate #ifndef NO_COMPETING_THREADS
2397c478bd9Sstevel@tonic-gate #define	NO_COMPETING_THREADS	1
2407c478bd9Sstevel@tonic-gate #endif
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate #ifndef _ASM
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate #ifdef	__STDC__
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate int _sema_held(sema_t *);
2477c478bd9Sstevel@tonic-gate int _rw_read_held(rwlock_t *);
2487c478bd9Sstevel@tonic-gate int _rw_write_held(rwlock_t *);
2497c478bd9Sstevel@tonic-gate int _mutex_held(mutex_t *);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate int _sema_held();
2547c478bd9Sstevel@tonic-gate int _rw_read_held();
2557c478bd9Sstevel@tonic-gate int _rw_write_held();
2567c478bd9Sstevel@tonic-gate int _mutex_held();
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate #endif /* _ASM */
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate #endif
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate #endif	/* _SYNCH_H */
267