rf_shutdown.c revision 1.2 1 /* $NetBSD: rf_shutdown.c,v 1.2 1999/01/14 22:49:05 thorpej Exp $ */
2 /*
3 * rf_shutdown.c
4 */
5 /*
6 * Copyright (c) 1996 Carnegie-Mellon University.
7 * All rights reserved.
8 *
9 * Author: Jim Zelenka
10 *
11 * Permission to use, copy, modify and distribute this software and
12 * its documentation is hereby granted, provided that both the copyright
13 * notice and this permission notice appear in all copies of the
14 * software, derivative works or modified versions, and any portions
15 * thereof, and that both notices appear in supporting documentation.
16 *
17 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
19 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20 *
21 * Carnegie Mellon requests users of this software to return to
22 *
23 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
24 * School of Computer Science
25 * Carnegie Mellon University
26 * Pittsburgh PA 15213-3890
27 *
28 * any improvements or extensions that they make and grant Carnegie the
29 * rights to redistribute these changes.
30 */
31 /*
32 * Maintain lists of cleanup functions. Also, mechanisms for coordinating
33 * thread startup and shutdown.
34 */
35
36 #include "rf_types.h"
37 #include "rf_threadstuff.h"
38 #include "rf_shutdown.h"
39 #include "rf_debugMem.h"
40 #include "rf_freelist.h"
41 #include "rf_threadid.h"
42
43 static void rf_FreeShutdownEnt(RF_ShutdownList_t *ent)
44 {
45 #ifdef KERNEL
46 #ifdef __NetBSD__
47 FREE(ent, M_RAIDFRAME);
48 #else
49 FREE(ent, M_DEVBUF);
50 #endif /* __NetBSD__ */
51 #else /* KERNEL */
52 free(ent);
53 #endif /* KERNEL */
54 }
55
56 int _rf_ShutdownCreate(
57 RF_ShutdownList_t **listp,
58 void (*cleanup)(void *arg),
59 void *arg,
60 char *file,
61 int line)
62 {
63 RF_ShutdownList_t *ent;
64
65 /*
66 * Have to directly allocate memory here, since we start up before
67 * and shutdown after RAIDframe internal allocation system.
68 */
69 #ifdef KERNEL
70 #ifdef __NetBSD__
71 ent = (RF_ShutdownList_t *)malloc( sizeof(RF_ShutdownList_t), M_RAIDFRAME, M_WAITOK);
72 #if 0
73 MALLOC(ent, RF_ShutdownList_t *, sizeof(RF_ShutdownList_t), M_RAIDFRAME, M_WAITOK);
74 #endif
75 #else
76 ent = (RF_ShutdownList_t *)malloc( sizeof(RF_ShutdownList_t), M_DEVBUF, M_WAITOK);
77 #if 0
78 MALLOC(ent, RF_ShutdownList_t *, sizeof(RF_ShutdownList_t), M_DEVBUF, M_WAITOK);
79 #endif
80 #endif /* __NetBSD__ */
81 #else /* KERNEL */
82 ent = (RF_ShutdownList_t *)malloc(sizeof(RF_ShutdownList_t));
83 #endif /* KERNEL */
84 if (ent == NULL)
85 return(ENOMEM);
86 ent->cleanup = cleanup;
87 ent->arg = arg;
88 ent->file = file;
89 ent->line = line;
90 ent->next = *listp;
91 *listp = ent;
92 return(0);
93 }
94
95 int rf_ShutdownList(RF_ShutdownList_t **list)
96 {
97 RF_ShutdownList_t *r, *next;
98 char *file;
99 int line;
100
101 for(r=*list;r;r=next) {
102 next = r->next;
103 file = r->file;
104 line = r->line;
105
106 if (rf_shutdownDebug) {
107 int tid;
108 rf_get_threadid(tid);
109 printf("[%d] call shutdown, created %s:%d\n", tid, file, line);
110 }
111
112 r->cleanup(r->arg);
113
114 if (rf_shutdownDebug) {
115 int tid;
116 rf_get_threadid(tid);
117 printf("[%d] completed shutdown, created %s:%d\n", tid, file, line);
118 }
119
120 rf_FreeShutdownEnt(r);
121 }
122 *list = NULL;
123 return(0);
124 }
125