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"
27 #include <isc/eventlib.h>
28 #include <isc/assertions.h>
29 #include "eventlib_p.h"
31 #include "port_after.h"
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);
44 * Enter a new wait function on the queue.
47 evWaitFor(evContext opaqueCtx
, const void *tag
,
48 evWaitFunc func
, void *uap
, evWaitID
*id
)
50 evContext_p
*ctx
= opaqueCtx
.opaque
;
52 evWaitList
*wl
= evGetWaitList(ctx
, tag
, 1);
72 * Mark runnable all waiting functions having a certain tag.
75 evDo(evContext opaqueCtx
, const void *tag
) {
76 evContext_p
*ctx
= opaqueCtx
.opaque
;
77 evWaitList
*wl
= evGetWaitList(ctx
, tag
, 0);
86 INSIST(first
!= NULL
);
88 if (ctx
->waitDone
.last
!= NULL
)
89 ctx
->waitDone
.last
->next
= first
;
91 ctx
->waitDone
.first
= first
;
92 ctx
->waitDone
.last
= wl
->last
;
93 evFreeWaitList(ctx
, wl
);
99 * Remove a waiting (or ready to run) function from the queue.
102 evUnwait(evContext opaqueCtx
, evWaitID id
) {
103 evContext_p
*ctx
= opaqueCtx
.opaque
;
109 INSIST(this != NULL
);
110 wl
= evGetWaitList(ctx
, this->tag
, 0);
112 for (prev
= NULL
, this = wl
->first
;
114 prev
= this, this = this->next
)
115 if (this == (evWait
*)id
.opaque
) {
118 prev
->next
= this->next
;
120 wl
->first
= this->next
;
121 if (wl
->last
== this)
123 if (wl
->first
== NULL
)
124 evFreeWaitList(ctx
, wl
);
130 /* Maybe it's done */
131 for (prev
= NULL
, this = ctx
->waitDone
.first
;
133 prev
= this, this = this->next
)
134 if (this == (evWait
*)id
.opaque
) {
137 prev
->next
= this->next
;
139 ctx
->waitDone
.first
= this->next
;
140 if (ctx
->waitDone
.last
== this)
141 ctx
->waitDone
.last
= prev
;
160 evDefer(evContext opaqueCtx
, evWaitFunc func
, void *uap
) {
161 evContext_p
*ctx
= opaqueCtx
.opaque
;
169 if (ctx
->waitDone
.last
!= NULL
)
170 ctx
->waitDone
.last
->next
= new;
172 ctx
->waitDone
.first
= new;
173 ctx
->waitDone
.last
= new;
182 print_waits(evContext_p
*ctx
) {
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");
201 evNewWaitList(evContext_p
*ctx
) {
207 new->first
= new->last
= NULL
;
209 new->next
= ctx
->waitLists
;
210 if (new->next
!= NULL
)
211 new->next
->prev
= new;
212 ctx
->waitLists
= new;
217 evFreeWaitList(evContext_p
*ctx
, evWaitList
*this) {
219 INSIST(this != NULL
);
221 if (this->prev
!= NULL
)
222 this->prev
->next
= this->next
;
224 ctx
->waitLists
= this->next
;
225 if (this->next
!= NULL
)
226 this->next
->prev
= this->prev
;
231 evGetWaitList(evContext_p
*ctx
, const void *tag
, int should_create
) {
234 for (this = ctx
->waitLists
; this != NULL
; this = this->next
) {
235 if (this->first
!= NULL
&& this->first
->tag
== tag
)
238 if (this == NULL
&& should_create
)
239 this = evNewWaitList(ctx
);