2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (c) 1996-1999 by Internet Software Consortium
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A/* ev_waits.c - implement deferred function calls for the eventlib
2N/A * vix 05dec95 [initial]
2N/A */
2N/A
2N/A#if !defined(LINT) && !defined(CODECENTER)
2N/Astatic const char rcsid[] = "$Id: ev_waits.c,v 1.4 2005/04/27 04:56:36 sra Exp $";
2N/A#endif
2N/A
2N/A#include "port_before.h"
2N/A#include "fd_setsize.h"
2N/A
2N/A#include <errno.h>
2N/A
2N/A#include <isc/eventlib.h>
2N/A#include <isc/assertions.h>
2N/A#include "eventlib_p.h"
2N/A
2N/A#include "port_after.h"
2N/A
2N/A/* Forward. */
2N/A
2N/Astatic void print_waits(evContext_p *ctx);
2N/Astatic evWaitList * evNewWaitList(evContext_p *);
2N/Astatic void evFreeWaitList(evContext_p *, evWaitList *);
2N/Astatic evWaitList * evGetWaitList(evContext_p *, const void *, int);
2N/A
2N/A
2N/A/* Public. */
2N/A
2N/A/*%
2N/A * Enter a new wait function on the queue.
2N/A */
2N/Aint
2N/AevWaitFor(evContext opaqueCtx, const void *tag,
2N/A evWaitFunc func, void *uap, evWaitID *id)
2N/A{
2N/A evContext_p *ctx = opaqueCtx.opaque;
2N/A evWait *new;
2N/A evWaitList *wl = evGetWaitList(ctx, tag, 1);
2N/A
2N/A OKNEW(new);
2N/A new->func = func;
2N/A new->uap = uap;
2N/A new->tag = tag;
2N/A new->next = NULL;
2N/A if (wl->last != NULL)
2N/A wl->last->next = new;
2N/A else
2N/A wl->first = new;
2N/A wl->last = new;
2N/A if (id != NULL)
2N/A id->opaque = new;
2N/A if (ctx->debug >= 9)
2N/A print_waits(ctx);
2N/A return (0);
2N/A}
2N/A
2N/A/*%
2N/A * Mark runnable all waiting functions having a certain tag.
2N/A */
2N/Aint
2N/AevDo(evContext opaqueCtx, const void *tag) {
2N/A evContext_p *ctx = opaqueCtx.opaque;
2N/A evWaitList *wl = evGetWaitList(ctx, tag, 0);
2N/A evWait *first;
2N/A
2N/A if (!wl) {
2N/A errno = ENOENT;
2N/A return (-1);
2N/A }
2N/A
2N/A first = wl->first;
2N/A INSIST(first != NULL);
2N/A
2N/A if (ctx->waitDone.last != NULL)
2N/A ctx->waitDone.last->next = first;
2N/A else
2N/A ctx->waitDone.first = first;
2N/A ctx->waitDone.last = wl->last;
2N/A evFreeWaitList(ctx, wl);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*%
2N/A * Remove a waiting (or ready to run) function from the queue.
2N/A */
2N/Aint
2N/AevUnwait(evContext opaqueCtx, evWaitID id) {
2N/A evContext_p *ctx = opaqueCtx.opaque;
2N/A evWait *this, *prev;
2N/A evWaitList *wl;
2N/A int found = 0;
2N/A
2N/A this = id.opaque;
2N/A INSIST(this != NULL);
2N/A wl = evGetWaitList(ctx, this->tag, 0);
2N/A if (wl != NULL) {
2N/A for (prev = NULL, this = wl->first;
2N/A this != NULL;
2N/A prev = this, this = this->next)
2N/A if (this == (evWait *)id.opaque) {
2N/A found = 1;
2N/A if (prev != NULL)
2N/A prev->next = this->next;
2N/A else
2N/A wl->first = this->next;
2N/A if (wl->last == this)
2N/A wl->last = prev;
2N/A if (wl->first == NULL)
2N/A evFreeWaitList(ctx, wl);
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (!found) {
2N/A /* Maybe it's done */
2N/A for (prev = NULL, this = ctx->waitDone.first;
2N/A this != NULL;
2N/A prev = this, this = this->next)
2N/A if (this == (evWait *)id.opaque) {
2N/A found = 1;
2N/A if (prev != NULL)
2N/A prev->next = this->next;
2N/A else
2N/A ctx->waitDone.first = this->next;
2N/A if (ctx->waitDone.last == this)
2N/A ctx->waitDone.last = prev;
2N/A break;
2N/A }
2N/A }
2N/A
2N/A if (!found) {
2N/A errno = ENOENT;
2N/A return (-1);
2N/A }
2N/A
2N/A FREE(this);
2N/A
2N/A if (ctx->debug >= 9)
2N/A print_waits(ctx);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/AevDefer(evContext opaqueCtx, evWaitFunc func, void *uap) {
2N/A evContext_p *ctx = opaqueCtx.opaque;
2N/A evWait *new;
2N/A
2N/A OKNEW(new);
2N/A new->func = func;
2N/A new->uap = uap;
2N/A new->tag = NULL;
2N/A new->next = NULL;
2N/A if (ctx->waitDone.last != NULL)
2N/A ctx->waitDone.last->next = new;
2N/A else
2N/A ctx->waitDone.first = new;
2N/A ctx->waitDone.last = new;
2N/A if (ctx->debug >= 9)
2N/A print_waits(ctx);
2N/A return (0);
2N/A}
2N/A
2N/A/* Private. */
2N/A
2N/Astatic void
2N/Aprint_waits(evContext_p *ctx) {
2N/A evWaitList *wl;
2N/A evWait *this;
2N/A
2N/A evPrintf(ctx, 9, "wait waiting:\n");
2N/A for (wl = ctx->waitLists; wl != NULL; wl = wl->next) {
2N/A INSIST(wl->first != NULL);
2N/A evPrintf(ctx, 9, " tag %p:", wl->first->tag);
2N/A for (this = wl->first; this != NULL; this = this->next)
2N/A evPrintf(ctx, 9, " %p", this);
2N/A evPrintf(ctx, 9, "\n");
2N/A }
2N/A evPrintf(ctx, 9, "wait done:");
2N/A for (this = ctx->waitDone.first; this != NULL; this = this->next)
2N/A evPrintf(ctx, 9, " %p", this);
2N/A evPrintf(ctx, 9, "\n");
2N/A}
2N/A
2N/Astatic evWaitList *
2N/AevNewWaitList(evContext_p *ctx) {
2N/A evWaitList *new;
2N/A
2N/A NEW(new);
2N/A if (new == NULL)
2N/A return (NULL);
2N/A new->first = new->last = NULL;
2N/A new->prev = NULL;
2N/A new->next = ctx->waitLists;
2N/A if (new->next != NULL)
2N/A new->next->prev = new;
2N/A ctx->waitLists = new;
2N/A return (new);
2N/A}
2N/A
2N/Astatic void
2N/AevFreeWaitList(evContext_p *ctx, evWaitList *this) {
2N/A
2N/A INSIST(this != NULL);
2N/A
2N/A if (this->prev != NULL)
2N/A this->prev->next = this->next;
2N/A else
2N/A ctx->waitLists = this->next;
2N/A if (this->next != NULL)
2N/A this->next->prev = this->prev;
2N/A FREE(this);
2N/A}
2N/A
2N/Astatic evWaitList *
2N/AevGetWaitList(evContext_p *ctx, const void *tag, int should_create) {
2N/A evWaitList *this;
2N/A
2N/A for (this = ctx->waitLists; this != NULL; this = this->next) {
2N/A if (this->first != NULL && this->first->tag == tag)
2N/A break;
2N/A }
2N/A if (this == NULL && should_create)
2N/A this = evNewWaitList(ctx);
2N/A return (this);
2N/A}
2N/A
2N/A/*! \file */