4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1996-1999 by Internet Software Consortium
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* ev_waits.c - implement deferred function calls for the eventlib
21 * vix 05dec95 [initial]
24 #if !defined(LINT) && !defined(CODECENTER)
25 static const char rcsid
[] = "Id: ev_waits.c,v 1.4 2005/04/27 04:56:36 sra Exp";
28 #include "port_before.h"
29 #include "fd_setsize.h"
33 #include <isc/eventlib.h>
34 #include <isc/assertions.h>
35 #include "eventlib_p.h"
37 #include "port_after.h"
41 static void print_waits(evContext_p
*ctx
);
42 static evWaitList
* evNewWaitList(evContext_p
*);
43 static void evFreeWaitList(evContext_p
*, evWaitList
*);
44 static evWaitList
* evGetWaitList(evContext_p
*, const void *, int);
50 * Enter a new wait function on the queue.
53 evWaitFor(evContext opaqueCtx
, const void *tag
,
54 evWaitFunc func
, void *uap
, evWaitID
*id
)
56 evContext_p
*ctx
= opaqueCtx
.opaque
;
58 evWaitList
*wl
= evGetWaitList(ctx
, tag
, 1);
78 * Mark runnable all waiting functions having a certain tag.
81 evDo(evContext opaqueCtx
, const void *tag
) {
82 evContext_p
*ctx
= opaqueCtx
.opaque
;
83 evWaitList
*wl
= evGetWaitList(ctx
, tag
, 0);
92 INSIST(first
!= NULL
);
94 if (ctx
->waitDone
.last
!= NULL
)
95 ctx
->waitDone
.last
->next
= first
;
97 ctx
->waitDone
.first
= first
;
98 ctx
->waitDone
.last
= wl
->last
;
99 evFreeWaitList(ctx
, wl
);
105 * Remove a waiting (or ready to run) function from the queue.
108 evUnwait(evContext opaqueCtx
, evWaitID id
) {
109 evContext_p
*ctx
= opaqueCtx
.opaque
;
115 INSIST(this != NULL
);
116 wl
= evGetWaitList(ctx
, this->tag
, 0);
118 for (prev
= NULL
, this = wl
->first
;
120 prev
= this, this = this->next
)
121 if (this == (evWait
*)id
.opaque
) {
124 prev
->next
= this->next
;
126 wl
->first
= this->next
;
127 if (wl
->last
== this)
129 if (wl
->first
== NULL
)
130 evFreeWaitList(ctx
, wl
);
136 /* Maybe it's done */
137 for (prev
= NULL
, this = ctx
->waitDone
.first
;
139 prev
= this, this = this->next
)
140 if (this == (evWait
*)id
.opaque
) {
143 prev
->next
= this->next
;
145 ctx
->waitDone
.first
= this->next
;
146 if (ctx
->waitDone
.last
== this)
147 ctx
->waitDone
.last
= prev
;
166 evDefer(evContext opaqueCtx
, evWaitFunc func
, void *uap
) {
167 evContext_p
*ctx
= opaqueCtx
.opaque
;
175 if (ctx
->waitDone
.last
!= NULL
)
176 ctx
->waitDone
.last
->next
= new;
178 ctx
->waitDone
.first
= new;
179 ctx
->waitDone
.last
= new;
188 print_waits(evContext_p
*ctx
) {
192 evPrintf(ctx
, 9, "wait waiting:\n");
193 for (wl
= ctx
->waitLists
; wl
!= NULL
; wl
= wl
->next
) {
194 INSIST(wl
->first
!= NULL
);
195 evPrintf(ctx
, 9, " tag %p:", wl
->first
->tag
);
196 for (this = wl
->first
; this != NULL
; this = this->next
)
197 evPrintf(ctx
, 9, " %p", this);
198 evPrintf(ctx
, 9, "\n");
200 evPrintf(ctx
, 9, "wait done:");
201 for (this = ctx
->waitDone
.first
; this != NULL
; this = this->next
)
202 evPrintf(ctx
, 9, " %p", this);
203 evPrintf(ctx
, 9, "\n");
207 evNewWaitList(evContext_p
*ctx
) {
213 new->first
= new->last
= NULL
;
215 new->next
= ctx
->waitLists
;
216 if (new->next
!= NULL
)
217 new->next
->prev
= new;
218 ctx
->waitLists
= new;
223 evFreeWaitList(evContext_p
*ctx
, evWaitList
*this) {
225 INSIST(this != NULL
);
227 if (this->prev
!= NULL
)
228 this->prev
->next
= this->next
;
230 ctx
->waitLists
= this->next
;
231 if (this->next
!= NULL
)
232 this->next
->prev
= this->prev
;
237 evGetWaitList(evContext_p
*ctx
, const void *tag
, int should_create
) {
240 for (this = ctx
->waitLists
; this != NULL
; this = this->next
) {
241 if (this->first
!= NULL
&& this->first
->tag
== tag
)
244 if (this == NULL
&& should_create
)
245 this = evNewWaitList(ctx
);