Home | History | Annotate | Line # | Download | only in rumpkern
ltsleep.c revision 1.2.4.2
      1  1.2.4.2  matt /*	$NetBSD: ltsleep.c,v 1.2.4.2 2007/11/06 23:34:37 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.2  matt 
     35  1.2.4.2  matt #include "rump_private.h"
     36  1.2.4.2  matt 
     37  1.2.4.2  matt struct ltsleeper {
     38  1.2.4.2  matt 	wchan_t id;
     39  1.2.4.2  matt 	kcondvar_t cv;
     40  1.2.4.2  matt 	LIST_ENTRY(ltsleeper) entries;
     41  1.2.4.2  matt };
     42  1.2.4.2  matt 
     43  1.2.4.2  matt static LIST_HEAD(, ltsleeper) sleepers = LIST_HEAD_INITIALIZER(sleepers);
     44  1.2.4.2  matt static kmutex_t sleepermtx;
     45  1.2.4.2  matt 
     46  1.2.4.2  matt int
     47  1.2.4.2  matt ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
     48  1.2.4.2  matt 	volatile struct simplelock *slock)
     49  1.2.4.2  matt {
     50  1.2.4.2  matt 	struct ltsleeper lts;
     51  1.2.4.2  matt 
     52  1.2.4.2  matt 	lts.id = ident;
     53  1.2.4.2  matt 	cv_init(&lts.cv, NULL);
     54  1.2.4.2  matt 
     55  1.2.4.2  matt 	mutex_enter(&sleepermtx);
     56  1.2.4.2  matt 	LIST_INSERT_HEAD(&sleepers, &lts, entries);
     57  1.2.4.2  matt 	/* protected by sleepermtx */
     58  1.2.4.2  matt 	if (slock)
     59  1.2.4.2  matt 		simple_unlock(slock);
     60  1.2.4.2  matt 	cv_wait(&lts.cv, &sleepermtx);
     61  1.2.4.2  matt 	LIST_REMOVE(&lts, entries);
     62  1.2.4.2  matt 	mutex_exit(&sleepermtx);
     63  1.2.4.2  matt 
     64  1.2.4.2  matt 	cv_destroy(&lts.cv);
     65  1.2.4.2  matt 
     66  1.2.4.2  matt 	if (slock && (prio & PNORELOCK) == 0)
     67  1.2.4.2  matt 		simple_lock(slock);
     68  1.2.4.2  matt 
     69  1.2.4.2  matt 	return 0;
     70  1.2.4.2  matt }
     71  1.2.4.2  matt 
     72  1.2.4.2  matt void
     73  1.2.4.2  matt wakeup(wchan_t ident)
     74  1.2.4.2  matt {
     75  1.2.4.2  matt 	struct ltsleeper *ltsp;
     76  1.2.4.2  matt 
     77  1.2.4.2  matt 	mutex_enter(&sleepermtx);
     78  1.2.4.2  matt 	LIST_FOREACH(ltsp, &sleepers, entries)
     79  1.2.4.2  matt 		if (ltsp->id == ident)
     80  1.2.4.2  matt 			cv_signal(&ltsp->cv);
     81  1.2.4.2  matt 	mutex_exit(&sleepermtx);
     82  1.2.4.2  matt }
     83  1.2.4.2  matt 
     84  1.2.4.2  matt void
     85  1.2.4.2  matt rump_sleepers_init()
     86  1.2.4.2  matt {
     87  1.2.4.2  matt 
     88  1.2.4.2  matt 	mutex_init(&sleepermtx, MUTEX_DEFAULT, 0);
     89  1.2.4.2  matt }
     90