xref: /dflybsd-src/sys/dev/drm/include/linux/rwsem.h (revision 57af1c6487a09d8ff8d9927b7655670892d40cb9)
153966124SFrançois Tigeot /*
2*57af1c64SFrançois Tigeot  * Copyright (c) 2018-2020 François Tigeot <ftigeot@wolfpond.org>
353966124SFrançois Tigeot  * All rights reserved.
453966124SFrançois Tigeot  *
553966124SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
653966124SFrançois Tigeot  * modification, are permitted provided that the following conditions
753966124SFrançois Tigeot  * are met:
853966124SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
953966124SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
1053966124SFrançois Tigeot  *    disclaimer.
1153966124SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
1253966124SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
1353966124SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
1453966124SFrançois Tigeot  *
1553966124SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1653966124SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1753966124SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1853966124SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1953966124SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2053966124SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2153966124SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2253966124SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2353966124SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2453966124SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2553966124SFrançois Tigeot  */
2653966124SFrançois Tigeot 
2753966124SFrançois Tigeot #ifndef _LINUX_RWSEM_H_
2853966124SFrançois Tigeot #define _LINUX_RWSEM_H_
2953966124SFrançois Tigeot 
3053966124SFrançois Tigeot #include <linux/types.h>
3153966124SFrançois Tigeot #include <linux/kernel.h>
3253966124SFrançois Tigeot #include <linux/list.h>
3353966124SFrançois Tigeot #include <linux/spinlock.h>
3453966124SFrançois Tigeot #include <linux/atomic.h>
3553966124SFrançois Tigeot 
36*57af1c64SFrançois Tigeot #include <sys/lock.h>
37*57af1c64SFrançois Tigeot 
380757a5e5SFrançois Tigeot #define down_read(semaphore)	lockmgr((semaphore), LK_SHARED)
39eaf74c33SFrançois Tigeot #define up_read(semaphore)	lockmgr((semaphore), LK_RELEASE)
40eaf74c33SFrançois Tigeot 
4153966124SFrançois Tigeot #define down_write(semaphore)	lockmgr((semaphore), LK_EXCLUSIVE)
4253966124SFrançois Tigeot #define up_write(semaphore)	lockmgr((semaphore), LK_RELEASE)
4353966124SFrançois Tigeot 
44728ffa82SFrançois Tigeot /*
45728ffa82SFrançois Tigeot  * trylock for reading -- returns 1 if successful, 0 if contention
46728ffa82SFrançois Tigeot  */
47728ffa82SFrançois Tigeot static inline int
down_read_trylock(struct lock * sem)48728ffa82SFrançois Tigeot down_read_trylock(struct lock *sem) {
49728ffa82SFrançois Tigeot 	return !lockmgr(sem, LK_EXCLUSIVE|LK_NOWAIT);
50728ffa82SFrançois Tigeot }
51728ffa82SFrançois Tigeot 
52*57af1c64SFrançois Tigeot static inline int
down_write_killable(struct lock * sem)53*57af1c64SFrançois Tigeot down_write_killable(struct lock *sem)
54*57af1c64SFrançois Tigeot {
55*57af1c64SFrançois Tigeot 	if (lockmgr(sem, LK_EXCLUSIVE|LK_SLEEPFAIL|LK_PCATCH))
56*57af1c64SFrançois Tigeot 		return -EINTR;
57*57af1c64SFrançois Tigeot 
58*57af1c64SFrançois Tigeot 	return 0;
59*57af1c64SFrançois Tigeot }
60*57af1c64SFrançois Tigeot 
6153966124SFrançois Tigeot #endif	/* _LINUX_RWSEM_H_ */
62