1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/* $OpenBSD: waitq.c,v 1.6 2018/05/31 21:06:12 gilles Exp $ */
/*
* Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/uio.h>
#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "smtpd.h"
struct waiter {
TAILQ_ENTRY(waiter) entry;
void (*cb)(void *, void *, void *);
void *arg;
};
struct waitq {
SPLAY_ENTRY(waitq) entry;
void *tag;
TAILQ_HEAD(, waiter) waiters;
};
static int waitq_cmp(struct waitq *, struct waitq *);
SPLAY_HEAD(waitqtree, waitq);
SPLAY_PROTOTYPE(waitqtree, waitq, entry, waitq_cmp);
static struct waitqtree waitqs = SPLAY_INITIALIZER(&waitqs);
static int
waitq_cmp(struct waitq *a, struct waitq *b)
{
if (a->tag < b->tag)
return (-1);
if (a->tag > b->tag)
return (1);
return (0);
}
SPLAY_GENERATE(waitqtree, waitq, entry, waitq_cmp);
int
waitq_wait(void *tag, void (*cb)(void *, void *, void *), void *arg)
{
struct waitq *wq, key;
struct waiter *w;
key.tag = tag;
wq = SPLAY_FIND(waitqtree, &waitqs, &key);
if (wq == NULL) {
wq = xmalloc(sizeof *wq);
wq->tag = tag;
TAILQ_INIT(&wq->waiters);
SPLAY_INSERT(waitqtree, &waitqs, wq);
}
w = xmalloc(sizeof *w);
w->cb = cb;
w->arg = arg;
TAILQ_INSERT_TAIL(&wq->waiters, w, entry);
return (w == TAILQ_FIRST(&wq->waiters));
}
void
waitq_run(void *tag, void *result)
{
struct waitq *wq, key;
struct waiter *w;
key.tag = tag;
wq = SPLAY_FIND(waitqtree, &waitqs, &key);
SPLAY_REMOVE(waitqtree, &waitqs, wq);
while ((w = TAILQ_FIRST(&wq->waiters))) {
TAILQ_REMOVE(&wq->waiters, w, entry);
w->cb(tag, w->arg, result);
free(w);
}
free(wq);
}
|