Home | History | Annotate | Line # | Download | only in rumpkern
sleepq.c revision 1.2.6.2
      1 /*	$NetBSD: sleepq.c,v 1.2.6.2 2009/05/04 08:14:30 yamt 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.2.6.2 2009/05/04 08:14:30 yamt 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 /*
     39  * Flimsy and minimalistic sleepq implementation.  This is implemented
     40  * only for the use of callouts in kern_timeout.c.  locking etc is
     41  * completely incorrect, horrible, etc etc etc.
     42  */
     43 
     44 syncobj_t sleep_syncobj;
     45 static kcondvar_t sq_cv;
     46 static kmutex_t sq_mtx;
     47 
     48 void
     49 sleepq_init(sleepq_t *sq)
     50 {
     51 
     52 	TAILQ_INIT(sq);
     53 
     54 	cv_init(&sq_cv, "sleepq"); /* XXX */
     55 	mutex_init(&sq_mtx, MUTEX_DEFAULT, IPL_NONE); /* multi-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 	if (__predict_false(sob != &sleep_syncobj || strcmp(wmsg, "callout"))) {
     64 		panic("sleepq: unsupported enqueue");
     65 	}
     66 
     67 	l->l_wchan = wc;
     68 	TAILQ_INSERT_TAIL(sq, l, l_sleepchain);
     69 }
     70 
     71 int
     72 sleepq_block(int timo, bool hatch)
     73 {
     74 	struct lwp *l = curlwp;
     75 
     76 	KASSERT(timo == 0 && !hatch);
     77 
     78 	mutex_enter(&sq_mtx);
     79 	while (l->l_wchan)
     80 		cv_wait(&sq_cv, &sq_mtx);
     81 	mutex_exit(&sq_mtx);
     82 
     83 	return 0;
     84 }
     85 
     86 lwp_t *
     87 sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected, kmutex_t *mp)
     88 {
     89 	struct lwp *l;
     90 	bool found = false;
     91 
     92 	TAILQ_FOREACH(l, sq, l_sleepchain) {
     93 		if (l->l_wchan == wchan) {
     94 			found = true;
     95 			l->l_wchan = NULL;
     96 		}
     97 	}
     98 	if (found)
     99 		cv_broadcast(&sq_cv);
    100 
    101 	mutex_spin_exit(mp);
    102 	return NULL;
    103 }
    104 
    105 /*
    106  * XXX: used only by callout, therefore here
    107  *
    108  * We don't fudge around with the lwp mutex at all, therefore
    109  * this is enough.
    110  */
    111 kmutex_t *
    112 lwp_lock_retry(struct lwp *l, kmutex_t *old)
    113 {
    114 
    115 	return old;
    116 }
    117