Home | History | Annotate | Line # | Download | only in rumpkern
sleepq.c revision 1.5
      1 /*	$NetBSD: sleepq.c,v 1.5 2009/10/21 23:13:53 rmind Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: sleepq.c,v 1.5 2009/10/21 23:13:53 rmind Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/condvar.h>
     33 #include <sys/mutex.h>
     34 #include <sys/queue.h>
     35 #include <sys/sleepq.h>
     36 #include <sys/syncobj.h>
     37 
     38 #include "rump_private.h"
     39 
     40 /*
     41  * Flimsy and minimalistic sleepq implementation.  This is implemented
     42  * only for the use of callouts in kern_timeout.c.  locking etc is
     43  * completely incorrect, horrible, etc etc etc.
     44  */
     45 
     46 syncobj_t sleep_syncobj;
     47 static kcondvar_t sq_cv;
     48 
     49 void
     50 sleepq_init(sleepq_t *sq)
     51 {
     52 
     53 	TAILQ_INIT(sq);
     54 
     55 	cv_init(&sq_cv, "sleepq"); /* XXX */
     56 }
     57 
     58 void
     59 sleepq_enqueue(sleepq_t *sq, wchan_t wc, const char *wmsg, syncobj_t *sob)
     60 {
     61 	struct lwp *l = curlwp;
     62 
     63 	l->l_wchan = wc;
     64 	l->l_sleepq = sq;
     65 	TAILQ_INSERT_TAIL(sq, l, l_sleepchain);
     66 }
     67 
     68 int
     69 sleepq_block(int timo, bool catch)
     70 {
     71 	struct lwp *l = curlwp;
     72 	int error = 0;
     73 	kmutex_t *mp = l->l_mutex;
     74 	int biglocks = l->l_biglocks;
     75 
     76 	while (l->l_wchan) {
     77 		if ((error=cv_timedwait(&sq_cv, mp, timo)) == EWOULDBLOCK) {
     78 			TAILQ_REMOVE(l->l_sleepq, l, l_sleepchain);
     79 			l->l_wchan = NULL;
     80 		}
     81 	}
     82 	mutex_spin_exit(mp);
     83 
     84 	if (biglocks)
     85 		KERNEL_LOCK(biglocks, curlwp);
     86 
     87 	return error;
     88 }
     89 
     90 lwp_t *
     91 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected, kmutex_t *mp)
     92 {
     93 	struct lwp *l, *l_next;
     94 	bool found = false;
     95 
     96 	if (__predict_false(expected != -1))
     97 		panic("sleepq_wake: \"expected\" not supported");
     98 
     99 	for (l = TAILQ_FIRST(sq); l; l = l_next) {
    100 		l_next = TAILQ_NEXT(l, l_sleepchain);
    101 		if (l->l_wchan == wchan) {
    102 			found = true;
    103 			l->l_wchan = NULL;
    104 			TAILQ_REMOVE(sq, l, l_sleepchain);
    105 		}
    106 	}
    107 	if (found)
    108 		cv_broadcast(&sq_cv);
    109 
    110 	mutex_spin_exit(mp);
    111 	return NULL;
    112 }
    113 
    114 void
    115 sleepq_unsleep(struct lwp *l, bool cleanup)
    116 {
    117 
    118 	l->l_wchan = NULL;
    119 	TAILQ_REMOVE(l->l_sleepq, l, l_sleepchain);
    120 	cv_broadcast(&sq_cv);
    121 
    122 	if (cleanup) {
    123 		mutex_spin_exit(l->l_mutex);
    124 	}
    125 }
    126 
    127 /*
    128  * Thread scheduler handles priorities.  Therefore no action here.
    129  * (maybe do something if we're deperate?)
    130  */
    131 void
    132 sleepq_changepri(struct lwp *l, pri_t pri)
    133 {
    134 
    135 }
    136 
    137 void
    138 sleepq_lendpri(struct lwp *l, pri_t pri)
    139 {
    140 
    141 }
    142 
    143 struct lwp *
    144 syncobj_noowner(wchan_t wc)
    145 {
    146 
    147 	return NULL;
    148 }
    149 
    150 /*
    151  * XXX: used only by callout, therefore here.  should try to use
    152  * one in kern_lwp directly.
    153  */
    154 kmutex_t *
    155 lwp_lock_retry(struct lwp *l, kmutex_t *old)
    156 {
    157 
    158 	while (l->l_mutex != old) {
    159 		mutex_spin_exit(old);
    160 		old = l->l_mutex;
    161 		mutex_spin_enter(old);
    162 	}
    163 	return old;
    164 }
    165