mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-31 14:35:26 +00:00
Supporting type and routines for ondestroy event notifcation.
This commit is contained in:
106
lib/isc/include/isc/ondestroy.h
Normal file
106
lib/isc/include/isc/ondestroy.h
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (C) 2000 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
||||
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef ISC_ONDESTROY_H
|
||||
#define ISC_ONDESTROY_H 1
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include <isc/event.h>
|
||||
|
||||
ISC_LANG_BEGINDECLS
|
||||
|
||||
/*
|
||||
* ondestroy handling.
|
||||
*
|
||||
* Any class ``X'' of objects that wants to send out notifications
|
||||
* on its destruction should declare a field of type isc_ondestroy_t
|
||||
* (call it 'ondest').
|
||||
*
|
||||
* typedef struct {
|
||||
* ...
|
||||
* isc_ondestroy_t ondest;
|
||||
* ...
|
||||
* } X;
|
||||
*
|
||||
* When an object ``A'' of type X is created
|
||||
* it must initialize the field ondest with a call to
|
||||
*
|
||||
* isc_ondestroy_init(&A->ondest).
|
||||
*
|
||||
* X should also provide a registration function for third-party
|
||||
* objects to call to register their interest in being told about
|
||||
* the destruction of a particular instance of X.
|
||||
*
|
||||
* isc_result_t
|
||||
* X_ondestroy(X *instance, isc_task_t *task,
|
||||
* isc_event_t **eventp) {
|
||||
* return(isc_ondestroy_register(&instance->ondest, task,eventp));
|
||||
* }
|
||||
*
|
||||
* Note: locking of the ondestory structure embedded inside of X, is
|
||||
* X's responsibility.
|
||||
*
|
||||
* When an instance of X is destroyed, a call to isc_ondestroy_notify()
|
||||
* sends the notifications:
|
||||
*
|
||||
* X *instance;
|
||||
* isc_ondestroy_t ondest = instance->ondest;
|
||||
*
|
||||
* ... completely cleanup 'instance' here...
|
||||
*
|
||||
* isc_ondestroy_notify(&ondest, instance);
|
||||
*
|
||||
*
|
||||
* see dns/zone.c for an ifdef'd-out example.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
unsigned int magic;
|
||||
isc_eventlist_t events;
|
||||
} isc_ondestroy_t;
|
||||
|
||||
void isc_ondestroy_init(isc_ondestroy_t *ondest);
|
||||
/*
|
||||
* Initialize the on ondest structure. *must* be called before first call
|
||||
* to isc_ondestroy_register().
|
||||
*/
|
||||
|
||||
isc_result_t isc_ondestroy_register(isc_ondestroy_t *ondest,
|
||||
isc_task_t *task,
|
||||
isc_event_t **eventp);
|
||||
|
||||
/*
|
||||
* Stores task and *eventp away inside *ondest. Ownership of **event is
|
||||
* taken from the caller (and *eventp is set to NULL). The task is attached
|
||||
* to.
|
||||
*/
|
||||
|
||||
|
||||
void isc_ondestroy_notify(isc_ondestroy_t *ondest, void *sender);
|
||||
/*
|
||||
* Dispatches the event(s) to the task(s) that were given in
|
||||
* isc_ondestroy_register call(s) (done via calls to
|
||||
* isc_task_sendanddetach()). Before dispatch, the sender value of each
|
||||
* event structure is set to the value of the sender paramater. The
|
||||
* internal structures of the ondest parameter are cleaned out, so no other
|
||||
* cleanup is needed.
|
||||
*/
|
||||
|
||||
ISC_LANG_ENDDECLS
|
||||
|
||||
#endif /* ISC_ONDESTROY_H */
|
83
lib/isc/ondestroy.c
Normal file
83
lib/isc/ondestroy.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (C) 2000 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
||||
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <isc/assertions.h>
|
||||
#include <isc/ondestroy.h>
|
||||
#include <isc/result.h>
|
||||
#include <isc/task.h>
|
||||
|
||||
#define ONDESTROY_MAGIC 0x44655374 /* DeSt */
|
||||
#define VALID_ONDESTROY(s) (s != NULL && (s->magic == ONDESTROY_MAGIC))
|
||||
|
||||
void
|
||||
isc_ondestroy_init(isc_ondestroy_t *ondest)
|
||||
{
|
||||
ondest->magic = ONDESTROY_MAGIC;
|
||||
ISC_LIST_INIT(ondest->events);
|
||||
}
|
||||
|
||||
|
||||
isc_result_t
|
||||
isc_ondestroy_register(isc_ondestroy_t *ondest,
|
||||
isc_task_t *task, isc_event_t **eventp)
|
||||
{
|
||||
isc_event_t *theevent;
|
||||
isc_task_t *thetask = NULL;
|
||||
|
||||
REQUIRE(VALID_ONDESTROY(ondest));
|
||||
REQUIRE(task != NULL);
|
||||
REQUIRE(eventp != NULL);
|
||||
|
||||
theevent = *eventp;
|
||||
|
||||
REQUIRE(theevent != NULL);
|
||||
|
||||
isc_task_attach(task, &thetask);
|
||||
|
||||
theevent->sender = thetask;
|
||||
|
||||
ISC_LIST_APPEND(ondest->events, theevent, link);
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
isc_ondestroy_notify(isc_ondestroy_t *ondest, void *sender)
|
||||
{
|
||||
isc_event_t *eventp;
|
||||
isc_task_t *task;
|
||||
|
||||
REQUIRE(VALID_ONDESTROY(ondest));
|
||||
|
||||
eventp = ISC_LIST_HEAD(ondest->events);
|
||||
while (eventp != NULL) {
|
||||
ISC_LIST_UNLINK(ondest->events, eventp, link);
|
||||
|
||||
task = eventp->sender;
|
||||
eventp->sender = sender;
|
||||
|
||||
isc_task_sendanddetach(&task, &eventp);
|
||||
|
||||
eventp = ISC_LIST_HEAD(ondest->events);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user