xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/ondestroy.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: ondestroy.c,v 1.2 2024/08/18 20:47:14 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos /*
4897be3a4Schristos  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5897be3a4Schristos  * Copyright (C) 2000, 2001  Internet Software Consortium.
6897be3a4Schristos  *
7897be3a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8897be3a4Schristos  * purpose with or without fee is hereby granted, provided that the above
9897be3a4Schristos  * copyright notice and this permission notice appear in all copies.
10897be3a4Schristos  *
11897be3a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12897be3a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13897be3a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14897be3a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15897be3a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16897be3a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17897be3a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18897be3a4Schristos  */
19897be3a4Schristos 
20897be3a4Schristos /* Id: ondestroy.c,v 1.16 2007/06/19 23:47:17 tbox Exp  */
21897be3a4Schristos 
22897be3a4Schristos /*! \file */
23897be3a4Schristos 
24897be3a4Schristos #include <config.h>
25897be3a4Schristos 
26897be3a4Schristos #include <stddef.h>
27897be3a4Schristos 
28897be3a4Schristos #include <isc/event.h>
29897be3a4Schristos #include <isc/magic.h>
30897be3a4Schristos #include <isc/ondestroy.h>
31897be3a4Schristos #include <isc/task.h>
32897be3a4Schristos #include <isc/util.h>
33897be3a4Schristos 
34897be3a4Schristos #define ONDESTROY_MAGIC		ISC_MAGIC('D', 'e', 'S', 't')
35897be3a4Schristos #define VALID_ONDESTROY(s)	ISC_MAGIC_VALID(s, ONDESTROY_MAGIC)
36897be3a4Schristos 
37897be3a4Schristos void
38897be3a4Schristos isc_ondestroy_init(isc_ondestroy_t *ondest) {
39897be3a4Schristos 	ondest->magic = ONDESTROY_MAGIC;
40897be3a4Schristos 	ISC_LIST_INIT(ondest->events);
41897be3a4Schristos }
42897be3a4Schristos 
43897be3a4Schristos isc_result_t
44897be3a4Schristos isc_ondestroy_register(isc_ondestroy_t *ondest, isc_task_t *task,
45897be3a4Schristos 		       isc_event_t **eventp)
46897be3a4Schristos {
47897be3a4Schristos 	isc_event_t *theevent;
48897be3a4Schristos 	isc_task_t *thetask = NULL;
49897be3a4Schristos 
50897be3a4Schristos 	REQUIRE(VALID_ONDESTROY(ondest));
51897be3a4Schristos 	REQUIRE(task != NULL);
52897be3a4Schristos 	REQUIRE(eventp != NULL);
53897be3a4Schristos 
54897be3a4Schristos 	theevent = *eventp;
55897be3a4Schristos 
56897be3a4Schristos 	REQUIRE(theevent != NULL);
57897be3a4Schristos 
58897be3a4Schristos 	isc_task_attach(task, &thetask);
59897be3a4Schristos 
60897be3a4Schristos 	theevent->ev_sender = thetask;
61897be3a4Schristos 
62897be3a4Schristos 	ISC_LIST_APPEND(ondest->events, theevent, ev_link);
63897be3a4Schristos 
64897be3a4Schristos 	return (ISC_R_SUCCESS);
65897be3a4Schristos }
66897be3a4Schristos 
67897be3a4Schristos void
68897be3a4Schristos isc_ondestroy_notify(isc_ondestroy_t *ondest, void *sender) {
69897be3a4Schristos 	isc_event_t *eventp;
70897be3a4Schristos 	isc_task_t *task;
71897be3a4Schristos 
72897be3a4Schristos 	REQUIRE(VALID_ONDESTROY(ondest));
73897be3a4Schristos 
74897be3a4Schristos 	eventp = ISC_LIST_HEAD(ondest->events);
75897be3a4Schristos 	while (eventp != NULL) {
76897be3a4Schristos 		ISC_LIST_UNLINK(ondest->events, eventp, ev_link);
77897be3a4Schristos 
78897be3a4Schristos 		task = eventp->ev_sender;
79897be3a4Schristos 		eventp->ev_sender = sender;
80897be3a4Schristos 
81897be3a4Schristos 		isc_task_sendanddetach(&task, &eventp);
82897be3a4Schristos 
83897be3a4Schristos 		eventp = ISC_LIST_HEAD(ondest->events);
84897be3a4Schristos 	}
85897be3a4Schristos }
86897be3a4Schristos 
87897be3a4Schristos 
88