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