sx.h revision 73900
1169695Skan/* 2169695Skan * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>. All rights reserved. 3169695Skan * 4169695Skan * Redistribution and use in source and binary forms, with or without 5169695Skan * modification, are permitted provided that the following conditions 6169695Skan * are met: 7169695Skan * 1. Redistributions of source code must retain the above copyright 8169695Skan * notice(s), this list of conditions and the following disclaimer as 9169695Skan * the first lines of this file unmodified other than the possible 10169695Skan * addition of one or more copyright notices. 11169695Skan * 2. Redistributions in binary form must reproduce the above copyright 12169695Skan * notice(s), this list of conditions and the following disclaimer in the 13169695Skan * documentation and/or other materials provided with the distribution. 14169695Skan * 15169695Skan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 16169695Skan * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17169695Skan * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18169695Skan * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 19169695Skan * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20169695Skan * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21169695Skan * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22169695Skan * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23169695Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24169695Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 25169695Skan * DAMAGE. 26169695Skan * 27169695Skan * $FreeBSD: head/sys/sys/sx.h 73900 2001-03-06 22:41:31Z jhb $ 28169695Skan */ 29169695Skan 30169695Skan#ifndef _SYS_SX_H_ 31169695Skan#define _SYS_SX_H_ 32169695Skan 33169695Skan#ifndef LOCORE 34169695Skan#include <sys/mutex.h> 35169695Skan#include <sys/condvar.h> 36169695Skan 37169695Skanstruct sx { 38169695Skan struct mtx sx_lock; /* General protection lock. */ 39169695Skan const char *sx_descr; /* sx lock description. */ 40169695Skan int sx_cnt; /* -1: xlock, > 0: slock count. */ 41169695Skan struct cv sx_shrd_cv; /* slock waiters. */ 42169695Skan int sx_shrd_wcnt; /* Number of slock waiters. */ 43169695Skan struct cv sx_excl_cv; /* xlock waiters. */ 44169695Skan int sx_excl_wcnt; /* Number of xlock waiters. */ 45169695Skan struct proc *sx_xholder; /* Thread presently holding xlock. */ 46169695Skan}; 47169695Skan 48169695Skan#ifdef _KERNEL 49169695Skanvoid sx_init(struct sx *sx, const char *description); 50169695Skanvoid sx_destroy(struct sx *sx); 51169695Skanvoid sx_slock(struct sx *sx); 52169695Skanvoid sx_xlock(struct sx *sx); 53169695Skanvoid sx_sunlock(struct sx *sx); 54169695Skanvoid sx_xunlock(struct sx *sx); 55169695Skan 56169695Skan#ifdef INVARIANTS 57169695Skan/* 58169695Skan * SX_ASSERT_SLOCKED() can only detect that at least *some* thread owns an 59169695Skan * slock, but it cannot guarantee that *this* thread owns an slock. 60169695Skan */ 61169695Skan#define SX_ASSERT_SLOCKED(sx) do { \ 62169695Skan mtx_lock(&(sx)->sx_lock); \ 63169695Skan KASSERT(((sx)->sx_cnt > 0), ("%s: lacking slock %s\n", \ 64169695Skan __FUNCTION__, (sx)->sx_descr)); \ 65169695Skan mtx_unlock(&(sx)->sx_lock); \ 66169695Skan} while (0) 67169695Skan 68169695Skan/* 69169695Skan * SX_ASSERT_XLOCKED() detects and guarantees that *we* own the xlock. 70169695Skan */ 71169695Skan#define SX_ASSERT_XLOCKED(sx) do { \ 72169695Skan mtx_lock(&(sx)->sx_lock); \ 73169695Skan KASSERT(((sx)->sx_xholder == curproc), \ 74169695Skan ("%s: thread %p lacking xlock %s\n", __FUNCTION__, \ 75169695Skan curproc, (sx)->sx_descr)); \ 76169695Skan mtx_unlock(&(sx)->sx_lock); \ 77169695Skan} while (0) 78169695Skan 79169695Skan#else /* INVARIANTS */ 80169695Skan#define SX_ASSERT_SLOCKED(sx) 81169695Skan#define SX_ASSERT_XLOCKED(sx) 82169695Skan#endif /* INVARIANTS */ 83169695Skan 84169695Skan#endif /* _KERNEL */ 85169695Skan#endif /* !LOCORE */ 86169695Skan#endif /* _SYS_SX_H_ */ 87169695Skan