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