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