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