rf_mcpair.c revision 1.9 1 /* $NetBSD: rf_mcpair.c,v 1.9 2003/12/29 03:33:48 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: Jim Zelenka
7 *
8 * Permission to use, copy, modify and distribute this software and
9 * its documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28
29 /* rf_mcpair.c
30 * an mcpair is a structure containing a mutex and a condition variable.
31 * it's used to block the current thread until some event occurs.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: rf_mcpair.c,v 1.9 2003/12/29 03:33:48 oster Exp $");
36
37 #include <dev/raidframe/raidframevar.h>
38
39 #include "rf_archs.h"
40 #include "rf_threadstuff.h"
41 #include "rf_mcpair.h"
42 #include "rf_debugMem.h"
43 #include "rf_general.h"
44 #include "rf_shutdown.h"
45
46 #include <sys/pool.h>
47 #include <sys/proc.h>
48
49 static struct pool rf_mcpair_pool;
50 #define RF_MAX_FREE_MCPAIR 128
51 #define RF_MCPAIR_INC 16
52 #define RF_MCPAIR_INITIAL 24
53
54 static int init_mcpair(RF_MCPair_t *);
55 static void clean_mcpair(RF_MCPair_t *);
56 static void rf_ShutdownMCPair(void *);
57
58
59
60 static int
61 init_mcpair(t)
62 RF_MCPair_t *t;
63 {
64 int rc;
65
66 rc = rf_mutex_init(&t->mutex);
67 if (rc) {
68 rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
69 return (rc);
70 }
71 rc = rf_cond_init(&t->cond);
72 if (rc) {
73 rf_print_unable_to_init_cond(__FILE__, __LINE__, rc);
74 rf_mutex_destroy(&t->mutex);
75 return (rc);
76 }
77 return (0);
78 }
79
80 static void
81 clean_mcpair(t)
82 RF_MCPair_t *t;
83 {
84 rf_mutex_destroy(&t->mutex);
85 rf_cond_destroy(&t->cond);
86 }
87
88 static void
89 rf_ShutdownMCPair(ignored)
90 void *ignored;
91 {
92 /* XXX what about the "cleaning" stuff??? Is it even necessary */
93 pool_destroy(&rf_mcpair_pool);
94 }
95
96 int
97 rf_ConfigureMCPair(listp)
98 RF_ShutdownList_t **listp;
99 {
100 int rc;
101
102 pool_init(&rf_mcpair_pool, sizeof(RF_MCPair_t), 0, 0, 0,
103 "rf_mcpair_pl", NULL);
104 pool_sethiwat(&rf_mcpair_pool, RF_MAX_FREE_MCPAIR);
105 pool_prime(&rf_mcpair_pool, RF_MCPAIR_INITIAL);
106
107 rc = rf_ShutdownCreate(listp, rf_ShutdownMCPair, NULL);
108 if (rc) {
109 rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
110 rf_ShutdownMCPair(NULL);
111 return (rc);
112 }
113
114 return (0);
115 }
116
117 RF_MCPair_t *
118 rf_AllocMCPair()
119 {
120 RF_MCPair_t *t;
121
122 t = pool_get(&rf_mcpair_pool, PR_WAITOK);
123 if (init_mcpair(t)) {
124 /* some sort of error... */
125 pool_put(&rf_mcpair_pool, t);
126 t = NULL;
127 }
128 if (t) {
129 t->flag = 0;
130 t->next = NULL;
131 }
132 return (t);
133 }
134
135 void
136 rf_FreeMCPair(t)
137 RF_MCPair_t *t;
138 {
139 clean_mcpair(t);
140 pool_put(&rf_mcpair_pool, t);
141 }
142 /* the callback function used to wake you up when you use an mcpair to wait for something */
143 void
144 rf_MCPairWakeupFunc(mcpair)
145 RF_MCPair_t *mcpair;
146 {
147 RF_LOCK_MUTEX(mcpair->mutex);
148 mcpair->flag = 1;
149 wakeup(&(mcpair->cond));
150 RF_UNLOCK_MUTEX(mcpair->mutex);
151 }
152