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