1*0a6a1f1dSLionel Sambuc /* $NetBSD: tsleep.c,v 1.4 2014/03/21 22:18:57 dholland Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc * are met:
1011be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1111be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1211be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1311be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1411be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1511be35a1SLionel Sambuc *
1611be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
1711be35a1SLionel Sambuc * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1811be35a1SLionel Sambuc * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1911be35a1SLionel Sambuc * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2011be35a1SLionel Sambuc * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
2111be35a1SLionel Sambuc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2211be35a1SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2311be35a1SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2411be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2511be35a1SLionel Sambuc * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2611be35a1SLionel Sambuc * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2711be35a1SLionel Sambuc * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2811be35a1SLionel Sambuc */
2911be35a1SLionel Sambuc
3011be35a1SLionel Sambuc #include <sys/cdefs.h>
3111be35a1SLionel Sambuc #if !defined(lint)
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: tsleep.c,v 1.4 2014/03/21 22:18:57 dholland Exp $");
3311be35a1SLionel Sambuc #endif /* !lint */
3411be35a1SLionel Sambuc
3511be35a1SLionel Sambuc #include <sys/param.h>
3611be35a1SLionel Sambuc #include <sys/kernel.h>
3711be35a1SLionel Sambuc #include <sys/kthread.h>
3811be35a1SLionel Sambuc #include <sys/proc.h>
3911be35a1SLionel Sambuc
4011be35a1SLionel Sambuc #include "kernspace.h"
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc #define NTHREADS 10
4311be35a1SLionel Sambuc
4411be35a1SLionel Sambuc /*
4511be35a1SLionel Sambuc * mpsafe thread. need dedicated interlock
4611be35a1SLionel Sambuc */
4711be35a1SLionel Sambuc static kmutex_t mymtx;
4811be35a1SLionel Sambuc
4911be35a1SLionel Sambuc static void
tinythread(void * arg)5011be35a1SLionel Sambuc tinythread(void *arg)
5111be35a1SLionel Sambuc {
5211be35a1SLionel Sambuc static int wakeups;
5311be35a1SLionel Sambuc int i, rv;
5411be35a1SLionel Sambuc bool relock = ((uintptr_t)arg % 2) == 0;
5511be35a1SLionel Sambuc
5611be35a1SLionel Sambuc for (i = 0; i < 1000; i++) {
5711be35a1SLionel Sambuc mutex_enter(&mymtx);
5811be35a1SLionel Sambuc wakeup(tinythread);
5911be35a1SLionel Sambuc if (wakeups >= NTHREADS-1) {
6011be35a1SLionel Sambuc mutex_exit(&mymtx);
6111be35a1SLionel Sambuc break;
6211be35a1SLionel Sambuc }
6311be35a1SLionel Sambuc rv = mtsleep(tinythread, relock ? 0 : PNORELOCK,
6411be35a1SLionel Sambuc "haa", 0, &mymtx);
6511be35a1SLionel Sambuc if (relock)
6611be35a1SLionel Sambuc mutex_exit(&mymtx);
6711be35a1SLionel Sambuc if (rv != 0)
6811be35a1SLionel Sambuc panic("mtsleep failed");
6911be35a1SLionel Sambuc }
7011be35a1SLionel Sambuc
7111be35a1SLionel Sambuc mutex_enter(&mymtx);
7211be35a1SLionel Sambuc wakeups++;
7311be35a1SLionel Sambuc wakeup(tinythread);
7411be35a1SLionel Sambuc
7511be35a1SLionel Sambuc rv = mtsleep(rumptest_tsleep, PNORELOCK, "kepuli", 1, &mymtx);
7611be35a1SLionel Sambuc if (rv != EWOULDBLOCK)
7711be35a1SLionel Sambuc panic("mtsleep unexpected return value %d", rv);
7811be35a1SLionel Sambuc
7911be35a1SLionel Sambuc kthread_exit(0);
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc
8211be35a1SLionel Sambuc void
rumptest_tsleep()8311be35a1SLionel Sambuc rumptest_tsleep()
8411be35a1SLionel Sambuc {
8511be35a1SLionel Sambuc struct lwp *notbigl[NTHREADS];
8611be35a1SLionel Sambuc int rv, i;
8711be35a1SLionel Sambuc
8811be35a1SLionel Sambuc mutex_init(&mymtx, MUTEX_DEFAULT, IPL_NONE);
8911be35a1SLionel Sambuc
9011be35a1SLionel Sambuc for (i = 0; i < NTHREADS; i++) {
9111be35a1SLionel Sambuc rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN| KTHREAD_MPSAFE,
9211be35a1SLionel Sambuc NULL, tinythread, (void *)(uintptr_t)i, ¬bigl[i], "nb");
9311be35a1SLionel Sambuc if (rv)
9411be35a1SLionel Sambuc panic("thread create failed: %d", rv);
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc
9711be35a1SLionel Sambuc for (i = 0; i < NTHREADS; i++) {
9811be35a1SLionel Sambuc kthread_join(notbigl[i]);
9911be35a1SLionel Sambuc }
10011be35a1SLionel Sambuc }
101