sx.h revision 139825
1139825Simp/*- 273782Sjasone * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>. All rights reserved. 373782Sjasone * 473782Sjasone * Redistribution and use in source and binary forms, with or without 573782Sjasone * modification, are permitted provided that the following conditions 673782Sjasone * are met: 773782Sjasone * 1. Redistributions of source code must retain the above copyright 873782Sjasone * notice(s), this list of conditions and the following disclaimer as 973782Sjasone * the first lines of this file unmodified other than the possible 1073782Sjasone * addition of one or more copyright notices. 1173782Sjasone * 2. Redistributions in binary form must reproduce the above copyright 1273782Sjasone * notice(s), this list of conditions and the following disclaimer in the 1373782Sjasone * documentation and/or other materials provided with the distribution. 1473782Sjasone * 1573782Sjasone * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 1673782Sjasone * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1773782Sjasone * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1873782Sjasone * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 1973782Sjasone * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 2073782Sjasone * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 2173782Sjasone * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 2273782Sjasone * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2373782Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2473782Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 2573782Sjasone * DAMAGE. 2673782Sjasone * 2773782Sjasone * $FreeBSD: head/sys/sys/sx.h 139825 2005-01-07 02:29:27Z imp $ 2873782Sjasone */ 2973782Sjasone 3073782Sjasone#ifndef _SYS_SX_H_ 3173782Sjasone#define _SYS_SX_H_ 3273782Sjasone 33125444Sbde#include <sys/queue.h> 3483103Sjhb#include <sys/_lock.h> 3574912Sjhb#include <sys/condvar.h> /* XXX */ 3673782Sjasone 3773782Sjasonestruct sx { 3874912Sjhb struct lock_object sx_object; /* Common lock properties. */ 3986333Sdillon struct mtx *sx_lock; /* General protection lock. */ 4073782Sjasone int sx_cnt; /* -1: xlock, > 0: slock count. */ 4173782Sjasone struct cv sx_shrd_cv; /* slock waiters. */ 4273782Sjasone int sx_shrd_wcnt; /* Number of slock waiters. */ 4373782Sjasone struct cv sx_excl_cv; /* xlock waiters. */ 4473782Sjasone int sx_excl_wcnt; /* Number of xlock waiters. */ 4583366Sjulian struct thread *sx_xholder; /* Thread presently holding xlock. */ 4673782Sjasone}; 4773782Sjasone 4873782Sjasone#ifdef _KERNEL 4993672Sarrvoid sx_sysinit(void *arg); 5073782Sjasonevoid sx_init(struct sx *sx, const char *description); 5173782Sjasonevoid sx_destroy(struct sx *sx); 5274912Sjhbvoid _sx_slock(struct sx *sx, const char *file, int line); 5374912Sjhbvoid _sx_xlock(struct sx *sx, const char *file, int line); 5478872Sjhbint _sx_try_slock(struct sx *sx, const char *file, int line); 5578872Sjhbint _sx_try_xlock(struct sx *sx, const char *file, int line); 5674912Sjhbvoid _sx_sunlock(struct sx *sx, const char *file, int line); 5774912Sjhbvoid _sx_xunlock(struct sx *sx, const char *file, int line); 5881599Sjasoneint _sx_try_upgrade(struct sx *sx, const char *file, int line); 5981599Sjasonevoid _sx_downgrade(struct sx *sx, const char *file, int line); 6085388Sjhb#ifdef INVARIANT_SUPPORT 6185388Sjhbvoid _sx_assert(struct sx *sx, int what, const char *file, int line); 6293688Sarr#endif 6393672Sarr 6493672Sarrstruct sx_args { 6593672Sarr struct sx *sa_sx; 6693672Sarr const char *sa_desc; 6793672Sarr}; 6893672Sarr 6993672Sarr#define SX_SYSINIT(name, sxa, desc) \ 7093672Sarr static struct sx_args name##_args = { \ 7193672Sarr sxa, \ 7293672Sarr desc \ 7393672Sarr }; \ 7493672Sarr SYSINIT(name##_sx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 7593672Sarr sx_sysinit, &name##_args) 7673782Sjasone 7783593Sjhb#define sx_slock(sx) _sx_slock((sx), LOCK_FILE, LOCK_LINE) 7883593Sjhb#define sx_xlock(sx) _sx_xlock((sx), LOCK_FILE, LOCK_LINE) 7983593Sjhb#define sx_try_slock(sx) _sx_try_slock((sx), LOCK_FILE, LOCK_LINE) 8083593Sjhb#define sx_try_xlock(sx) _sx_try_xlock((sx), LOCK_FILE, LOCK_LINE) 8183593Sjhb#define sx_sunlock(sx) _sx_sunlock((sx), LOCK_FILE, LOCK_LINE) 8283593Sjhb#define sx_xunlock(sx) _sx_xunlock((sx), LOCK_FILE, LOCK_LINE) 8383593Sjhb#define sx_try_upgrade(sx) _sx_try_upgrade((sx), LOCK_FILE, LOCK_LINE) 8483593Sjhb#define sx_downgrade(sx) _sx_downgrade((sx), LOCK_FILE, LOCK_LINE) 85131984Sdarrenr#define sx_unlock(sx) \ 86131984Sdarrenr do { \ 87131984Sdarrenr if ((sx)->sx_cnt < 0) \ 88131984Sdarrenr sx_xunlock(sx); \ 89131984Sdarrenr else \ 90131984Sdarrenr sx_sunlock(sx); \ 91131984Sdarrenr } while (0) 9274912Sjhb 9385412Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 9485388Sjhb#define SX_LOCKED LA_LOCKED 9585388Sjhb#define SX_SLOCKED LA_SLOCKED 9685388Sjhb#define SX_XLOCKED LA_XLOCKED 97125419Spjd#define SX_UNLOCKED LA_UNLOCKED 98125444Sbde#endif 9973863Sbmilekic 10085412Sjhb#ifdef INVARIANTS 10185388Sjhb#define sx_assert(sx, what) _sx_assert((sx), (what), LOCK_FILE, LOCK_LINE) 102125444Sbde#else 10385388Sjhb#define sx_assert(sx, what) 104125444Sbde#endif 10573782Sjasone 106125444Sbde#endif /* _KERNEL */ 107125444Sbde 108125444Sbde#endif /* !_SYS_SX_H_ */ 109