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