xref: /illumos-gate/usr/src/lib/libresolv2/common/isc/ev_waits.c (revision b1e2e3fb17324e9ddf43db264a0c64da7756d9e6)
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1996-1999 by Internet Software Consortium
4  *
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.
8  *
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.
16  */
17 
18 /* ev_waits.c - implement deferred function calls for the eventlib
19  * vix 05dec95 [initial]
20  */
21 
22 #include "port_before.h"
23 #include "fd_setsize.h"
24 
25 #include <errno.h>
26 
27 #include <isc/eventlib.h>
28 #include <isc/assertions.h>
29 #include "eventlib_p.h"
30 
31 #include "port_after.h"
32 
33 /* Forward. */
34 
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);
39 
40 
41 /* Public. */
42 
43 /*%
44  * Enter a new wait function on the queue.
45  */
46 int
47 evWaitFor(evContext opaqueCtx, const void *tag,
48 	  evWaitFunc func, void *uap, evWaitID *id)
49 {
50 	evContext_p *ctx = opaqueCtx.opaque;
51 	evWait *new;
52 	evWaitList *wl = evGetWaitList(ctx, tag, 1);
53 
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);
69 }
70 
71 /*%
72  * Mark runnable all waiting functions having a certain tag.
73  */
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;
79 
80 	if (!wl) {
81 		errno = ENOENT;
82 		return (-1);
83 	}
84 
85 	first = wl->first;
86 	INSIST(first != NULL);
87 
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);
94 
95 	return (0);
96 }
97 
98 /*%
99  * Remove a waiting (or ready to run) function from the queue.
100  */
101 int
102 evUnwait(evContext opaqueCtx, evWaitID id) {
103 	evContext_p *ctx = opaqueCtx.opaque;
104 	evWait *this, *prev;
105 	evWaitList *wl;
106 	int found = 0;
107 
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;
126 			}
127 	}
128 
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;
143 			}
144 	}
145 
146 	if (!found) {
147 		errno = ENOENT;
148 		return (-1);
149 	}
150 
151 	FREE(this);
152 
153 	if (ctx->debug >= 9)
154 		print_waits(ctx);
155 
156 	return (0);
157 }
158 
159 int
160 evDefer(evContext opaqueCtx, evWaitFunc func, void *uap) {
161 	evContext_p *ctx = opaqueCtx.opaque;
162 	evWait *new;
163 
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);
177 }
178 
179 /* Private. */
180 
181 static void
182 print_waits(evContext_p *ctx) {
183 	evWaitList *wl;
184 	evWait *this;
185 
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");
193 	}
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");
198 }
199 
200 static evWaitList *
201 evNewWaitList(evContext_p *ctx) {
202 	evWaitList *new;
203 
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);
214 }
215 
216 static void
217 evFreeWaitList(evContext_p *ctx, evWaitList *this) {
218 
219 	INSIST(this != NULL);
220 
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);
228 }
229 
230 static evWaitList *
231 evGetWaitList(evContext_p *ctx, const void *tag, int should_create) {
232 	evWaitList *this;
233 
234 	for (this = ctx->waitLists; this != NULL; this = this->next) {
235 		if (this->first != NULL && this->first->tag == tag)
236 			break;
237 	}
238 	if (this == NULL && should_create)
239 		this = evNewWaitList(ctx);
240 	return (this);
241 }
242 
243 /*! \file */
244