rf_mcpair.c revision 1.5 1 /* $NetBSD: rf_mcpair.c,v 1.5 2001/09/26 02:52:57 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_archs.h"
36 #include "rf_threadstuff.h"
37 #include "rf_mcpair.h"
38 #include "rf_debugMem.h"
39 #include "rf_freelist.h"
40 #include "rf_shutdown.h"
41
42 #include <sys/proc.h>
43
44 static RF_FreeList_t *rf_mcpair_freelist;
45
46 #define RF_MAX_FREE_MCPAIR 128
47 #define RF_MCPAIR_INC 16
48 #define RF_MCPAIR_INITIAL 24
49
50 static int init_mcpair(RF_MCPair_t *);
51 static void clean_mcpair(RF_MCPair_t *);
52 static void rf_ShutdownMCPair(void *);
53
54
55
56 static int
57 init_mcpair(t)
58 RF_MCPair_t *t;
59 {
60 int rc;
61
62 rc = rf_mutex_init(&t->mutex);
63 if (rc) {
64 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
65 __LINE__, rc);
66 return (rc);
67 }
68 rc = rf_cond_init(&t->cond);
69 if (rc) {
70 RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
71 __LINE__, rc);
72 rf_mutex_destroy(&t->mutex);
73 return (rc);
74 }
75 return (0);
76 }
77
78 static void
79 clean_mcpair(t)
80 RF_MCPair_t *t;
81 {
82 rf_mutex_destroy(&t->mutex);
83 rf_cond_destroy(&t->cond);
84 }
85
86 static void
87 rf_ShutdownMCPair(ignored)
88 void *ignored;
89 {
90 RF_FREELIST_DESTROY_CLEAN(rf_mcpair_freelist, next, (RF_MCPair_t *), clean_mcpair);
91 }
92
93 int
94 rf_ConfigureMCPair(listp)
95 RF_ShutdownList_t **listp;
96 {
97 int rc;
98
99 RF_FREELIST_CREATE(rf_mcpair_freelist, RF_MAX_FREE_MCPAIR,
100 RF_MCPAIR_INC, sizeof(RF_MCPair_t));
101 rc = rf_ShutdownCreate(listp, rf_ShutdownMCPair, NULL);
102 if (rc) {
103 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
104 __FILE__, __LINE__, rc);
105 rf_ShutdownMCPair(NULL);
106 return (rc);
107 }
108 RF_FREELIST_PRIME_INIT(rf_mcpair_freelist, RF_MCPAIR_INITIAL, next,
109 (RF_MCPair_t *), init_mcpair);
110 return (0);
111 }
112
113 RF_MCPair_t *
114 rf_AllocMCPair()
115 {
116 RF_MCPair_t *t;
117
118 RF_FREELIST_GET_INIT(rf_mcpair_freelist, t, next, (RF_MCPair_t *), init_mcpair);
119 if (t) {
120 t->flag = 0;
121 t->next = NULL;
122 }
123 return (t);
124 }
125
126 void
127 rf_FreeMCPair(t)
128 RF_MCPair_t *t;
129 {
130 RF_FREELIST_FREE_CLEAN(rf_mcpair_freelist, t, next, clean_mcpair);
131 }
132 /* the callback function used to wake you up when you use an mcpair to wait for something */
133 void
134 rf_MCPairWakeupFunc(mcpair)
135 RF_MCPair_t *mcpair;
136 {
137 RF_LOCK_MUTEX(mcpair->mutex);
138 mcpair->flag = 1;
139 wakeup(&(mcpair->cond));
140 RF_UNLOCK_MUTEX(mcpair->mutex);
141 }
142