xref: /dragonfly/sys/dev/drm/include/linux/mutex.h (revision ef50770e)
12581d4aaSFrançois Tigeot /*
2*ef50770eSFrançois Tigeot  * Copyright (c) 2014-2020 François Tigeot <ftigeot@wolfpond.org>
32581d4aaSFrançois Tigeot  * All rights reserved.
42581d4aaSFrançois Tigeot  *
52581d4aaSFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
62581d4aaSFrançois Tigeot  * modification, are permitted provided that the following conditions
72581d4aaSFrançois Tigeot  * are met:
82581d4aaSFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
92581d4aaSFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
102581d4aaSFrançois Tigeot  *    disclaimer.
112581d4aaSFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
122581d4aaSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
132581d4aaSFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
142581d4aaSFrançois Tigeot  *
152581d4aaSFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162581d4aaSFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172581d4aaSFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182581d4aaSFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192581d4aaSFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202581d4aaSFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212581d4aaSFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222581d4aaSFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232581d4aaSFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242581d4aaSFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252581d4aaSFrançois Tigeot  */
262581d4aaSFrançois Tigeot 
272581d4aaSFrançois Tigeot #ifndef _LINUX_MUTEX_H_
282581d4aaSFrançois Tigeot #define _LINUX_MUTEX_H_
292581d4aaSFrançois Tigeot 
30d6aa1cc5SFrançois Tigeot #include <asm/current.h>
31d6aa1cc5SFrançois Tigeot #include <linux/list.h>
32961a6190SFrançois Tigeot #include <linux/lockdep.h>
33d6aa1cc5SFrançois Tigeot #include <linux/atomic.h>
34c6002f72SFrançois Tigeot #include <asm/processor.h>
352581d4aaSFrançois Tigeot 
363b6a19b2SMatthew Dillon #define mutex_is_locked(lock)	(lockinuse(lock))
372581d4aaSFrançois Tigeot 
38303d234dSFrançois Tigeot #define mutex_lock(lock)	lockmgr(lock, LK_EXCLUSIVE)
39303d234dSFrançois Tigeot #define mutex_unlock(lock)	lockmgr(lock, LK_RELEASE)
40303d234dSFrançois Tigeot 
413558045eSSascha Wildner #define mutex_trylock(lock)	lockmgr_try(lock, LK_EXCLUSIVE)
428634e73aSFrançois Tigeot 
4317bbf32eSFrançois Tigeot static inline int
mutex_lock_interruptible(struct lock * lock)4417bbf32eSFrançois Tigeot mutex_lock_interruptible(struct lock *lock)
4517bbf32eSFrançois Tigeot {
46beb32878SFrançois Tigeot 	if (lockmgr(lock, LK_EXCLUSIVE|LK_SLEEPFAIL|LK_PCATCH))
4717bbf32eSFrançois Tigeot 		return -EINTR;
4817bbf32eSFrançois Tigeot 
4917bbf32eSFrançois Tigeot 	return 0;
5017bbf32eSFrançois Tigeot }
518634e73aSFrançois Tigeot 
52115eefbdSFrançois Tigeot #define DEFINE_MUTEX(mutex)	\
53115eefbdSFrançois Tigeot 	struct lock mutex;	\
54115eefbdSFrançois Tigeot 	LOCK_SYSINIT(mutex, &mutex, "lmutex", LK_CANRECURSE)
55115eefbdSFrançois Tigeot 
5639cfddd2SFrançois Tigeot static inline void
mutex_destroy(struct lock * mutex)5739cfddd2SFrançois Tigeot mutex_destroy(struct lock *mutex)
5839cfddd2SFrançois Tigeot {
5939cfddd2SFrançois Tigeot 	lockuninit(mutex);
6039cfddd2SFrançois Tigeot }
6139cfddd2SFrançois Tigeot 
62*ef50770eSFrançois Tigeot #define mutex_lock_nested(lock, unused)	mutex_lock(lock)
63*ef50770eSFrançois Tigeot 
64*ef50770eSFrançois Tigeot enum mutex_trylock_recursive_enum {
65*ef50770eSFrançois Tigeot 	MUTEX_TRYLOCK_FAILED    = 0,
66*ef50770eSFrançois Tigeot 	MUTEX_TRYLOCK_SUCCESS   = 1,
67*ef50770eSFrançois Tigeot 	MUTEX_TRYLOCK_RECURSIVE,
68*ef50770eSFrançois Tigeot };
69*ef50770eSFrançois Tigeot 
70*ef50770eSFrançois Tigeot static inline enum mutex_trylock_recursive_enum
mutex_trylock_recursive(struct lock * lock)71*ef50770eSFrançois Tigeot mutex_trylock_recursive(struct lock *lock)
72*ef50770eSFrançois Tigeot {
73*ef50770eSFrançois Tigeot 	if (lockowned(lock))
74*ef50770eSFrançois Tigeot 		return MUTEX_TRYLOCK_RECURSIVE;
75*ef50770eSFrançois Tigeot 
76*ef50770eSFrançois Tigeot 	if (mutex_trylock(lock))
77*ef50770eSFrançois Tigeot 		return MUTEX_TRYLOCK_SUCCESS;
78*ef50770eSFrançois Tigeot 
79*ef50770eSFrançois Tigeot 	return MUTEX_TRYLOCK_FAILED;
80*ef50770eSFrançois Tigeot }
81*ef50770eSFrançois Tigeot 
822581d4aaSFrançois Tigeot #endif	/* _LINUX_MUTEX_H_ */
83