Merge from vendor branch CVS:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / ondestroy.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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.11.2.1 2004/03/09 06:11:50 marka Exp $ */
19
20 #include <config.h>
21
22 #include <stddef.h>
23
24 #include <isc/event.h>
25 #include <isc/magic.h>
26 #include <isc/ondestroy.h>
27 #include <isc/task.h>
28 #include <isc/util.h>
29
30 #define ONDESTROY_MAGIC         ISC_MAGIC('D', 'e', 'S', 't')
31 #define VALID_ONDESTROY(s)      ISC_MAGIC_VALID(s, ONDESTROY_MAGIC)
32
33 void
34 isc_ondestroy_init(isc_ondestroy_t *ondest) {
35         ondest->magic = ONDESTROY_MAGIC;
36         ISC_LIST_INIT(ondest->events);
37 }
38
39 isc_result_t
40 isc_ondestroy_register(isc_ondestroy_t *ondest, isc_task_t *task,
41                        isc_event_t **eventp)
42 {
43         isc_event_t *theevent;
44         isc_task_t *thetask = NULL;
45
46         REQUIRE(VALID_ONDESTROY(ondest));
47         REQUIRE(task != NULL);
48         REQUIRE(eventp != NULL);
49
50         theevent = *eventp;
51
52         REQUIRE(theevent != NULL);
53
54         isc_task_attach(task, &thetask);
55
56         theevent->ev_sender = thetask;
57
58         ISC_LIST_APPEND(ondest->events, theevent, ev_link);
59
60         return (ISC_R_SUCCESS);
61 }
62
63 void
64 isc_ondestroy_notify(isc_ondestroy_t *ondest, void *sender) {
65         isc_event_t *eventp;
66         isc_task_t *task;
67
68         REQUIRE(VALID_ONDESTROY(ondest));
69
70         eventp = ISC_LIST_HEAD(ondest->events);
71         while (eventp != NULL) {
72                 ISC_LIST_UNLINK(ondest->events, eventp, ev_link);
73
74                 task = eventp->ev_sender;
75                 eventp->ev_sender = sender;
76
77                 isc_task_sendanddetach(&task, &eventp);
78
79                 eventp = ISC_LIST_HEAD(ondest->events);
80         }
81 }
82
83