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