xref: /netbsd-src/sys/dev/raidframe/rf_mcpair.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: rf_mcpair.c,v 1.1 1998/11/13 04:20:31 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@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 /* :
35  * Log: rf_mcpair.c,v
36  * Revision 1.16  1996/06/19 22:23:01  jimz
37  * parity verification is now a layout-configurable thing
38  * not all layouts currently support it (correctly, anyway)
39  *
40  * Revision 1.15  1996/06/17  03:18:04  jimz
41  * include shutdown.h for macroized ShutdownCreate
42  *
43  * Revision 1.14  1996/06/10  11:55:47  jimz
44  * Straightened out some per-array/not-per-array distinctions, fixed
45  * a couple bugs related to confusion. Added shutdown lists. Removed
46  * layout shutdown function (now subsumed by shutdown lists).
47  *
48  * Revision 1.13  1996/06/05  18:06:02  jimz
49  * Major code cleanup. The Great Renaming is now done.
50  * Better modularity. Better typing. Fixed a bunch of
51  * synchronization bugs. Made a lot of global stuff
52  * per-desc or per-array. Removed dead code.
53  *
54  * Revision 1.12  1996/06/02  17:31:48  jimz
55  * Moved a lot of global stuff into array structure, where it belongs.
56  * Fixed up paritylogging, pss modules in this manner. Some general
57  * code cleanup. Removed lots of dead code, some dead files.
58  *
59  * Revision 1.11  1996/05/30  23:22:16  jimz
60  * bugfixes of serialization, timing problems
61  * more cleanup
62  *
63  * Revision 1.10  1996/05/20  16:15:22  jimz
64  * switch to rf_{mutex,cond}_{init,destroy}
65  *
66  * Revision 1.9  1996/05/18  19:51:34  jimz
67  * major code cleanup- fix syntax, make some types consistent,
68  * add prototypes, clean out dead code, et cetera
69  *
70  * Revision 1.8  1996/05/16  16:04:42  jimz
71  * convert to return-val on FREELIST init
72  *
73  * Revision 1.7  1996/05/16  14:47:21  jimz
74  * rewrote to use RF_FREELIST
75  *
76  * Revision 1.6  1995/12/01  19:25:43  root
77  * added copyright info
78  *
79  */
80 
81 #include "rf_types.h"
82 #include "rf_threadstuff.h"
83 #include "rf_mcpair.h"
84 #include "rf_debugMem.h"
85 #include "rf_freelist.h"
86 #include "rf_shutdown.h"
87 
88 #if defined(__NetBSD__) && defined(_KERNEL)
89 #include <sys/proc.h>
90 
91 #endif
92 
93 static RF_FreeList_t *rf_mcpair_freelist;
94 
95 #define RF_MAX_FREE_MCPAIR 128
96 #define RF_MCPAIR_INC       16
97 #define RF_MCPAIR_INITIAL   24
98 
99 static int init_mcpair(RF_MCPair_t *);
100 static void clean_mcpair(RF_MCPair_t *);
101 static void rf_ShutdownMCPair(void *);
102 
103 
104 
105 static int init_mcpair(t)
106   RF_MCPair_t  *t;
107 {
108 	int rc;
109 
110 	rc = rf_mutex_init(&t->mutex);
111 	if (rc) {
112 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
113 			__LINE__, rc);
114 		return(rc);
115 	}
116 	rc = rf_cond_init(&t->cond);
117 	if (rc) {
118 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
119 			__LINE__, rc);
120 		rf_mutex_destroy(&t->mutex);
121 		return(rc);
122 	}
123 	return(0);
124 }
125 
126 static void clean_mcpair(t)
127   RF_MCPair_t  *t;
128 {
129 	rf_mutex_destroy(&t->mutex);
130 	rf_cond_destroy(&t->cond);
131 }
132 
133 static void rf_ShutdownMCPair(ignored)
134   void  *ignored;
135 {
136 	RF_FREELIST_DESTROY_CLEAN(rf_mcpair_freelist,next,(RF_MCPair_t *),clean_mcpair);
137 }
138 
139 int rf_ConfigureMCPair(listp)
140   RF_ShutdownList_t  **listp;
141 {
142 	int rc;
143 
144 	RF_FREELIST_CREATE(rf_mcpair_freelist, RF_MAX_FREE_MCPAIR,
145 		RF_MCPAIR_INC, sizeof(RF_MCPair_t));
146 	rc = rf_ShutdownCreate(listp, rf_ShutdownMCPair, NULL);
147 	if (rc) {
148 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
149 			__FILE__, __LINE__, rc);
150 		rf_ShutdownMCPair(NULL);
151 		return(rc);
152 	}
153 	RF_FREELIST_PRIME_INIT(rf_mcpair_freelist, RF_MCPAIR_INITIAL,next,
154 		(RF_MCPair_t *),init_mcpair);
155 	return(0);
156 }
157 
158 RF_MCPair_t *rf_AllocMCPair()
159 {
160 	RF_MCPair_t *t;
161 
162 	RF_FREELIST_GET_INIT(rf_mcpair_freelist,t,next,(RF_MCPair_t *),init_mcpair);
163 	if (t) {
164 		t->flag = 0;
165 		t->next = NULL;
166 	}
167 	return(t);
168 }
169 
170 void rf_FreeMCPair(t)
171   RF_MCPair_t   *t;
172 {
173 	RF_FREELIST_FREE_CLEAN(rf_mcpair_freelist,t,next,clean_mcpair);
174 }
175 
176 /* the callback function used to wake you up when you use an mcpair to wait for something */
177 void rf_MCPairWakeupFunc(mcpair)
178   RF_MCPair_t  *mcpair;
179 {
180 	RF_LOCK_MUTEX(mcpair->mutex);
181 	mcpair->flag = 1;
182 #if 0
183 printf("MCPairWakeupFunc called!\n");
184 #endif
185 #ifdef KERNEL
186 	wakeup(&(mcpair->flag)); /* XXX Does this do anything useful!! GO */
187 	/* XXX Looks like the following is needed to truly get the
188 functionality they were looking for here... This could be a side-effect
189 of my using a tsleep in the NetBSD port though... XXX */
190 #if defined(__NetBSD__) && defined(_KERNEL)
191 	wakeup(&(mcpair->cond)); /* XXX XXX XXX GO */
192 #endif
193 #else /* KERNEL */
194 	RF_SIGNAL_COND(mcpair->cond);
195 #endif /* KERNEL */
196 	RF_UNLOCK_MUTEX(mcpair->mutex);
197 }
198