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