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