Remove building with NOCRYPTO option
[minix.git] / tests / rump / kernspace / alloc.c
blob5cbcfb61f2ce3be3362e02447630ab1a1203d525
1 /* $NetBSD: alloc.c,v 1.1 2010/06/14 21:06:09 pooka Exp $ */
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 #if !defined(lint)
32 __RCSID("$NetBSD: alloc.c,v 1.1 2010/06/14 21:06:09 pooka Exp $");
33 #endif /* !lint */
35 #include <sys/param.h>
36 #include <sys/condvar.h>
37 #include <sys/kmem.h>
38 #include <sys/kthread.h>
39 #include <sys/mutex.h>
40 #include <sys/pool.h>
41 #include <sys/proc.h>
43 #include <uvm/uvm.h>
45 #include <rump/rumpuser.h>
46 #include "kernspace.h"
48 static void *store[32];
49 static struct pool pp1, pp2;
51 static kmutex_t mtx;
52 static kcondvar_t kcv;
53 static int curstat;
55 static void
56 hthr(void *arg)
58 int i;
60 mutex_enter(&mtx);
61 curstat++;
62 cv_signal(&kcv);
64 while (curstat < 2)
65 cv_wait(&kcv, &mtx);
66 mutex_exit(&mtx);
68 /* try to guarantee that the sleep is triggered in PR_WAITOK */
69 while ((kernel_map->flags & VM_MAP_WANTVA) == 0)
70 kpause("take5", false, 1, NULL);
72 for (i = 0; i < __arraycount(store); i++) {
73 pool_put(&pp1, store[i]);
76 kthread_exit(0);
79 void
80 rumptest_alloc(size_t thelimit)
82 char *c;
83 int succ, i;
85 mutex_init(&mtx, MUTEX_DEFAULT, IPL_NONE);
86 cv_init(&kcv, "venailu");
88 kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, hthr, NULL, NULL, "h");
90 pool_init(&pp1, 1024, 0, 0, 0, "vara-allas",
91 &pool_allocator_nointr, IPL_NONE);
92 pool_init(&pp2, 1024, 0, 0, 0, "allas",
93 &pool_allocator_nointr, IPL_NONE);
95 for (i = 0; i < __arraycount(store); i++) {
96 store[i] = pool_get(&pp1, PR_NOWAIT);
97 if (store[i] == NULL) {
98 panic("pool_get store failed");
102 /* wait until other thread runs */
103 mutex_enter(&mtx);
104 while (curstat == 0)
105 cv_wait(&kcv, &mtx);
106 mutex_exit(&mtx);
108 for (succ = 0;; succ++) {
109 if (succ * 1024 > thelimit)
110 panic("managed to allocate over limit");
111 if ((c = pool_get(&pp2, PR_NOWAIT)) == NULL) {
112 mutex_enter(&mtx);
113 curstat++;
114 cv_signal(&kcv);
115 mutex_exit(&mtx);
116 if (pool_get(&pp2, PR_WAITOK) == NULL)
117 panic("pool get PR_WAITOK failed");
118 break;
120 *c = 'a';