xref: /netbsd-src/tests/rump/kernspace/thread.c (revision a0ffc02ab8759aabd4564ea572c53052e92b8584)
1*a0ffc02aSrmind /*	$NetBSD: thread.c,v 1.2 2011/08/07 14:03:15 rmind Exp $	*/
29a733049Spooka 
39a733049Spooka /*-
49a733049Spooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
59a733049Spooka  * All rights reserved.
69a733049Spooka  *
79a733049Spooka  * Redistribution and use in source and binary forms, with or without
89a733049Spooka  * modification, are permitted provided that the following conditions
99a733049Spooka  * are met:
109a733049Spooka  * 1. Redistributions of source code must retain the above copyright
119a733049Spooka  *    notice, this list of conditions and the following disclaimer.
129a733049Spooka  * 2. Redistributions in binary form must reproduce the above copyright
139a733049Spooka  *    notice, this list of conditions and the following disclaimer in the
149a733049Spooka  *    documentation and/or other materials provided with the distribution.
159a733049Spooka  *
169a733049Spooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
179a733049Spooka  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
189a733049Spooka  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
199a733049Spooka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209a733049Spooka  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
219a733049Spooka  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229a733049Spooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
239a733049Spooka  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
249a733049Spooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
259a733049Spooka  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
269a733049Spooka  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
279a733049Spooka  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289a733049Spooka  */
299a733049Spooka 
309a733049Spooka #include <sys/cdefs.h>
319a733049Spooka #if !defined(lint)
32*a0ffc02aSrmind __RCSID("$NetBSD: thread.c,v 1.2 2011/08/07 14:03:15 rmind Exp $");
339a733049Spooka #endif /* !lint */
349a733049Spooka 
359a733049Spooka #include <sys/param.h>
369a733049Spooka #include <sys/kernel.h>
379a733049Spooka #include <sys/kthread.h>
389a733049Spooka #include <sys/mutex.h>
399a733049Spooka #include <sys/proc.h>
409a733049Spooka 
419a733049Spooka #include "kernspace.h"
429a733049Spooka 
439a733049Spooka static volatile int testit;
449a733049Spooka 
459a733049Spooka static void
jointhread(void * arg)469a733049Spooka jointhread(void *arg)
479a733049Spooka {
489a733049Spooka 
499a733049Spooka 	kpause("take5", false, 1, NULL);
509a733049Spooka 	testit = 1;
519a733049Spooka 	kthread_exit(0);
529a733049Spooka }
539a733049Spooka 
549a733049Spooka void
rumptest_threadjoin()559a733049Spooka rumptest_threadjoin()
569a733049Spooka {
579a733049Spooka 	struct lwp *newl;
589a733049Spooka 	int rv;
599a733049Spooka 
60*a0ffc02aSrmind 	rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
619a733049Spooka 	    jointhread, NULL, &newl, "jointest");
629a733049Spooka 	if (rv)
639a733049Spooka 		panic("thread creation failed: %d", rv);
649a733049Spooka 	rv = kthread_join(newl);
659a733049Spooka 	if (rv)
669a733049Spooka 		panic("thread join failed: %d", rv);
679a733049Spooka 
689a733049Spooka 	if (testit != 1)
699a733049Spooka 		panic("new thread did not run");
709a733049Spooka }
719a733049Spooka 
729a733049Spooka static kmutex_t mtx;
739a733049Spooka static kcondvar_t cv;
749a733049Spooka static int value;
759a733049Spooka 
769a733049Spooka static void
thethread(void * arg)779a733049Spooka thethread(void *arg)
789a733049Spooka {
799a733049Spooka 
809a733049Spooka 	mutex_enter(&mtx);
819a733049Spooka 	value = 1;
829a733049Spooka 	cv_signal(&cv);
839a733049Spooka 	mutex_exit(&mtx);
849a733049Spooka 
859a733049Spooka 	kthread_exit(0);
869a733049Spooka }
879a733049Spooka 
889a733049Spooka void
rumptest_thread()899a733049Spooka rumptest_thread()
909a733049Spooka {
919a733049Spooka 	struct lwp *newl;
929a733049Spooka 	int rv;
939a733049Spooka 
949a733049Spooka 	mutex_init(&mtx, MUTEX_DEFAULT, IPL_NONE);
959a733049Spooka 	cv_init(&cv, "jooei");
969a733049Spooka 	rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
979a733049Spooka 	    thethread, NULL, &newl, "ktest");
989a733049Spooka 	if (rv)
999a733049Spooka 		panic("thread creation failed: %d", rv);
1009a733049Spooka 
1019a733049Spooka 	mutex_enter(&mtx);
1029a733049Spooka 	while (value == 0)
1039a733049Spooka 		cv_wait(&cv, &mtx);
1049a733049Spooka 	mutex_exit(&mtx);
1059a733049Spooka 
1069a733049Spooka 	/* try to verify thread really exists and we don't crash */
1079a733049Spooka 	kpause("take1", false, 1, NULL);
1089a733049Spooka }
109