1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5271127Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28219820Sjeff */
29219820Sjeff#ifndef	_LINUX_WAIT_H_
30219820Sjeff#define	_LINUX_WAIT_H_
31219820Sjeff
32219820Sjeff#include <linux/spinlock.h>
33219820Sjeff#include <linux/sched.h>
34219820Sjeff#include <linux/list.h>
35219820Sjeff
36219820Sjeff#include <sys/param.h>
37219820Sjeff#include <sys/systm.h>
38219820Sjeff#include <sys/sleepqueue.h>
39219820Sjeff#include <sys/kernel.h>
40219820Sjeff#include <sys/proc.h>
41219820Sjeff
42219820Sjeffstruct __wait_queue_head {
43219820Sjeff	unsigned int	wchan;
44219820Sjeff};
45219820Sjefftypedef struct __wait_queue_head wait_queue_head_t;
46219820Sjeff
47219820Sjeff#define	init_waitqueue_head(x)
48219820Sjeff
49219820Sjeffstatic inline void
50219820Sjeff__wake_up(struct __wait_queue_head *q, int all)
51219820Sjeff{
52219820Sjeff	int wakeup_swapper;
53219820Sjeff	void *c;
54219820Sjeff
55219820Sjeff	c = &q->wchan;
56219820Sjeff	sleepq_lock(c);
57219820Sjeff	if (all)
58219820Sjeff		wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
59219820Sjeff	else
60219820Sjeff		wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
61219820Sjeff	sleepq_release(c);
62219820Sjeff	if (wakeup_swapper)
63219820Sjeff		kick_proc0();
64219820Sjeff}
65219820Sjeff
66219820Sjeff#define	wake_up(q)				__wake_up(q, 0)
67219820Sjeff#define	wake_up_nr(q, nr)			__wake_up(q, 1)
68219820Sjeff#define	wake_up_all(q)				__wake_up(q, 1)
69219820Sjeff#define	wake_up_interruptible(q)		__wake_up(q, 0)
70219820Sjeff#define	wake_up_interruptible_nr(q, nr)		__wake_up(q, 1)
71219820Sjeff#define	wake_up_interruptible_all(q, nr)	__wake_up(q, 1)
72219820Sjeff
73219820Sjeff#define	wait_event(q, cond)						\
74219820Sjeffdo {									\
75219820Sjeff	void *c = &(q).wchan;						\
76219820Sjeff	if (!(cond)) {							\
77219820Sjeff		for (;;) {						\
78219820Sjeff			sleepq_lock(c);					\
79219820Sjeff			if (cond) {					\
80219820Sjeff				sleepq_release(c);			\
81219820Sjeff				break;					\
82219820Sjeff			}						\
83219820Sjeff			sleepq_add(c, NULL, "completion", SLEEPQ_SLEEP, 0); \
84219820Sjeff			sleepq_wait(c, 0);				\
85219820Sjeff		}							\
86219820Sjeff	}								\
87219820Sjeff} while (0)
88219820Sjeff
89219820Sjeff#define	wait_event_interruptible(q, cond)				\
90219820Sjeff({									\
91219820Sjeff	void *c = &(q).wchan;						\
92219820Sjeff	int _error;							\
93219820Sjeff									\
94219820Sjeff	_error = 0;							\
95219820Sjeff	if (!(cond)) {							\
96219820Sjeff		for (; _error == 0;) {					\
97219820Sjeff			sleepq_lock(c);					\
98219820Sjeff			if (cond) {					\
99219820Sjeff				sleepq_release(c);			\
100219820Sjeff				break;					\
101219820Sjeff			}						\
102219820Sjeff			sleepq_add(c, NULL, "completion",		\
103219820Sjeff			    SLEEPQ_SLEEP | SLEEPQ_INTERRUPTIBLE, 0);	\
104219820Sjeff			if (sleepq_wait_sig(c, 0))			\
105219820Sjeff				_error = -ERESTARTSYS;			\
106219820Sjeff		}							\
107219820Sjeff	}								\
108219820Sjeff	-_error;							\
109219820Sjeff})
110219820Sjeff
111219820Sjeff#define	DEFINE_WAIT(x)
112219820Sjeff
113219820Sjeff#endif	/* _LINUX_WAIT_H_ */
114