Home | History | Annotate | Line # | Download | only in rumpkern
ltsleep.c revision 1.3.4.2
      1  1.3.4.2  yamt /*	$NetBSD: ltsleep.c,v 1.3.4.2 2007/11/15 11:45:26 yamt Exp $	*/
      2  1.3.4.2  yamt 
      3  1.3.4.2  yamt /*
      4  1.3.4.2  yamt  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  1.3.4.2  yamt  *
      6  1.3.4.2  yamt  * Development of this software was supported by the
      7  1.3.4.2  yamt  * Finnish Cultural Foundation.
      8  1.3.4.2  yamt  *
      9  1.3.4.2  yamt  * Redistribution and use in source and binary forms, with or without
     10  1.3.4.2  yamt  * modification, are permitted provided that the following conditions
     11  1.3.4.2  yamt  * are met:
     12  1.3.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     13  1.3.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     14  1.3.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.3.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     16  1.3.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     17  1.3.4.2  yamt  *
     18  1.3.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  1.3.4.2  yamt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  1.3.4.2  yamt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  1.3.4.2  yamt  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.3.4.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.3.4.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  1.3.4.2  yamt  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.3.4.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.3.4.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.3.4.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.3.4.2  yamt  * SUCH DAMAGE.
     29  1.3.4.2  yamt  */
     30  1.3.4.2  yamt 
     31  1.3.4.2  yamt #include <sys/param.h>
     32  1.3.4.2  yamt #include <sys/proc.h>
     33  1.3.4.2  yamt #include <sys/queue.h>
     34  1.3.4.2  yamt 
     35  1.3.4.2  yamt #include "rump_private.h"
     36  1.3.4.2  yamt #include "rumpuser.h"
     37  1.3.4.2  yamt 
     38  1.3.4.2  yamt struct ltsleeper {
     39  1.3.4.2  yamt 	wchan_t id;
     40  1.3.4.2  yamt 	kcondvar_t cv;
     41  1.3.4.2  yamt 	LIST_ENTRY(ltsleeper) entries;
     42  1.3.4.2  yamt };
     43  1.3.4.2  yamt 
     44  1.3.4.2  yamt static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
     45  1.3.4.2  yamt static kmutex_t sleepermtx;
     46  1.3.4.2  yamt 
     47  1.3.4.2  yamt int
     48  1.3.4.2  yamt ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
     49  1.3.4.2  yamt 	volatile struct simplelock *slock)
     50  1.3.4.2  yamt {
     51  1.3.4.2  yamt 	struct ltsleeper lts;
     52  1.3.4.2  yamt 	int iplrecurse;
     53  1.3.4.2  yamt 
     54  1.3.4.2  yamt 	lts.id = ident;
     55  1.3.4.2  yamt 	cv_init(&lts.cv, NULL);
     56  1.3.4.2  yamt 
     57  1.3.4.2  yamt 	mutex_enter(&sleepermtx);
     58  1.3.4.2  yamt 	LIST_INSERT_HEAD(&sleepers, &lts, entries);
     59  1.3.4.2  yamt 
     60  1.3.4.2  yamt 	/* release spl */
     61  1.3.4.2  yamt 	iplrecurse = rumpuser_whatis_ipl();
     62  1.3.4.2  yamt 	while (iplrecurse--)
     63  1.3.4.2  yamt 		rumpuser_rw_exit(&rumpspl);
     64  1.3.4.2  yamt 
     65  1.3.4.2  yamt 	/* protected by sleepermtx */
     66  1.3.4.2  yamt 	if (slock)
     67  1.3.4.2  yamt 		simple_unlock(slock);
     68  1.3.4.2  yamt 	cv_wait(&lts.cv, &sleepermtx);
     69  1.3.4.2  yamt 
     70  1.3.4.2  yamt 	/* retake ipl */
     71  1.3.4.2  yamt 	iplrecurse = rumpuser_whatis_ipl();
     72  1.3.4.2  yamt 	while (iplrecurse--)
     73  1.3.4.2  yamt 		rumpuser_rw_enter(&rumpspl, 0);
     74  1.3.4.2  yamt 
     75  1.3.4.2  yamt 	LIST_REMOVE(&lts, entries);
     76  1.3.4.2  yamt 	mutex_exit(&sleepermtx);
     77  1.3.4.2  yamt 
     78  1.3.4.2  yamt 	cv_destroy(&lts.cv);
     79  1.3.4.2  yamt 
     80  1.3.4.2  yamt 	if (slock && (prio & PNORELOCK) == 0)
     81  1.3.4.2  yamt 		simple_lock(slock);
     82  1.3.4.2  yamt 
     83  1.3.4.2  yamt 	return 0;
     84  1.3.4.2  yamt }
     85  1.3.4.2  yamt 
     86  1.3.4.2  yamt void
     87  1.3.4.2  yamt wakeup(wchan_t ident)
     88  1.3.4.2  yamt {
     89  1.3.4.2  yamt 	struct ltsleeper *ltsp;
     90  1.3.4.2  yamt 
     91  1.3.4.2  yamt 	mutex_enter(&sleepermtx);
     92  1.3.4.2  yamt 	LIST_FOREACH(ltsp, &sleepers, entries)
     93  1.3.4.2  yamt 		if (ltsp->id == ident)
     94  1.3.4.2  yamt 			cv_signal(&ltsp->cv);
     95  1.3.4.2  yamt 	mutex_exit(&sleepermtx);
     96  1.3.4.2  yamt }
     97  1.3.4.2  yamt 
     98  1.3.4.2  yamt void
     99  1.3.4.2  yamt rump_sleepers_init()
    100  1.3.4.2  yamt {
    101  1.3.4.2  yamt 
    102  1.3.4.2  yamt 	mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
    103  1.3.4.2  yamt }
    104