Home | History | Annotate | Line # | Download | only in drm
drm_wait_netbsd.h revision 1.1.2.3
      1 /*	$NetBSD: drm_wait_netbsd.h,v 1.1.2.3 2013/07/24 02:20:38 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _DRM_DRM_WAIT_NETBSD_H_
     33 #define _DRM_DRM_WAIT_NETBSD_H_
     34 
     35 #include <sys/condvar.h>
     36 #include <sys/mutex.h>
     37 #include <sys/systm.h>
     38 
     39 #include <linux/mutex.h>
     40 #include <linux/spinlock.h>
     41 
     42 typedef kcondvar_t drm_waitqueue_t;
     43 
     44 static inline void
     45 DRM_INIT_WAITQUEUE(drm_waitqueue_t *q, const char *name)
     46 {
     47 	cv_init(q, name);
     48 }
     49 
     50 static inline void
     51 DRM_WAKEUP_ONE(drm_waitqueue_t *q, struct mutex *interlock)
     52 {
     53 	KASSERT(mutex_is_locked(interlock));
     54 	cv_signal(q);
     55 }
     56 
     57 static inline void
     58 DRM_WAKEUP_ALL(drm_waitqueue_t *q, struct mutex *interlock)
     59 {
     60 	KASSERT(mutex_is_locked(interlock));
     61 	cv_broadcast(q);
     62 }
     63 
     64 static inline void
     65 DRM_SPIN_WAKEUP_ONE(drm_waitqueue_t *q, spinlock_t *interlock)
     66 {
     67 	KASSERT(spin_is_locked(interlock));
     68 	cv_signal(q);
     69 }
     70 
     71 static inline void
     72 DRM_SPIN_WAKEUP_ALL(drm_waitqueue_t *q, spinlock_t *interlock)
     73 {
     74 	KASSERT(spin_is_locked(interlock));
     75 	cv_broadcast(q);
     76 }
     77 
     78 #define	DRM_WAIT_UNTIL(RET, Q, INTERLOCK, CONDITION)	do		\
     79 {									\
     80 	KASSERT(mutex_is_locked((INTERLOCK)));				\
     81 	while (!(CONDITION)) {						\
     82 		/* XXX errno NetBSD->Linux */				\
     83 		(RET) = -cv_wait_sig((Q), &(INTERLOCK)->mtx_lock);	\
     84 		if (RET)						\
     85 			break;						\
     86 	}								\
     87 } while (0)
     88 
     89 #define	DRM_TIMED_WAIT_UNTIL(RET, Q, INTERLOCK, TICKS, CONDITION) do	\
     90 {									\
     91 	KASSERT(mutex_is_locked((INTERLOCK)));				\
     92 	while (!(CONDITION)) {						\
     93 		/* XXX errno NetBSD->Linux */				\
     94 		(RET) = -cv_timedwait_sig((Q), &(INTERLOCK)->mtx_lock,	\
     95 		    (TICKS));						\
     96 		if (RET)						\
     97 			break;						\
     98 	}								\
     99 } while (0)
    100 
    101 #define	DRM_SPIN_WAIT_UNTIL(RET, Q, INTERLOCK, CONDITION)	do	\
    102 {									\
    103 	KASSERT(spin_is_locked((INTERLOCK)));				\
    104 	while (!(CONDITION)) {						\
    105 		/* XXX errno NetBSD->Linux */				\
    106 		(RET) = -cv_wait_sig((Q), &(INTERLOCK)->sl_lock);	\
    107 		if (RET)						\
    108 			break;						\
    109 	}								\
    110 } while (0)
    111 
    112 #define	DRM_SPIN_TIMED_WAIT_UNTIL(RET, Q, INTERLOCK, TICKS, CONDITION)	\
    113 	do								\
    114 {									\
    115 	KASSERT(spin_is_locked((INTERLOCK)));				\
    116 	while (!(CONDITION)) {						\
    117 		/* XXX errno NetBSD->Linux */				\
    118 		(RET) = -cv_timedwait_sig((Q), &(INTERLOCK)->sl_lock,	\
    119 		    (TICKS));						\
    120 		if (RET)						\
    121 			break;						\
    122 	}								\
    123 } while (0)
    124 
    125 #endif  /* _DRM_DRM_WAIT_NETBSD_H_ */
    126