xref: /netbsd-src/sys/dev/raidframe/rf_shutdown.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: rf_shutdown.c,v 1.6 2000/01/13 23:41:18 oster 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@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 
42 static void
43 rf_FreeShutdownEnt(RF_ShutdownList_t * ent)
44 {
45 	FREE(ent, M_RAIDFRAME);
46 }
47 
48 int
49 _rf_ShutdownCreate(
50     RF_ShutdownList_t ** listp,
51     void (*cleanup) (void *arg),
52     void *arg,
53     char *file,
54     int line)
55 {
56 	RF_ShutdownList_t *ent;
57 
58 	/*
59          * Have to directly allocate memory here, since we start up before
60          * and shutdown after RAIDframe internal allocation system.
61          */
62 	/* 	ent = (RF_ShutdownList_t *) malloc(sizeof(RF_ShutdownList_t),
63 		M_RAIDFRAME, M_WAITOK); */
64 	ent = (RF_ShutdownList_t *) malloc(sizeof(RF_ShutdownList_t),
65 					   M_RAIDFRAME, M_NOWAIT);
66 	if (ent == NULL)
67 		return (ENOMEM);
68 	ent->cleanup = cleanup;
69 	ent->arg = arg;
70 	ent->file = file;
71 	ent->line = line;
72 	ent->next = *listp;
73 	*listp = ent;
74 	return (0);
75 }
76 
77 int
78 rf_ShutdownList(RF_ShutdownList_t ** list)
79 {
80 	RF_ShutdownList_t *r, *next;
81 	char   *file;
82 	int     line;
83 
84 	for (r = *list; r; r = next) {
85 		next = r->next;
86 		file = r->file;
87 		line = r->line;
88 
89 		if (rf_shutdownDebug) {
90 			printf("call shutdown, created %s:%d\n", file, line);
91 		}
92 		r->cleanup(r->arg);
93 
94 		if (rf_shutdownDebug) {
95 			printf("completed shutdown, created %s:%d\n", file, line);
96 		}
97 		rf_FreeShutdownEnt(r);
98 	}
99 	*list = NULL;
100 	return (0);
101 }
102