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 #if !defined(LINT) && !defined(CODECENTER)
23 static const char rcsid
[] = "$Id: ev_waits.c,v 1.4 2005/04/27 04:56:36 sra Exp $";
26 #include "port_before.h"
27 #include "fd_setsize.h"
31 #include <isc/eventlib.h>
32 #include <isc/assertions.h>
33 #include "eventlib_p.h"
35 #include "port_after.h"
39 static void print_waits(evContext_p
*ctx
);
40 static evWaitList
* evNewWaitList(evContext_p
*);
41 static void evFreeWaitList(evContext_p
*, evWaitList
*);
42 static evWaitList
* evGetWaitList(evContext_p
*, const void *, int);
48 * Enter a new wait function on the queue.
51 evWaitFor(evContext opaqueCtx
, const void *tag
,
52 evWaitFunc func
, void *uap
, evWaitID
*id
)
54 evContext_p
*ctx
= opaqueCtx
.opaque
;
56 evWaitList
*wl
= evGetWaitList(ctx
, tag
, 1);
76 * Mark runnable all waiting functions having a certain tag.
79 evDo(evContext opaqueCtx
, const void *tag
) {
80 evContext_p
*ctx
= opaqueCtx
.opaque
;
81 evWaitList
*wl
= evGetWaitList(ctx
, tag
, 0);
90 INSIST(first
!= NULL
);
92 if (ctx
->waitDone
.last
!= NULL
)
93 ctx
->waitDone
.last
->next
= first
;
95 ctx
->waitDone
.first
= first
;
96 ctx
->waitDone
.last
= wl
->last
;
97 evFreeWaitList(ctx
, wl
);
103 * Remove a waiting (or ready to run) function from the queue.
106 evUnwait(evContext opaqueCtx
, evWaitID id
) {
107 evContext_p
*ctx
= opaqueCtx
.opaque
;
113 INSIST(this != NULL
);
114 wl
= evGetWaitList(ctx
, this->tag
, 0);
116 for (prev
= NULL
, this = wl
->first
;
118 prev
= this, this = this->next
)
119 if (this == (evWait
*)id
.opaque
) {
122 prev
->next
= this->next
;
124 wl
->first
= this->next
;
125 if (wl
->last
== this)
127 if (wl
->first
== NULL
)
128 evFreeWaitList(ctx
, wl
);
134 /* Maybe it's done */
135 for (prev
= NULL
, this = ctx
->waitDone
.first
;
137 prev
= this, this = this->next
)
138 if (this == (evWait
*)id
.opaque
) {
141 prev
->next
= this->next
;
143 ctx
->waitDone
.first
= this->next
;
144 if (ctx
->waitDone
.last
== this)
145 ctx
->waitDone
.last
= prev
;
164 evDefer(evContext opaqueCtx
, evWaitFunc func
, void *uap
) {
165 evContext_p
*ctx
= opaqueCtx
.opaque
;
173 if (ctx
->waitDone
.last
!= NULL
)
174 ctx
->waitDone
.last
->next
= new;
176 ctx
->waitDone
.first
= new;
177 ctx
->waitDone
.last
= new;
186 print_waits(evContext_p
*ctx
) {
190 evPrintf(ctx
, 9, "wait waiting:\n");
191 for (wl
= ctx
->waitLists
; wl
!= NULL
; wl
= wl
->next
) {
192 INSIST(wl
->first
!= NULL
);
193 evPrintf(ctx
, 9, " tag %p:", wl
->first
->tag
);
194 for (this = wl
->first
; this != NULL
; this = this->next
)
195 evPrintf(ctx
, 9, " %p", this);
196 evPrintf(ctx
, 9, "\n");
198 evPrintf(ctx
, 9, "wait done:");
199 for (this = ctx
->waitDone
.first
; this != NULL
; this = this->next
)
200 evPrintf(ctx
, 9, " %p", this);
201 evPrintf(ctx
, 9, "\n");
205 evNewWaitList(evContext_p
*ctx
) {
211 new->first
= new->last
= NULL
;
213 new->next
= ctx
->waitLists
;
214 if (new->next
!= NULL
)
215 new->next
->prev
= new;
216 ctx
->waitLists
= new;
221 evFreeWaitList(evContext_p
*ctx
, evWaitList
*this) {
223 INSIST(this != NULL
);
225 if (this->prev
!= NULL
)
226 this->prev
->next
= this->next
;
228 ctx
->waitLists
= this->next
;
229 if (this->next
!= NULL
)
230 this->next
->prev
= this->prev
;
235 evGetWaitList(evContext_p
*ctx
, const void *tag
, int should_create
) {
238 for (this = ctx
->waitLists
; this != NULL
; this = this->next
) {
239 if (this->first
!= NULL
&& this->first
->tag
== tag
)
242 if (this == NULL
&& should_create
)
243 this = evNewWaitList(ctx
);