xref: /dragonfly/sys/dev/drm/include/linux/mutex.h (revision 3b6a19b2)
12581d4aaSFrançois Tigeot /*
2303d234dSFrançois Tigeot  * Copyright (c) 2014-2015 François Tigeot
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 
302581d4aaSFrançois Tigeot #include <sys/lock.h>
31961a6190SFrançois Tigeot #include <linux/lockdep.h>
322581d4aaSFrançois Tigeot 
33*3b6a19b2SMatthew Dillon #define mutex_is_locked(lock)	(lockinuse(lock))
342581d4aaSFrançois Tigeot 
35303d234dSFrançois Tigeot #define mutex_lock(lock)	lockmgr(lock, LK_EXCLUSIVE)
36303d234dSFrançois Tigeot #define mutex_unlock(lock)	lockmgr(lock, LK_RELEASE)
37303d234dSFrançois Tigeot 
381186f36aSMatthew Dillon /* NOTE: mutex_trylock() returns non-zero on success */
391186f36aSMatthew Dillon #define mutex_trylock(lock)	(!lockmgr(lock, LK_EXCLUSIVE|LK_NOWAIT))
408634e73aSFrançois Tigeot 
4117bbf32eSFrançois Tigeot static inline int
4217bbf32eSFrançois Tigeot mutex_lock_interruptible(struct lock *lock)
4317bbf32eSFrançois Tigeot {
44beb32878SFrançois Tigeot 	if (lockmgr(lock, LK_EXCLUSIVE|LK_SLEEPFAIL|LK_PCATCH))
4517bbf32eSFrançois Tigeot 		return -EINTR;
4617bbf32eSFrançois Tigeot 
4717bbf32eSFrançois Tigeot 	return 0;
4817bbf32eSFrançois Tigeot }
498634e73aSFrançois Tigeot 
50115eefbdSFrançois Tigeot #define DEFINE_MUTEX(mutex)	\
51115eefbdSFrançois Tigeot 	struct lock mutex;	\
52115eefbdSFrançois Tigeot 	LOCK_SYSINIT(mutex, &mutex, "lmutex", LK_CANRECURSE)
53115eefbdSFrançois Tigeot 
542581d4aaSFrançois Tigeot #endif	/* _LINUX_MUTEX_H_ */
55