xref: /netbsd/sys/dev/raidframe/rf_mcpair.c (revision bf9ec67e)
1 /*	$NetBSD: rf_mcpair.c,v 1.7 2001/11/13 07:11:14 lukem 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.7 2001/11/13 07:11:14 lukem 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_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
69 		    __LINE__, rc);
70 		return (rc);
71 	}
72 	rc = rf_cond_init(&t->cond);
73 	if (rc) {
74 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
75 		    __LINE__, rc);
76 		rf_mutex_destroy(&t->mutex);
77 		return (rc);
78 	}
79 	return (0);
80 }
81 
82 static void
83 clean_mcpair(t)
84 	RF_MCPair_t *t;
85 {
86 	rf_mutex_destroy(&t->mutex);
87 	rf_cond_destroy(&t->cond);
88 }
89 
90 static void
91 rf_ShutdownMCPair(ignored)
92 	void   *ignored;
93 {
94 	RF_FREELIST_DESTROY_CLEAN(rf_mcpair_freelist, next, (RF_MCPair_t *), clean_mcpair);
95 }
96 
97 int
98 rf_ConfigureMCPair(listp)
99 	RF_ShutdownList_t **listp;
100 {
101 	int     rc;
102 
103 	RF_FREELIST_CREATE(rf_mcpair_freelist, RF_MAX_FREE_MCPAIR,
104 	    RF_MCPAIR_INC, sizeof(RF_MCPair_t));
105 	rc = rf_ShutdownCreate(listp, rf_ShutdownMCPair, NULL);
106 	if (rc) {
107 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
108 		    __FILE__, __LINE__, rc);
109 		rf_ShutdownMCPair(NULL);
110 		return (rc);
111 	}
112 	RF_FREELIST_PRIME_INIT(rf_mcpair_freelist, RF_MCPAIR_INITIAL, next,
113 	    (RF_MCPair_t *), init_mcpair);
114 	return (0);
115 }
116 
117 RF_MCPair_t *
118 rf_AllocMCPair()
119 {
120 	RF_MCPair_t *t;
121 
122 	RF_FREELIST_GET_INIT(rf_mcpair_freelist, t, next, (RF_MCPair_t *), init_mcpair);
123 	if (t) {
124 		t->flag = 0;
125 		t->next = NULL;
126 	}
127 	return (t);
128 }
129 
130 void
131 rf_FreeMCPair(t)
132 	RF_MCPair_t *t;
133 {
134 	RF_FREELIST_FREE_CLEAN(rf_mcpair_freelist, t, next, clean_mcpair);
135 }
136 /* the callback function used to wake you up when you use an mcpair to wait for something */
137 void
138 rf_MCPairWakeupFunc(mcpair)
139 	RF_MCPair_t *mcpair;
140 {
141 	RF_LOCK_MUTEX(mcpair->mutex);
142 	mcpair->flag = 1;
143 	wakeup(&(mcpair->cond));
144 	RF_UNLOCK_MUTEX(mcpair->mutex);
145 }
146