1*57718be8SEnji Cooper /* $NetBSD: thread.c,v 1.2 2011/08/07 14:03:15 rmind Exp $ */ 2*57718be8SEnji Cooper 3*57718be8SEnji Cooper /*- 4*57718be8SEnji Cooper * Copyright (c) 2010 The NetBSD Foundation, Inc. 5*57718be8SEnji Cooper * All rights reserved. 6*57718be8SEnji Cooper * 7*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 8*57718be8SEnji Cooper * modification, are permitted provided that the following conditions 9*57718be8SEnji Cooper * are met: 10*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 11*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 12*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 13*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 14*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 15*57718be8SEnji Cooper * 16*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17*57718be8SEnji Cooper * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18*57718be8SEnji Cooper * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19*57718be8SEnji Cooper * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20*57718be8SEnji Cooper * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21*57718be8SEnji Cooper * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*57718be8SEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23*57718be8SEnji Cooper * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25*57718be8SEnji Cooper * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26*57718be8SEnji Cooper * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27*57718be8SEnji Cooper * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28*57718be8SEnji Cooper */ 29*57718be8SEnji Cooper 30*57718be8SEnji Cooper #include <sys/cdefs.h> 31*57718be8SEnji Cooper #if !defined(lint) 32*57718be8SEnji Cooper __RCSID("$NetBSD: thread.c,v 1.2 2011/08/07 14:03:15 rmind Exp $"); 33*57718be8SEnji Cooper #endif /* !lint */ 34*57718be8SEnji Cooper 35*57718be8SEnji Cooper #include <sys/param.h> 36*57718be8SEnji Cooper #include <sys/kernel.h> 37*57718be8SEnji Cooper #include <sys/kthread.h> 38*57718be8SEnji Cooper #include <sys/mutex.h> 39*57718be8SEnji Cooper #include <sys/proc.h> 40*57718be8SEnji Cooper 41*57718be8SEnji Cooper #include "kernspace.h" 42*57718be8SEnji Cooper 43*57718be8SEnji Cooper static volatile int testit; 44*57718be8SEnji Cooper 45*57718be8SEnji Cooper static void 46*57718be8SEnji Cooper jointhread(void *arg) 47*57718be8SEnji Cooper { 48*57718be8SEnji Cooper 49*57718be8SEnji Cooper kpause("take5", false, 1, NULL); 50*57718be8SEnji Cooper testit = 1; 51*57718be8SEnji Cooper kthread_exit(0); 52*57718be8SEnji Cooper } 53*57718be8SEnji Cooper 54*57718be8SEnji Cooper void 55*57718be8SEnji Cooper rumptest_threadjoin() 56*57718be8SEnji Cooper { 57*57718be8SEnji Cooper struct lwp *newl; 58*57718be8SEnji Cooper int rv; 59*57718be8SEnji Cooper 60*57718be8SEnji Cooper rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL, 61*57718be8SEnji Cooper jointhread, NULL, &newl, "jointest"); 62*57718be8SEnji Cooper if (rv) 63*57718be8SEnji Cooper panic("thread creation failed: %d", rv); 64*57718be8SEnji Cooper rv = kthread_join(newl); 65*57718be8SEnji Cooper if (rv) 66*57718be8SEnji Cooper panic("thread join failed: %d", rv); 67*57718be8SEnji Cooper 68*57718be8SEnji Cooper if (testit != 1) 69*57718be8SEnji Cooper panic("new thread did not run"); 70*57718be8SEnji Cooper } 71*57718be8SEnji Cooper 72*57718be8SEnji Cooper static kmutex_t mtx; 73*57718be8SEnji Cooper static kcondvar_t cv; 74*57718be8SEnji Cooper static int value; 75*57718be8SEnji Cooper 76*57718be8SEnji Cooper static void 77*57718be8SEnji Cooper thethread(void *arg) 78*57718be8SEnji Cooper { 79*57718be8SEnji Cooper 80*57718be8SEnji Cooper mutex_enter(&mtx); 81*57718be8SEnji Cooper value = 1; 82*57718be8SEnji Cooper cv_signal(&cv); 83*57718be8SEnji Cooper mutex_exit(&mtx); 84*57718be8SEnji Cooper 85*57718be8SEnji Cooper kthread_exit(0); 86*57718be8SEnji Cooper } 87*57718be8SEnji Cooper 88*57718be8SEnji Cooper void 89*57718be8SEnji Cooper rumptest_thread() 90*57718be8SEnji Cooper { 91*57718be8SEnji Cooper struct lwp *newl; 92*57718be8SEnji Cooper int rv; 93*57718be8SEnji Cooper 94*57718be8SEnji Cooper mutex_init(&mtx, MUTEX_DEFAULT, IPL_NONE); 95*57718be8SEnji Cooper cv_init(&cv, "jooei"); 96*57718be8SEnji Cooper rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, 97*57718be8SEnji Cooper thethread, NULL, &newl, "ktest"); 98*57718be8SEnji Cooper if (rv) 99*57718be8SEnji Cooper panic("thread creation failed: %d", rv); 100*57718be8SEnji Cooper 101*57718be8SEnji Cooper mutex_enter(&mtx); 102*57718be8SEnji Cooper while (value == 0) 103*57718be8SEnji Cooper cv_wait(&cv, &mtx); 104*57718be8SEnji Cooper mutex_exit(&mtx); 105*57718be8SEnji Cooper 106*57718be8SEnji Cooper /* try to verify thread really exists and we don't crash */ 107*57718be8SEnji Cooper kpause("take1", false, 1, NULL); 108*57718be8SEnji Cooper } 109