10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 54570Sraf * Common Development and Distribution License (the "License"). 64570Sraf * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 214570Sraf 220Sstevel@tonic-gate /* 23*13081SChris.Kiick@Sun.COM * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #ifndef _SYNCH_H 270Sstevel@tonic-gate #define _SYNCH_H 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * synch.h: 310Sstevel@tonic-gate * definitions needed to use the thread synchronization interface 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate #ifndef _ASM 350Sstevel@tonic-gate #include <sys/machlock.h> 360Sstevel@tonic-gate #include <sys/time_impl.h> 370Sstevel@tonic-gate #include <sys/synch.h> 380Sstevel@tonic-gate #endif /* _ASM */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #ifdef __cplusplus 410Sstevel@tonic-gate extern "C" { 420Sstevel@tonic-gate #endif 430Sstevel@tonic-gate 440Sstevel@tonic-gate #ifndef _ASM 450Sstevel@tonic-gate 460Sstevel@tonic-gate /* 470Sstevel@tonic-gate * Semaphores 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate typedef struct _sema { 500Sstevel@tonic-gate /* this structure must be the same as sem_t in <semaphore.h> */ 510Sstevel@tonic-gate uint32_t count; /* semaphore count */ 520Sstevel@tonic-gate uint16_t type; 530Sstevel@tonic-gate uint16_t magic; 540Sstevel@tonic-gate upad64_t pad1[3]; /* reserved for a mutex_t */ 550Sstevel@tonic-gate upad64_t pad2[2]; /* reserved for a cond_t */ 560Sstevel@tonic-gate } sema_t; 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * POSIX.1c Note: 600Sstevel@tonic-gate * POSIX.1c requires that <pthread.h> define the structures pthread_mutex_t 610Sstevel@tonic-gate * and pthread_cond_t. These structures are identical to mutex_t (lwp_mutex_t) 620Sstevel@tonic-gate * and cond_t (lwp_cond_t) which are defined in <synch.h>. A nested included 630Sstevel@tonic-gate * of <synch.h> (to allow a "#typedef mutex_t pthread_mutex_t") would pull in 640Sstevel@tonic-gate * non-posix symbols/constants violating the namespace restrictions. Hence, 650Sstevel@tonic-gate * pthread_mutex_t/pthread_cond_t have been redefined in <pthread.h> (actually 660Sstevel@tonic-gate * in <sys/types.h>). Any modifications done to mutex_t/lwp_mutex_t or 670Sstevel@tonic-gate * cond_t/lwp_cond_t should also be done to pthread_mutex_t/pthread_cond_t. 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate typedef lwp_mutex_t mutex_t; 700Sstevel@tonic-gate typedef lwp_cond_t cond_t; 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * Readers/writer locks 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * NOTE: The layout of this structure should be kept in sync with the layout 760Sstevel@tonic-gate * of the correponding structure of pthread_rwlock_t in sys/types.h. 770Sstevel@tonic-gate * Also, there is an identical structure for lwp_rwlock_t in <sys/synch.h>. 780Sstevel@tonic-gate * Because we have to deal with C++, we cannot redefine this one as that one. 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate typedef struct _rwlock { 814570Sraf int32_t readers; /* rwstate word */ 820Sstevel@tonic-gate uint16_t type; 830Sstevel@tonic-gate uint16_t magic; 844570Sraf mutex_t mutex; /* used with process-shared rwlocks */ 854570Sraf cond_t readercv; /* used only to indicate ownership */ 864570Sraf cond_t writercv; /* used only to indicate ownership */ 870Sstevel@tonic-gate } rwlock_t; 880Sstevel@tonic-gate 890Sstevel@tonic-gate #ifdef __STDC__ 900Sstevel@tonic-gate int _lwp_mutex_lock(lwp_mutex_t *); 910Sstevel@tonic-gate int _lwp_mutex_unlock(lwp_mutex_t *); 920Sstevel@tonic-gate int _lwp_mutex_trylock(lwp_mutex_t *); 930Sstevel@tonic-gate int _lwp_cond_wait(lwp_cond_t *, lwp_mutex_t *); 940Sstevel@tonic-gate int _lwp_cond_timedwait(lwp_cond_t *, lwp_mutex_t *, timespec_t *); 950Sstevel@tonic-gate int _lwp_cond_reltimedwait(lwp_cond_t *, lwp_mutex_t *, timespec_t *); 960Sstevel@tonic-gate int _lwp_cond_signal(lwp_cond_t *); 970Sstevel@tonic-gate int _lwp_cond_broadcast(lwp_cond_t *); 980Sstevel@tonic-gate int _lwp_sema_init(lwp_sema_t *, int); 990Sstevel@tonic-gate int _lwp_sema_wait(lwp_sema_t *); 1000Sstevel@tonic-gate int _lwp_sema_trywait(lwp_sema_t *); 1010Sstevel@tonic-gate int _lwp_sema_post(lwp_sema_t *); 1020Sstevel@tonic-gate int cond_init(cond_t *, int, void *); 1030Sstevel@tonic-gate int cond_destroy(cond_t *); 1040Sstevel@tonic-gate int cond_wait(cond_t *, mutex_t *); 1050Sstevel@tonic-gate int cond_timedwait(cond_t *, mutex_t *, const timespec_t *); 1060Sstevel@tonic-gate int cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *); 1070Sstevel@tonic-gate int cond_signal(cond_t *); 1080Sstevel@tonic-gate int cond_broadcast(cond_t *); 1090Sstevel@tonic-gate int mutex_init(mutex_t *, int, void *); 1100Sstevel@tonic-gate int mutex_destroy(mutex_t *); 1114574Sraf int mutex_consistent(mutex_t *); 1120Sstevel@tonic-gate int mutex_lock(mutex_t *); 1130Sstevel@tonic-gate int mutex_trylock(mutex_t *); 1140Sstevel@tonic-gate int mutex_unlock(mutex_t *); 1150Sstevel@tonic-gate int rwlock_init(rwlock_t *, int, void *); 1160Sstevel@tonic-gate int rwlock_destroy(rwlock_t *); 1170Sstevel@tonic-gate int rw_rdlock(rwlock_t *); 1180Sstevel@tonic-gate int rw_wrlock(rwlock_t *); 1190Sstevel@tonic-gate int rw_unlock(rwlock_t *); 1200Sstevel@tonic-gate int rw_tryrdlock(rwlock_t *); 1210Sstevel@tonic-gate int rw_trywrlock(rwlock_t *); 1220Sstevel@tonic-gate int sema_init(sema_t *, unsigned int, int, void *); 1230Sstevel@tonic-gate int sema_destroy(sema_t *); 1240Sstevel@tonic-gate int sema_wait(sema_t *); 1250Sstevel@tonic-gate int sema_timedwait(sema_t *, const timespec_t *); 1260Sstevel@tonic-gate int sema_reltimedwait(sema_t *, const timespec_t *); 1270Sstevel@tonic-gate int sema_post(sema_t *); 1280Sstevel@tonic-gate int sema_trywait(sema_t *); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate #else /* __STDC__ */ 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate int _lwp_mutex_lock(); 1330Sstevel@tonic-gate int _lwp_mutex_unlock(); 1340Sstevel@tonic-gate int _lwp_mutex_trylock(); 1350Sstevel@tonic-gate int _lwp_cond_wait(); 1360Sstevel@tonic-gate int _lwp_cond_timedwait(); 1370Sstevel@tonic-gate int _lwp_cond_reltimedwait(); 1380Sstevel@tonic-gate int _lwp_cond_signal(); 1390Sstevel@tonic-gate int _lwp_cond_broadcast(); 1400Sstevel@tonic-gate int _lwp_sema_init(); 1410Sstevel@tonic-gate int _lwp_sema_wait(); 1420Sstevel@tonic-gate int _lwp_sema_trywait(); 1430Sstevel@tonic-gate int _lwp_sema_post(); 1440Sstevel@tonic-gate int cond_init(); 1450Sstevel@tonic-gate int cond_destroy(); 1460Sstevel@tonic-gate int cond_wait(); 1470Sstevel@tonic-gate int cond_timedwait(); 1480Sstevel@tonic-gate int cond_reltimedwait(); 1490Sstevel@tonic-gate int cond_signal(); 1500Sstevel@tonic-gate int cond_broadcast(); 1510Sstevel@tonic-gate int mutex_init(); 1520Sstevel@tonic-gate int mutex_destroy(); 1534574Sraf int mutex_consistent(); 1540Sstevel@tonic-gate int mutex_lock(); 1550Sstevel@tonic-gate int mutex_trylock(); 1560Sstevel@tonic-gate int mutex_unlock(); 1570Sstevel@tonic-gate int rwlock_init(); 1580Sstevel@tonic-gate int rwlock_destroy(); 1590Sstevel@tonic-gate int rw_rdlock(); 1600Sstevel@tonic-gate int rw_wrlock(); 1610Sstevel@tonic-gate int rw_unlock(); 1620Sstevel@tonic-gate int rw_tryrdlock(); 1630Sstevel@tonic-gate int rw_trywrlock(); 1640Sstevel@tonic-gate int sema_init(); 1650Sstevel@tonic-gate int sema_destroy(); 1660Sstevel@tonic-gate int sema_wait(); 1670Sstevel@tonic-gate int sema_timedwait(); 1680Sstevel@tonic-gate int sema_reltimedwait(); 1690Sstevel@tonic-gate int sema_post(); 1700Sstevel@tonic-gate int sema_trywait(); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate #endif /* __STDC__ */ 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate #endif /* _ASM */ 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate /* "Magic numbers" tagging synchronization object types */ 1770Sstevel@tonic-gate #define MUTEX_MAGIC _MUTEX_MAGIC 1780Sstevel@tonic-gate #define SEMA_MAGIC _SEMA_MAGIC 1790Sstevel@tonic-gate #define COND_MAGIC _COND_MAGIC 1800Sstevel@tonic-gate #define RWL_MAGIC _RWL_MAGIC 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* 1830Sstevel@tonic-gate * POSIX.1c Note: 1840Sstevel@tonic-gate * DEFAULTMUTEX is defined same as PTHREAD_MUTEX_INITIALIZER in <pthread.h>. 1850Sstevel@tonic-gate * DEFAULTCV is defined same as PTHREAD_COND_INITIALIZER in <pthread.h>. 1860Sstevel@tonic-gate * DEFAULTRWLOCK is defined same as PTHREAD_RWLOCK_INITIALIZER in <pthread.h>. 1870Sstevel@tonic-gate * Any changes to these macros should be reflected in <pthread.h> 1880Sstevel@tonic-gate */ 1890Sstevel@tonic-gate #define DEFAULTMUTEX \ 1900Sstevel@tonic-gate {{0, 0, 0, {USYNC_THREAD}, MUTEX_MAGIC}, \ 1910Sstevel@tonic-gate {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0} 1920Sstevel@tonic-gate #define SHAREDMUTEX \ 1930Sstevel@tonic-gate {{0, 0, 0, {USYNC_PROCESS}, MUTEX_MAGIC}, \ 1940Sstevel@tonic-gate {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0} 1950Sstevel@tonic-gate #define RECURSIVEMUTEX \ 1960Sstevel@tonic-gate {{0, 0, 0, {USYNC_THREAD|LOCK_RECURSIVE}, MUTEX_MAGIC}, \ 1970Sstevel@tonic-gate {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0} 1980Sstevel@tonic-gate #define ERRORCHECKMUTEX \ 1990Sstevel@tonic-gate {{0, 0, 0, {USYNC_THREAD|LOCK_ERRORCHECK}, MUTEX_MAGIC}, \ 2000Sstevel@tonic-gate {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0} 2010Sstevel@tonic-gate #define RECURSIVE_ERRORCHECKMUTEX \ 2020Sstevel@tonic-gate {{0, 0, 0, {USYNC_THREAD|LOCK_RECURSIVE|LOCK_ERRORCHECK}, \ 2030Sstevel@tonic-gate MUTEX_MAGIC}, {{{0, 0, 0, 0, 0, 0, 0, 0}}}, 0} 2040Sstevel@tonic-gate #define DEFAULTCV \ 2050Sstevel@tonic-gate {{{0, 0, 0, 0}, USYNC_THREAD, COND_MAGIC}, 0} 2060Sstevel@tonic-gate #define SHAREDCV \ 2070Sstevel@tonic-gate {{{0, 0, 0, 0}, USYNC_PROCESS, COND_MAGIC}, 0} 2080Sstevel@tonic-gate #define DEFAULTSEMA \ 2090Sstevel@tonic-gate {0, USYNC_THREAD, SEMA_MAGIC, {0, 0, 0}, {0, 0}} 2100Sstevel@tonic-gate #define SHAREDSEMA \ 2110Sstevel@tonic-gate {0, USYNC_PROCESS, SEMA_MAGIC, {0, 0, 0}, {0, 0}} 2120Sstevel@tonic-gate #define DEFAULTRWLOCK \ 2130Sstevel@tonic-gate {0, USYNC_THREAD, RWL_MAGIC, DEFAULTMUTEX, DEFAULTCV, DEFAULTCV} 2140Sstevel@tonic-gate #define SHAREDRWLOCK \ 2150Sstevel@tonic-gate {0, USYNC_PROCESS, RWL_MAGIC, SHAREDMUTEX, SHAREDCV, SHAREDCV} 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate /* 2180Sstevel@tonic-gate * Tests on lock states. 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate #define SEMA_HELD(x) _sema_held(x) 2210Sstevel@tonic-gate #define RW_READ_HELD(x) _rw_read_held(x) 2220Sstevel@tonic-gate #define RW_WRITE_HELD(x) _rw_write_held(x) 2230Sstevel@tonic-gate #define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x)) 2240Sstevel@tonic-gate #define MUTEX_HELD(x) _mutex_held(x) 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * The following definitions are for assertions which can be checked 2280Sstevel@tonic-gate * statically by tools like lock_lint. You can also define your own 2290Sstevel@tonic-gate * run-time test for each. If you don't, we define them to 1 so that 2300Sstevel@tonic-gate * such assertions simply pass. 2310Sstevel@tonic-gate */ 2320Sstevel@tonic-gate #ifndef NO_LOCKS_HELD 2330Sstevel@tonic-gate #define NO_LOCKS_HELD 1 2340Sstevel@tonic-gate #endif 2350Sstevel@tonic-gate #ifndef NO_COMPETING_THREADS 2360Sstevel@tonic-gate #define NO_COMPETING_THREADS 1 2370Sstevel@tonic-gate #endif 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate #ifndef _ASM 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate #ifdef __STDC__ 2420Sstevel@tonic-gate 24311466SRoger.Faulkner@Sun.COM /* 24411466SRoger.Faulkner@Sun.COM * The *_held() functions apply equally well to Solaris threads 24511466SRoger.Faulkner@Sun.COM * and to Posix threads synchronization objects, but the formal 24611466SRoger.Faulkner@Sun.COM * type declarations are different, so we just declare the argument 24711466SRoger.Faulkner@Sun.COM * to each *_held() function to be a void *, expecting that they will 24811466SRoger.Faulkner@Sun.COM * be called with the proper type of argument in each case. 24911466SRoger.Faulkner@Sun.COM */ 25011466SRoger.Faulkner@Sun.COM int _sema_held(void *); /* sema_t or sem_t */ 25111466SRoger.Faulkner@Sun.COM int _rw_read_held(void *); /* rwlock_t or pthread_rwlock_t */ 25211466SRoger.Faulkner@Sun.COM int _rw_write_held(void *); /* rwlock_t or pthread_rwlock_t */ 25311466SRoger.Faulkner@Sun.COM int _mutex_held(void *); /* mutex_t or pthread_mutex_t */ 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate #else /* __STDC__ */ 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate int _sema_held(); 2580Sstevel@tonic-gate int _rw_read_held(); 2590Sstevel@tonic-gate int _rw_write_held(); 2600Sstevel@tonic-gate int _mutex_held(); 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate #endif /* __STDC__ */ 2630Sstevel@tonic-gate 264*13081SChris.Kiick@Sun.COM /* Pause API */ 265*13081SChris.Kiick@Sun.COM #ifdef __STDC__ 266*13081SChris.Kiick@Sun.COM void smt_pause(void); 267*13081SChris.Kiick@Sun.COM #else /* __STDC__ */ 268*13081SChris.Kiick@Sun.COM void smt_pause(); 269*13081SChris.Kiick@Sun.COM #endif /* __STDC__ */ 270*13081SChris.Kiick@Sun.COM 2710Sstevel@tonic-gate #endif /* _ASM */ 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate #ifdef __cplusplus 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate #endif 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate #endif /* _SYNCH_H */ 278