8849 libresolv2: remove rcsid
[unleashed.git] / usr / src / lib / libresolv2 / common / isc / ev_waits.c
blob9c4e819eb8d5cd1a78becba85683c8da2394bbfc
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1996-1999 by Internet Software Consortium
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.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* ev_waits.c - implement deferred function calls for the eventlib
19 * vix 05dec95 [initial]
22 #include "port_before.h"
23 #include "fd_setsize.h"
25 #include <errno.h>
27 #include <isc/eventlib.h>
28 #include <isc/assertions.h>
29 #include "eventlib_p.h"
31 #include "port_after.h"
33 /* Forward. */
35 static void print_waits(evContext_p *ctx);
36 static evWaitList * evNewWaitList(evContext_p *);
37 static void evFreeWaitList(evContext_p *, evWaitList *);
38 static evWaitList * evGetWaitList(evContext_p *, const void *, int);
41 /* Public. */
43 /*%
44 * Enter a new wait function on the queue.
46 int
47 evWaitFor(evContext opaqueCtx, const void *tag,
48 evWaitFunc func, void *uap, evWaitID *id)
50 evContext_p *ctx = opaqueCtx.opaque;
51 evWait *new;
52 evWaitList *wl = evGetWaitList(ctx, tag, 1);
54 OKNEW(new);
55 new->func = func;
56 new->uap = uap;
57 new->tag = tag;
58 new->next = NULL;
59 if (wl->last != NULL)
60 wl->last->next = new;
61 else
62 wl->first = new;
63 wl->last = new;
64 if (id != NULL)
65 id->opaque = new;
66 if (ctx->debug >= 9)
67 print_waits(ctx);
68 return (0);
71 /*%
72 * Mark runnable all waiting functions having a certain tag.
74 int
75 evDo(evContext opaqueCtx, const void *tag) {
76 evContext_p *ctx = opaqueCtx.opaque;
77 evWaitList *wl = evGetWaitList(ctx, tag, 0);
78 evWait *first;
80 if (!wl) {
81 errno = ENOENT;
82 return (-1);
85 first = wl->first;
86 INSIST(first != NULL);
88 if (ctx->waitDone.last != NULL)
89 ctx->waitDone.last->next = first;
90 else
91 ctx->waitDone.first = first;
92 ctx->waitDone.last = wl->last;
93 evFreeWaitList(ctx, wl);
95 return (0);
98 /*%
99 * Remove a waiting (or ready to run) function from the queue.
102 evUnwait(evContext opaqueCtx, evWaitID id) {
103 evContext_p *ctx = opaqueCtx.opaque;
104 evWait *this, *prev;
105 evWaitList *wl;
106 int found = 0;
108 this = id.opaque;
109 INSIST(this != NULL);
110 wl = evGetWaitList(ctx, this->tag, 0);
111 if (wl != NULL) {
112 for (prev = NULL, this = wl->first;
113 this != NULL;
114 prev = this, this = this->next)
115 if (this == (evWait *)id.opaque) {
116 found = 1;
117 if (prev != NULL)
118 prev->next = this->next;
119 else
120 wl->first = this->next;
121 if (wl->last == this)
122 wl->last = prev;
123 if (wl->first == NULL)
124 evFreeWaitList(ctx, wl);
125 break;
129 if (!found) {
130 /* Maybe it's done */
131 for (prev = NULL, this = ctx->waitDone.first;
132 this != NULL;
133 prev = this, this = this->next)
134 if (this == (evWait *)id.opaque) {
135 found = 1;
136 if (prev != NULL)
137 prev->next = this->next;
138 else
139 ctx->waitDone.first = this->next;
140 if (ctx->waitDone.last == this)
141 ctx->waitDone.last = prev;
142 break;
146 if (!found) {
147 errno = ENOENT;
148 return (-1);
151 FREE(this);
153 if (ctx->debug >= 9)
154 print_waits(ctx);
156 return (0);
160 evDefer(evContext opaqueCtx, evWaitFunc func, void *uap) {
161 evContext_p *ctx = opaqueCtx.opaque;
162 evWait *new;
164 OKNEW(new);
165 new->func = func;
166 new->uap = uap;
167 new->tag = NULL;
168 new->next = NULL;
169 if (ctx->waitDone.last != NULL)
170 ctx->waitDone.last->next = new;
171 else
172 ctx->waitDone.first = new;
173 ctx->waitDone.last = new;
174 if (ctx->debug >= 9)
175 print_waits(ctx);
176 return (0);
179 /* Private. */
181 static void
182 print_waits(evContext_p *ctx) {
183 evWaitList *wl;
184 evWait *this;
186 evPrintf(ctx, 9, "wait waiting:\n");
187 for (wl = ctx->waitLists; wl != NULL; wl = wl->next) {
188 INSIST(wl->first != NULL);
189 evPrintf(ctx, 9, " tag %p:", wl->first->tag);
190 for (this = wl->first; this != NULL; this = this->next)
191 evPrintf(ctx, 9, " %p", this);
192 evPrintf(ctx, 9, "\n");
194 evPrintf(ctx, 9, "wait done:");
195 for (this = ctx->waitDone.first; this != NULL; this = this->next)
196 evPrintf(ctx, 9, " %p", this);
197 evPrintf(ctx, 9, "\n");
200 static evWaitList *
201 evNewWaitList(evContext_p *ctx) {
202 evWaitList *new;
204 NEW(new);
205 if (new == NULL)
206 return (NULL);
207 new->first = new->last = NULL;
208 new->prev = NULL;
209 new->next = ctx->waitLists;
210 if (new->next != NULL)
211 new->next->prev = new;
212 ctx->waitLists = new;
213 return (new);
216 static void
217 evFreeWaitList(evContext_p *ctx, evWaitList *this) {
219 INSIST(this != NULL);
221 if (this->prev != NULL)
222 this->prev->next = this->next;
223 else
224 ctx->waitLists = this->next;
225 if (this->next != NULL)
226 this->next->prev = this->prev;
227 FREE(this);
230 static evWaitList *
231 evGetWaitList(evContext_p *ctx, const void *tag, int should_create) {
232 evWaitList *this;
234 for (this = ctx->waitLists; this != NULL; this = this->next) {
235 if (this->first != NULL && this->first->tag == tag)
236 break;
238 if (this == NULL && should_create)
239 this = evNewWaitList(ctx);
240 return (this);
243 /*! \file */