xref: /netbsd-src/tests/rump/kernspace/tsleep.c (revision c93eb026cf1ae58271cc982e7c6792ca2075e0aa)
1*c93eb026Sdholland /*	$NetBSD: tsleep.c,v 1.4 2014/03/21 22:18:57 dholland 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*c93eb026Sdholland __RCSID("$NetBSD: tsleep.c,v 1.4 2014/03/21 22:18:57 dholland Exp $");
339a733049Spooka #endif /* !lint */
349a733049Spooka 
359a733049Spooka #include <sys/param.h>
369a733049Spooka #include <sys/kernel.h>
379a733049Spooka #include <sys/kthread.h>
389a733049Spooka #include <sys/proc.h>
399a733049Spooka 
409a733049Spooka #include "kernspace.h"
419a733049Spooka 
429a733049Spooka #define NTHREADS 10
439a733049Spooka 
449a733049Spooka /*
459a733049Spooka  * mpsafe thread.  need dedicated interlock
469a733049Spooka  */
479a733049Spooka static kmutex_t mymtx;
489a733049Spooka 
499a733049Spooka static void
tinythread(void * arg)509a733049Spooka tinythread(void *arg)
519a733049Spooka {
529a733049Spooka 	static int wakeups;
539a733049Spooka 	int i, rv;
549a733049Spooka 	bool relock = ((uintptr_t)arg % 2) == 0;
559a733049Spooka 
569a733049Spooka 	for (i = 0; i < 1000; i++) {
579a733049Spooka 		mutex_enter(&mymtx);
589a733049Spooka 		wakeup(tinythread);
599a733049Spooka 		if (wakeups >= NTHREADS-1) {
609a733049Spooka 			mutex_exit(&mymtx);
619a733049Spooka 			break;
629a733049Spooka 		}
639a733049Spooka 		rv = mtsleep(tinythread, relock ? 0 : PNORELOCK,
649a733049Spooka 		    "haa", 0, &mymtx);
659a733049Spooka 		if (relock)
669a733049Spooka 			mutex_exit(&mymtx);
679a733049Spooka 		if (rv != 0)
689a733049Spooka 			panic("mtsleep failed");
699a733049Spooka 	}
709a733049Spooka 
719a733049Spooka 	mutex_enter(&mymtx);
729a733049Spooka 	wakeups++;
739a733049Spooka 	wakeup(tinythread);
749a733049Spooka 
759a733049Spooka 	rv = mtsleep(rumptest_tsleep, PNORELOCK, "kepuli", 1, &mymtx);
769a733049Spooka 	if (rv != EWOULDBLOCK)
779a733049Spooka 		panic("mtsleep unexpected return value %d", rv);
789a733049Spooka 
799a733049Spooka 	kthread_exit(0);
809a733049Spooka }
819a733049Spooka 
829a733049Spooka void
rumptest_tsleep()839a733049Spooka rumptest_tsleep()
849a733049Spooka {
859a733049Spooka 	struct lwp *notbigl[NTHREADS];
869a733049Spooka 	int rv, i;
879a733049Spooka 
889a733049Spooka 	mutex_init(&mymtx, MUTEX_DEFAULT, IPL_NONE);
899a733049Spooka 
909a733049Spooka 	for (i = 0; i < NTHREADS; i++) {
91a0ffc02aSrmind 		rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN| KTHREAD_MPSAFE,
929a733049Spooka 		    NULL, tinythread, (void *)(uintptr_t)i, &notbigl[i], "nb");
939a733049Spooka 		if (rv)
949a733049Spooka 			panic("thread create failed: %d", rv);
959a733049Spooka 	}
969a733049Spooka 
979a733049Spooka 	for (i = 0; i < NTHREADS; i++) {
989a733049Spooka 		kthread_join(notbigl[i]);
999a733049Spooka 	}
1009a733049Spooka }
101