1 /* $NetBSD: rf_mcpair.c,v 1.25 2021/07/23 00:54:45 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@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.25 2021/07/23 00:54:45 oster 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_general.h"
44 #include "rf_shutdown.h"
45 #include "rf_netbsd.h"
46 #include "rf_raid.h"
47
48 #include <sys/pool.h>
49 #include <sys/proc.h>
50
51 #define RF_MAX_FREE_MCPAIR 128
52 #define RF_MIN_FREE_MCPAIR 24
53
54 static void rf_ShutdownMCPair(void *);
55
56 static void
rf_ShutdownMCPair(void * arg)57 rf_ShutdownMCPair(void *arg)
58 {
59 RF_Raid_t *raidPtr;
60
61 raidPtr = (RF_Raid_t *) arg;
62
63 pool_destroy(&raidPtr->pools.mcpair);
64 }
65
66 int
rf_ConfigureMCPair(RF_ShutdownList_t ** listp,RF_Raid_t * raidPtr,RF_Config_t * cfgPtr)67 rf_ConfigureMCPair(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
68 RF_Config_t *cfgPtr)
69 {
70
71 rf_pool_init(raidPtr, raidPtr->poolNames.mcpair, &raidPtr->pools.mcpair, sizeof(RF_MCPair_t),
72 "mcpair", RF_MIN_FREE_MCPAIR, RF_MAX_FREE_MCPAIR);
73 rf_ShutdownCreate(listp, rf_ShutdownMCPair, raidPtr);
74
75 return (0);
76 }
77
78 RF_MCPair_t *
rf_AllocMCPair(RF_Raid_t * raidPtr)79 rf_AllocMCPair(RF_Raid_t *raidPtr)
80 {
81 RF_MCPair_t *t;
82
83 t = pool_get(&raidPtr->pools.mcpair, PR_WAITOK);
84 rf_init_mutex2(t->mutex, IPL_VM);
85 rf_init_cond2(t->cond, "mcpair");
86 t->flag = 0;
87
88 return (t);
89 }
90
91 void
rf_FreeMCPair(RF_Raid_t * raidPtr,RF_MCPair_t * t)92 rf_FreeMCPair(RF_Raid_t *raidPtr, RF_MCPair_t *t)
93 {
94 rf_destroy_cond2(t->cond);
95 rf_destroy_mutex2(t->mutex);
96 pool_put(&raidPtr->pools.mcpair, t);
97 }
98
99 /* the callback function used to wake you up when you use an mcpair to
100 wait for something */
101 void
rf_MCPairWakeupFunc(RF_MCPair_t * mcpair)102 rf_MCPairWakeupFunc(RF_MCPair_t *mcpair)
103 {
104 RF_LOCK_MCPAIR(mcpair);
105 mcpair->flag = 1;
106 rf_broadcast_cond2(mcpair->cond);
107 RF_UNLOCK_MCPAIR(mcpair);
108 }
109