Home | History | Annotate | Line # | Download | only in rumpkern
klock.c revision 1.2.2.3
      1  1.2.2.2  rmind /*	$NetBSD: klock.c,v 1.2.2.3 2011/03/05 20:56:14 rmind Exp $	*/
      2  1.2.2.2  rmind 
      3  1.2.2.2  rmind /*
      4  1.2.2.2  rmind  * Copyright (c) 2007-2010 Antti Kantee.  All Rights Reserved.
      5  1.2.2.2  rmind  *
      6  1.2.2.2  rmind  * Development of this software was supported by the
      7  1.2.2.2  rmind  * Finnish Cultural Foundation.
      8  1.2.2.2  rmind  *
      9  1.2.2.2  rmind  * Redistribution and use in source and binary forms, with or without
     10  1.2.2.2  rmind  * modification, are permitted provided that the following conditions
     11  1.2.2.2  rmind  * are met:
     12  1.2.2.2  rmind  * 1. Redistributions of source code must retain the above copyright
     13  1.2.2.2  rmind  *    notice, this list of conditions and the following disclaimer.
     14  1.2.2.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.2.2.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     16  1.2.2.2  rmind  *    documentation and/or other materials provided with the distribution.
     17  1.2.2.2  rmind  *
     18  1.2.2.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  1.2.2.2  rmind  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  1.2.2.2  rmind  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  1.2.2.2  rmind  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.2.2.2  rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.2.2.2  rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  1.2.2.2  rmind  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.2.2.2  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.2.2.2  rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.2.2.2  rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.2.2.2  rmind  * SUCH DAMAGE.
     29  1.2.2.2  rmind  */
     30  1.2.2.2  rmind 
     31  1.2.2.2  rmind #include <sys/cdefs.h>
     32  1.2.2.2  rmind __KERNEL_RCSID(0, "$NetBSD: klock.c,v 1.2.2.3 2011/03/05 20:56:14 rmind Exp $");
     33  1.2.2.2  rmind 
     34  1.2.2.2  rmind #include <sys/param.h>
     35  1.2.2.2  rmind #include <sys/systm.h>
     36  1.2.2.2  rmind 
     37  1.2.2.2  rmind #include <rump/rumpuser.h>
     38  1.2.2.2  rmind 
     39  1.2.2.2  rmind #include "rump_private.h"
     40  1.2.2.2  rmind 
     41  1.2.2.2  rmind /*
     42  1.2.2.2  rmind  * giant lock
     43  1.2.2.2  rmind  */
     44  1.2.2.2  rmind 
     45  1.2.2.3  rmind struct rumpuser_mtx *rump_giantlock;
     46  1.2.2.3  rmind static int giantcnt;
     47  1.2.2.3  rmind static struct lwp *giantowner;
     48  1.2.2.2  rmind 
     49  1.2.2.2  rmind void
     50  1.2.2.3  rmind rump_kernel_bigwrap(int *nlocks)
     51  1.2.2.2  rmind {
     52  1.2.2.2  rmind 
     53  1.2.2.3  rmind 	KASSERT(giantcnt > 0 && curlwp == giantowner);
     54  1.2.2.3  rmind 	giantowner = NULL;
     55  1.2.2.3  rmind 	*nlocks = giantcnt;
     56  1.2.2.3  rmind 	giantcnt = 0;
     57  1.2.2.2  rmind }
     58  1.2.2.2  rmind 
     59  1.2.2.2  rmind void
     60  1.2.2.3  rmind rump_kernel_bigunwrap(int nlocks)
     61  1.2.2.2  rmind {
     62  1.2.2.2  rmind 
     63  1.2.2.3  rmind 	KASSERT(giantowner == NULL);
     64  1.2.2.3  rmind 	giantowner = curlwp;
     65  1.2.2.3  rmind 	giantcnt = nlocks;
     66  1.2.2.2  rmind }
     67  1.2.2.2  rmind 
     68  1.2.2.2  rmind void
     69  1.2.2.2  rmind _kernel_lock(int nlocks)
     70  1.2.2.2  rmind {
     71  1.2.2.3  rmind 	struct lwp *l = curlwp;
     72  1.2.2.2  rmind 
     73  1.2.2.3  rmind 	while (nlocks) {
     74  1.2.2.3  rmind 		if (giantowner == l) {
     75  1.2.2.3  rmind 			giantcnt += nlocks;
     76  1.2.2.3  rmind 			nlocks = 0;
     77  1.2.2.3  rmind 		} else {
     78  1.2.2.3  rmind 			if (!rumpuser_mutex_tryenter(rump_giantlock)) {
     79  1.2.2.3  rmind 				rump_unschedule_cpu1(l, NULL);
     80  1.2.2.3  rmind 				rumpuser_mutex_enter_nowrap(rump_giantlock);
     81  1.2.2.3  rmind 				rump_schedule_cpu(l);
     82  1.2.2.3  rmind 			}
     83  1.2.2.3  rmind 			giantowner = l;
     84  1.2.2.3  rmind 			giantcnt = 1;
     85  1.2.2.3  rmind 			nlocks--;
     86  1.2.2.2  rmind 		}
     87  1.2.2.2  rmind 	}
     88  1.2.2.2  rmind }
     89  1.2.2.2  rmind 
     90  1.2.2.2  rmind void
     91  1.2.2.2  rmind _kernel_unlock(int nlocks, int *countp)
     92  1.2.2.2  rmind {
     93  1.2.2.2  rmind 
     94  1.2.2.3  rmind 	if (giantowner != curlwp) {
     95  1.2.2.2  rmind 		KASSERT(nlocks == 0);
     96  1.2.2.2  rmind 		if (countp)
     97  1.2.2.2  rmind 			*countp = 0;
     98  1.2.2.2  rmind 		return;
     99  1.2.2.2  rmind 	}
    100  1.2.2.2  rmind 
    101  1.2.2.2  rmind 	if (countp)
    102  1.2.2.3  rmind 		*countp = giantcnt;
    103  1.2.2.2  rmind 	if (nlocks == 0)
    104  1.2.2.3  rmind 		nlocks = giantcnt;
    105  1.2.2.2  rmind 	if (nlocks == -1) {
    106  1.2.2.3  rmind 		KASSERT(giantcnt == 1);
    107  1.2.2.2  rmind 		nlocks = 1;
    108  1.2.2.2  rmind 	}
    109  1.2.2.3  rmind 	KASSERT(nlocks <= giantcnt);
    110  1.2.2.2  rmind 	while (nlocks--) {
    111  1.2.2.3  rmind 		giantcnt--;
    112  1.2.2.3  rmind 	}
    113  1.2.2.3  rmind 
    114  1.2.2.3  rmind 	if (giantcnt == 0) {
    115  1.2.2.3  rmind 		giantowner = NULL;
    116  1.2.2.2  rmind 		rumpuser_mutex_exit(rump_giantlock);
    117  1.2.2.2  rmind 	}
    118  1.2.2.2  rmind }
    119  1.2.2.2  rmind 
    120  1.2.2.2  rmind void
    121  1.2.2.2  rmind rump_user_unschedule(int nlocks, int *countp, void *interlock)
    122  1.2.2.2  rmind {
    123  1.2.2.2  rmind 
    124  1.2.2.2  rmind 	_kernel_unlock(nlocks, countp);
    125  1.2.2.2  rmind 	/*
    126  1.2.2.2  rmind 	 * XXX: technically we should unschedule_cpu1() here, but that
    127  1.2.2.2  rmind 	 * requires rump_intr_enter/exit to be implemented.
    128  1.2.2.2  rmind 	 */
    129  1.2.2.2  rmind 	rump_unschedule_cpu_interlock(curlwp, interlock);
    130  1.2.2.2  rmind }
    131  1.2.2.2  rmind 
    132  1.2.2.2  rmind void
    133  1.2.2.2  rmind rump_user_schedule(int nlocks, void *interlock)
    134  1.2.2.2  rmind {
    135  1.2.2.2  rmind 
    136  1.2.2.2  rmind 	rump_schedule_cpu_interlock(curlwp, interlock);
    137  1.2.2.2  rmind 
    138  1.2.2.2  rmind 	if (nlocks)
    139  1.2.2.2  rmind 		_kernel_lock(nlocks);
    140  1.2.2.2  rmind }
    141