rf_mcpair.c revision 1.6 1 /* $NetBSD: rf_mcpair.c,v 1.6 2001/10/04 15:58:54 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 <dev/raidframe/raidframevar.h>
35
36 #include "rf_archs.h"
37 #include "rf_threadstuff.h"
38 #include "rf_mcpair.h"
39 #include "rf_debugMem.h"
40 #include "rf_freelist.h"
41 #include "rf_shutdown.h"
42
43 #include <sys/proc.h>
44
45 static RF_FreeList_t *rf_mcpair_freelist;
46
47 #define RF_MAX_FREE_MCPAIR 128
48 #define RF_MCPAIR_INC 16
49 #define RF_MCPAIR_INITIAL 24
50
51 static int init_mcpair(RF_MCPair_t *);
52 static void clean_mcpair(RF_MCPair_t *);
53 static void rf_ShutdownMCPair(void *);
54
55
56
57 static int
58 init_mcpair(t)
59 RF_MCPair_t *t;
60 {
61 int rc;
62
63 rc = rf_mutex_init(&t->mutex);
64 if (rc) {
65 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
66 __LINE__, rc);
67 return (rc);
68 }
69 rc = rf_cond_init(&t->cond);
70 if (rc) {
71 RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
72 __LINE__, rc);
73 rf_mutex_destroy(&t->mutex);
74 return (rc);
75 }
76 return (0);
77 }
78
79 static void
80 clean_mcpair(t)
81 RF_MCPair_t *t;
82 {
83 rf_mutex_destroy(&t->mutex);
84 rf_cond_destroy(&t->cond);
85 }
86
87 static void
88 rf_ShutdownMCPair(ignored)
89 void *ignored;
90 {
91 RF_FREELIST_DESTROY_CLEAN(rf_mcpair_freelist, next, (RF_MCPair_t *), clean_mcpair);
92 }
93
94 int
95 rf_ConfigureMCPair(listp)
96 RF_ShutdownList_t **listp;
97 {
98 int rc;
99
100 RF_FREELIST_CREATE(rf_mcpair_freelist, RF_MAX_FREE_MCPAIR,
101 RF_MCPAIR_INC, sizeof(RF_MCPair_t));
102 rc = rf_ShutdownCreate(listp, rf_ShutdownMCPair, NULL);
103 if (rc) {
104 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
105 __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