xref: /netbsd-src/share/examples/puffs/pgfs/pgfs_waitq.c (revision 49413e37758aefe75effb0f891d65c98cc944d13)
1*49413e37Syamt /*	$NetBSD: pgfs_waitq.c,v 1.1 2011/10/12 01:05:00 yamt Exp $	*/
2*49413e37Syamt 
3*49413e37Syamt /*-
4*49413e37Syamt  * Copyright (c)2010,2011 YAMAMOTO Takashi,
5*49413e37Syamt  * All rights reserved.
6*49413e37Syamt  *
7*49413e37Syamt  * Redistribution and use in source and binary forms, with or without
8*49413e37Syamt  * modification, are permitted provided that the following conditions
9*49413e37Syamt  * are met:
10*49413e37Syamt  * 1. Redistributions of source code must retain the above copyright
11*49413e37Syamt  *    notice, this list of conditions and the following disclaimer.
12*49413e37Syamt  * 2. Redistributions in binary form must reproduce the above copyright
13*49413e37Syamt  *    notice, this list of conditions and the following disclaimer in the
14*49413e37Syamt  *    documentation and/or other materials provided with the distribution.
15*49413e37Syamt  *
16*49413e37Syamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*49413e37Syamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*49413e37Syamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*49413e37Syamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*49413e37Syamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*49413e37Syamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*49413e37Syamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*49413e37Syamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*49413e37Syamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*49413e37Syamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*49413e37Syamt  * SUCH DAMAGE.
27*49413e37Syamt  */
28*49413e37Syamt 
29*49413e37Syamt /*
30*49413e37Syamt  * a dumb wait queue on puffs_cc
31*49413e37Syamt  */
32*49413e37Syamt 
33*49413e37Syamt #include <sys/cdefs.h>
34*49413e37Syamt #ifndef lint
35*49413e37Syamt __RCSID("$NetBSD: pgfs_waitq.c,v 1.1 2011/10/12 01:05:00 yamt Exp $");
36*49413e37Syamt #endif /* not lint */
37*49413e37Syamt 
38*49413e37Syamt #include <assert.h>
39*49413e37Syamt #include <puffs.h>
40*49413e37Syamt 
41*49413e37Syamt #include "pgfs_waitq.h"
42*49413e37Syamt #include "pgfs_debug.h"
43*49413e37Syamt 
44*49413e37Syamt struct waiter {
45*49413e37Syamt 	TAILQ_ENTRY(waiter) list;
46*49413e37Syamt 	struct puffs_cc *cc;
47*49413e37Syamt };
48*49413e37Syamt 
49*49413e37Syamt void
waitq_init(struct waitq * wq)50*49413e37Syamt waitq_init(struct waitq *wq)
51*49413e37Syamt {
52*49413e37Syamt 
53*49413e37Syamt 	TAILQ_INIT(wq);
54*49413e37Syamt }
55*49413e37Syamt 
56*49413e37Syamt void
waiton(struct waitq * wq,struct puffs_cc * cc)57*49413e37Syamt waiton(struct waitq *wq, struct puffs_cc *cc)
58*49413e37Syamt {
59*49413e37Syamt 	struct waiter w;
60*49413e37Syamt 
61*49413e37Syamt 	/*
62*49413e37Syamt 	 * insert to the tail of the queue.
63*49413e37Syamt 	 * some users rely on the FIFO behaviour.
64*49413e37Syamt 	 */
65*49413e37Syamt 	w.cc = cc;
66*49413e37Syamt 	TAILQ_INSERT_TAIL(wq, &w, list);
67*49413e37Syamt 	puffs_cc_yield(cc);
68*49413e37Syamt }
69*49413e37Syamt 
70*49413e37Syamt struct puffs_cc *
wakeup_one(struct waitq * wq)71*49413e37Syamt wakeup_one(struct waitq *wq)
72*49413e37Syamt {
73*49413e37Syamt 
74*49413e37Syamt 	struct waiter *w;
75*49413e37Syamt 
76*49413e37Syamt 	w = TAILQ_FIRST(wq);
77*49413e37Syamt 	if (w != NULL) {
78*49413e37Syamt 		TAILQ_REMOVE(wq, w, list);
79*49413e37Syamt 		puffs_cc_schedule(w->cc);
80*49413e37Syamt 		DPRINTF("waking up %p\n", w->cc);
81*49413e37Syamt 		return w->cc;
82*49413e37Syamt 	}
83*49413e37Syamt 	return NULL;
84*49413e37Syamt }
85*49413e37Syamt 
86*49413e37Syamt #if 0
87*49413e37Syamt static void
88*49413e37Syamt wakeup(struct waitq *wq)
89*49413e37Syamt {
90*49413e37Syamt 
91*49413e37Syamt 	struct waiter *w;
92*49413e37Syamt 
93*49413e37Syamt 	TAILQ_FOREACH(w, wq, list) {
94*49413e37Syamt 		puffs_cc_schedule(w->cc);
95*49413e37Syamt 		DPRINTF("waking up %p\n", w->cc);
96*49413e37Syamt 	}
97*49413e37Syamt 	TAILQ_INIT(wq);
98*49413e37Syamt }
99*49413e37Syamt #endif
100