rf_mcpair.c revision 1.3 1 /* $NetBSD: rf_mcpair.c,v 1.3 1999/02/05 00:06:13 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 "rf_types.h"
35 #include "rf_threadstuff.h"
36 #include "rf_mcpair.h"
37 #include "rf_debugMem.h"
38 #include "rf_freelist.h"
39 #include "rf_shutdown.h"
40
41 #include <sys/proc.h>
42
43 static RF_FreeList_t *rf_mcpair_freelist;
44
45 #define RF_MAX_FREE_MCPAIR 128
46 #define RF_MCPAIR_INC 16
47 #define RF_MCPAIR_INITIAL 24
48
49 static int init_mcpair(RF_MCPair_t *);
50 static void clean_mcpair(RF_MCPair_t *);
51 static void rf_ShutdownMCPair(void *);
52
53
54
55 static int
56 init_mcpair(t)
57 RF_MCPair_t *t;
58 {
59 int rc;
60
61 rc = rf_mutex_init(&t->mutex);
62 if (rc) {
63 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
64 __LINE__, rc);
65 return (rc);
66 }
67 rc = rf_cond_init(&t->cond);
68 if (rc) {
69 RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
70 __LINE__, rc);
71 rf_mutex_destroy(&t->mutex);
72 return (rc);
73 }
74 return (0);
75 }
76
77 static void
78 clean_mcpair(t)
79 RF_MCPair_t *t;
80 {
81 rf_mutex_destroy(&t->mutex);
82 rf_cond_destroy(&t->cond);
83 }
84
85 static void
86 rf_ShutdownMCPair(ignored)
87 void *ignored;
88 {
89 RF_FREELIST_DESTROY_CLEAN(rf_mcpair_freelist, next, (RF_MCPair_t *), clean_mcpair);
90 }
91
92 int
93 rf_ConfigureMCPair(listp)
94 RF_ShutdownList_t **listp;
95 {
96 int rc;
97
98 RF_FREELIST_CREATE(rf_mcpair_freelist, RF_MAX_FREE_MCPAIR,
99 RF_MCPAIR_INC, sizeof(RF_MCPair_t));
100 rc = rf_ShutdownCreate(listp, rf_ShutdownMCPair, NULL);
101 if (rc) {
102 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
103 __FILE__, __LINE__, rc);
104 rf_ShutdownMCPair(NULL);
105 return (rc);
106 }
107 RF_FREELIST_PRIME_INIT(rf_mcpair_freelist, RF_MCPAIR_INITIAL, next,
108 (RF_MCPair_t *), init_mcpair);
109 return (0);
110 }
111
112 RF_MCPair_t *
113 rf_AllocMCPair()
114 {
115 RF_MCPair_t *t;
116
117 RF_FREELIST_GET_INIT(rf_mcpair_freelist, t, next, (RF_MCPair_t *), init_mcpair);
118 if (t) {
119 t->flag = 0;
120 t->next = NULL;
121 }
122 return (t);
123 }
124
125 void
126 rf_FreeMCPair(t)
127 RF_MCPair_t *t;
128 {
129 RF_FREELIST_FREE_CLEAN(rf_mcpair_freelist, t, next, clean_mcpair);
130 }
131 /* the callback function used to wake you up when you use an mcpair to wait for something */
132 void
133 rf_MCPairWakeupFunc(mcpair)
134 RF_MCPair_t *mcpair;
135 {
136 RF_LOCK_MUTEX(mcpair->mutex);
137 mcpair->flag = 1;
138 #if 0
139 printf("MCPairWakeupFunc called!\n");
140 #endif
141 wakeup(&(mcpair->flag));/* XXX Does this do anything useful!! GO */
142 /* XXX Looks like the following is needed to truly get the
143 * functionality they were looking for here... This could be a
144 * side-effect of my using a tsleep in the NetBSD port though... XXX */
145 wakeup(&(mcpair->cond));/* XXX XXX XXX GO */
146 RF_UNLOCK_MUTEX(mcpair->mutex);
147 }
148