completion.h revision 277213
1/*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice unmodified, this list of conditions, and the following
13 *    disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef	_LINUX_COMPLETION_H_
31#define	_LINUX_COMPLETION_H_
32
33#include <linux/errno.h>
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/sleepqueue.h>
38#include <sys/kernel.h>
39#include <sys/proc.h>
40
41struct completion {
42	unsigned int done;
43};
44
45#define	INIT_COMPLETION(c)	((c).done = 0)
46#define	init_completion(c)	((c)->done = 0)
47
48static inline void
49_complete_common(struct completion *c, int all)
50{
51	int wakeup_swapper;
52
53	sleepq_lock(c);
54	c->done++;
55	if (all)
56		wakeup_swapper = sleepq_broadcast(c, SLEEPQ_SLEEP, 0, 0);
57	else
58		wakeup_swapper = sleepq_signal(c, SLEEPQ_SLEEP, 0, 0);
59	sleepq_release(c);
60	if (wakeup_swapper)
61		kick_proc0();
62}
63
64#define	complete(c)	_complete_common(c, 0)
65#define	complete_all(c)	_complete_common(c, 1)
66
67/*
68 * Indefinite wait for done != 0 with or without signals.
69 */
70static inline long
71_wait_for_common(struct completion *c, int flags)
72{
73
74	flags |= SLEEPQ_SLEEP;
75	for (;;) {
76		sleepq_lock(c);
77		if (c->done)
78			break;
79		sleepq_add(c, NULL, "completion", flags, 0);
80		if (flags & SLEEPQ_INTERRUPTIBLE) {
81			if (sleepq_wait_sig(c, 0) != 0)
82				return (-ERESTARTSYS);
83		} else
84			sleepq_wait(c, 0);
85	}
86	c->done--;
87	sleepq_release(c);
88
89	return (0);
90}
91
92#define	wait_for_completion(c)	_wait_for_common(c, 0)
93#define	wait_for_completion_interuptible(c)				\
94	_wait_for_common(c, SLEEPQ_INTERRUPTIBLE)
95
96static inline long
97_wait_for_timeout_common(struct completion *c, long timeout, int flags)
98{
99	long end;
100
101	end = ticks + timeout;
102	flags |= SLEEPQ_SLEEP;
103	for (;;) {
104		sleepq_lock(c);
105		if (c->done)
106			break;
107		sleepq_add(c, NULL, "completion", flags, 0);
108		sleepq_release(c);
109		sleepq_set_timeout(c, end - ticks);
110		sleepq_lock(c);
111		if (flags & SLEEPQ_INTERRUPTIBLE) {
112			if (sleepq_timedwait_sig(c, 0) != 0)
113				return (-ERESTARTSYS);
114		} else
115			sleepq_timedwait(c, 0);
116	}
117	c->done--;
118	sleepq_release(c);
119	timeout = end - ticks;
120
121	return (timeout > 0 ? timeout : 1);
122}
123
124#define	wait_for_completion_timeout(c, timeout)				\
125	_wait_for_timeout_common(c, timeout, 0)
126#define	wait_for_completion_interruptible_timeout(c, timeout)		\
127	_wait_for_timeout_common(c, timeout, SLEEPQ_INTERRUPTIBLE)
128
129static inline int
130try_wait_for_completion(struct completion *c)
131{
132	int isdone;
133
134	isdone = 1;
135	sleepq_lock(c);
136	if (c->done)
137		c->done--;
138	else
139		isdone = 0;
140	sleepq_release(c);
141	return (isdone);
142}
143
144static inline int
145completion_done(struct completion *c)
146{
147	int isdone;
148
149	isdone = 1;
150	sleepq_lock(c);
151	if (c->done == 0)
152		isdone = 0;
153	sleepq_release(c);
154	return (isdone);
155}
156
157#endif	/* _LINUX_COMPLETION_H_ */
158