xref: /freebsd/contrib/ntp/libntp/lib/isc/ondestroy.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1 /*
2  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* $Id: ondestroy.c,v 1.16 2007/06/19 23:47:17 tbox Exp $ */
19 
20 /*! \file */
21 
22 #include <config.h>
23 
24 #include <stddef.h>
25 
26 #include <isc/event.h>
27 #include <isc/magic.h>
28 #include <isc/ondestroy.h>
29 #include <isc/task.h>
30 #include <isc/util.h>
31 
32 #define ONDESTROY_MAGIC		ISC_MAGIC('D', 'e', 'S', 't')
33 #define VALID_ONDESTROY(s)	ISC_MAGIC_VALID(s, ONDESTROY_MAGIC)
34 
35 void
isc_ondestroy_init(isc_ondestroy_t * ondest)36 isc_ondestroy_init(isc_ondestroy_t *ondest) {
37 	ondest->magic = ONDESTROY_MAGIC;
38 	ISC_LIST_INIT(ondest->events);
39 }
40 
41 isc_result_t
isc_ondestroy_register(isc_ondestroy_t * ondest,isc_task_t * task,isc_event_t ** eventp)42 isc_ondestroy_register(isc_ondestroy_t *ondest, isc_task_t *task,
43 		       isc_event_t **eventp)
44 {
45 	isc_event_t *theevent;
46 	isc_task_t *thetask = NULL;
47 
48 	REQUIRE(VALID_ONDESTROY(ondest));
49 	REQUIRE(task != NULL);
50 	REQUIRE(eventp != NULL);
51 
52 	theevent = *eventp;
53 
54 	REQUIRE(theevent != NULL);
55 
56 	isc_task_attach(task, &thetask);
57 
58 	theevent->ev_sender = thetask;
59 
60 	ISC_LIST_APPEND(ondest->events, theevent, ev_link);
61 
62 	return (ISC_R_SUCCESS);
63 }
64 
65 void
isc_ondestroy_notify(isc_ondestroy_t * ondest,void * sender)66 isc_ondestroy_notify(isc_ondestroy_t *ondest, void *sender) {
67 	isc_event_t *eventp;
68 	isc_task_t *task;
69 
70 	REQUIRE(VALID_ONDESTROY(ondest));
71 
72 	eventp = ISC_LIST_HEAD(ondest->events);
73 	while (eventp != NULL) {
74 		ISC_LIST_UNLINK(ondest->events, eventp, ev_link);
75 
76 		task = eventp->ev_sender;
77 		eventp->ev_sender = sender;
78 
79 		isc_task_sendanddetach(&task, &eventp);
80 
81 		eventp = ISC_LIST_HEAD(ondest->events);
82 	}
83 }
84 
85 
86