xref: /netbsd-src/sys/dev/raidframe/rf_mcpair.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: rf_mcpair.c,v 1.8 2002/09/14 17:53:58 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 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: rf_mcpair.c,v 1.8 2002/09/14 17:53:58 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_freelist.h"
44 #include "rf_shutdown.h"
45 
46 #include <sys/proc.h>
47 
48 static RF_FreeList_t *rf_mcpair_freelist;
49 
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 	RF_FREELIST_DESTROY_CLEAN(rf_mcpair_freelist, next, (RF_MCPair_t *), clean_mcpair);
93 }
94 
95 int
96 rf_ConfigureMCPair(listp)
97 	RF_ShutdownList_t **listp;
98 {
99 	int     rc;
100 
101 	RF_FREELIST_CREATE(rf_mcpair_freelist, RF_MAX_FREE_MCPAIR,
102 	    RF_MCPAIR_INC, sizeof(RF_MCPair_t));
103 	rc = rf_ShutdownCreate(listp, rf_ShutdownMCPair, NULL);
104 	if (rc) {
105 		rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
106 		rf_ShutdownMCPair(NULL);
107 		return (rc);
108 	}
109 	RF_FREELIST_PRIME_INIT(rf_mcpair_freelist, RF_MCPAIR_INITIAL, next,
110 	    (RF_MCPair_t *), init_mcpair);
111 	return (0);
112 }
113 
114 RF_MCPair_t *
115 rf_AllocMCPair()
116 {
117 	RF_MCPair_t *t;
118 
119 	RF_FREELIST_GET_INIT(rf_mcpair_freelist, t, next, (RF_MCPair_t *), init_mcpair);
120 	if (t) {
121 		t->flag = 0;
122 		t->next = NULL;
123 	}
124 	return (t);
125 }
126 
127 void
128 rf_FreeMCPair(t)
129 	RF_MCPair_t *t;
130 {
131 	RF_FREELIST_FREE_CLEAN(rf_mcpair_freelist, t, next, clean_mcpair);
132 }
133 /* the callback function used to wake you up when you use an mcpair to wait for something */
134 void
135 rf_MCPairWakeupFunc(mcpair)
136 	RF_MCPair_t *mcpair;
137 {
138 	RF_LOCK_MUTEX(mcpair->mutex);
139 	mcpair->flag = 1;
140 	wakeup(&(mcpair->cond));
141 	RF_UNLOCK_MUTEX(mcpair->mutex);
142 }
143