rwsem.h revision 302408
159243Sobrien/*-
259243Sobrien * Copyright (c) 2010 Isilon Systems, Inc.
359243Sobrien * Copyright (c) 2010 iX Systems, Inc.
459243Sobrien * Copyright (c) 2010 Panasas, Inc.
559243Sobrien * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
659243Sobrien * All rights reserved.
759243Sobrien *
859243Sobrien * Redistribution and use in source and binary forms, with or without
959243Sobrien * modification, are permitted provided that the following conditions
1059243Sobrien * are met:
1159243Sobrien * 1. Redistributions of source code must retain the above copyright
1259243Sobrien *    notice unmodified, this list of conditions, and the following
1359243Sobrien *    disclaimer.
1459243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1559243Sobrien *    notice, this list of conditions and the following disclaimer in the
1659243Sobrien *    documentation and/or other materials provided with the distribution.
1759243Sobrien *
1859243Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1959243Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2059243Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2159243Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2259243Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2359243Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2459243Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2559243Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2659243Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2759243Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2859243Sobrien *
2959243Sobrien * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/rwsem.h 290135 2015-10-29 08:28:39Z hselasky $
3059243Sobrien */
3159243Sobrien#ifndef	_LINUX_RWSEM_H_
3259243Sobrien#define	_LINUX_RWSEM_H_
3359243Sobrien
3459243Sobrien#include <sys/param.h>
3559243Sobrien#include <sys/lock.h>
3659243Sobrien#include <sys/sx.h>
3759243Sobrien
3859243Sobrienstruct rw_semaphore {
3959243Sobrien	struct sx sx;
4059243Sobrien};
4159243Sobrien
4259243Sobrien#define	down_write(_rw)			sx_xlock(&(_rw)->sx)
4359243Sobrien#define	up_write(_rw)			sx_xunlock(&(_rw)->sx)
4459243Sobrien#define	down_read(_rw)			sx_slock(&(_rw)->sx)
4559243Sobrien#define	up_read(_rw)			sx_sunlock(&(_rw)->sx)
4659243Sobrien#define	down_read_trylock(_rw)		!!sx_try_slock(&(_rw)->sx)
4759243Sobrien#define	down_write_trylock(_rw)		!!sx_try_xlock(&(_rw)->sx)
4859243Sobrien#define	downgrade_write(_rw)		sx_downgrade(&(_rw)->sx)
4959243Sobrien#define	down_read_nested(_rw, _sc)	down_read(_rw)
5059243Sobrien
5159243Sobrienstatic inline void
5259243Sobrieninit_rwsem(struct rw_semaphore *rw)
5359243Sobrien{
5459243Sobrien
5559243Sobrien	memset(&rw->sx, 0, sizeof(rw->sx));
5659243Sobrien	sx_init_flags(&rw->sx, "lnxrwsem", SX_NOWITNESS);
5759243Sobrien}
5859243Sobrien
5959243Sobrien#endif	/* _LINUX_RWSEM_H_ */
6059243Sobrien