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