Code Coverage: vinyl-cache/bin/vinyld/waiter/cache_waiter.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 */
31
32
#include "config.h"
33
34
#include "cache/cache_int.h"
35
36
#include <stdlib.h>
37
38
#include "vbh.h"
39
#include "vtim.h"
40
41
#include "waiter/waiter.h"
42
#include "waiter/waiter_priv.h"
43
#include "waiter/mgt_waiter.h"
44
45
#include "VSC_waiter.h"
46
47
static int v_matchproto_(vbh_cmp_t)
48
waited_cmp(void *priv, const void *a, const void *b)
49 790
{
50 790
        const struct waiter *ww;
51 790
        const struct waited *aa, *bb;
52
53 790
        CAST_OBJ_NOTNULL(ww, priv, WAITER_MAGIC);
54 790
        CAST_OBJ_NOTNULL(aa, a, WAITED_MAGIC);
55 790
        CAST_OBJ_NOTNULL(bb, b, WAITED_MAGIC);
56
57 790
        return (Wait_When(aa) < Wait_When(bb));
58 790
}
59
60
static void v_matchproto_(vbh_update_t)
61
waited_update(void *priv, void *p, unsigned u)
62 6030
{
63 6030
        struct waited *pp;
64
65 6030
        (void)priv;
66 6030
        CAST_OBJ_NOTNULL(pp, p, WAITED_MAGIC);
67 6030
        pp->idx = u;
68 6030
}
69
70
/**********************************************************************/
71
72
void
73
Wait_Call(const struct waiter *w, struct waited *wp,
74
    enum wait_event ev, double now)
75 2706
{
76 2706
        CHECK_OBJ_NOTNULL(w, WAITER_MAGIC);
77 2706
        CHECK_OBJ_NOTNULL(wp, WAITED_MAGIC);
78 2706
        AN(wp->func);
79 2706
        assert(wp->idx == VBH_NOIDX);
80 2706
        AN(w->vsc);
81
82 2706
        switch (ev) {
83 1021
        case WAITER_REMCLOSE:
84 1021
                w->vsc->remclose++;
85 1021
                break;
86 50
        case WAITER_TIMEOUT:
87 50
                w->vsc->timeout++;
88 50
                break;
89 1635
        case WAITER_ACTION:
90 1635
                w->vsc->action++;
91 1635
                break;
92 0
        default:
93 0
                break;
94 2706
        }
95
96 2706
        wp->func(wp, ev, now);
97 2706
}
98
99
/**********************************************************************/
100
101
void
102
Wait_HeapInsert(const struct waiter *w, struct waited *wp)
103 2707
{
104 2707
        CHECK_OBJ_NOTNULL(w, WAITER_MAGIC);
105 2707
        CHECK_OBJ_NOTNULL(wp, WAITED_MAGIC);
106 2707
        assert(wp->idx == VBH_NOIDX);
107 2707
        AN(w->vsc);
108 2707
        w->vsc->conns++;
109 2707
        VBH_insert(w->heap, wp);
110 2707
}
111
112
/*
113
 * XXX: wp is const because otherwise FlexeLint complains.  However, *wp
114
 * XXX: will actually change as a result of calling this function, via
115
 * XXX: the pointer stored in the bin-heap.  I can see how this const
116
 * XXX: could maybe confuse a compilers optimizer, but I do not expect
117
 * XXX: any harm to come from it.  Caveat Emptor.
118
 */
119
120
int
121
Wait_HeapDelete(const struct waiter *w, const struct waited *wp)
122 2705
{
123 2705
        CHECK_OBJ_NOTNULL(w, WAITER_MAGIC);
124 2705
        CHECK_OBJ_NOTNULL(wp, WAITED_MAGIC);
125 2705
        if (wp->idx == VBH_NOIDX)
126 0
                return (0);
127 2705
        AN(w->vsc);
128 2705
        w->vsc->conns--;
129 2705
        VBH_delete(w->heap, wp->idx);
130 2705
        return (1);
131 2705
}
132
133
double
134
Wait_HeapDue(const struct waiter *w, struct waited **wpp)
135 6137
{
136 6137
        struct waited *wp;
137
138 6137
        wp = VBH_root(w->heap);
139 6137
        CHECK_OBJ_ORNULL(wp, WAITED_MAGIC);
140 6137
        if (wp == NULL) {
141 4269
                if (wpp != NULL)
142 4269
                        *wpp = NULL;
143 4269
                return (0);
144 4269
        }
145 1868
        if (wpp != NULL)
146 1822
                *wpp = wp;
147 1868
        return (Wait_When(wp));
148 6137
}
149
150
/**********************************************************************/
151
152
int
153
Wait_Enter(const struct waiter *w, struct waited *wp)
154 2707
{
155
156 2707
        CHECK_OBJ_NOTNULL(w, WAITER_MAGIC);
157 2707
        CHECK_OBJ_NOTNULL(wp, WAITED_MAGIC);
158 2707
        assert(wp->fd > 0);                     // stdin never comes here
159 2707
        AN(wp->func);
160 2707
        wp->idx = VBH_NOIDX;
161 2707
        return (w->impl->enter(w->priv, wp));
162 2707
}
163
164
/**********************************************************************/
165
166
const char *
167
Waiter_GetName(void)
168 11
{
169
170 11
        if (waiter != NULL)
171 11
                return (waiter->name);
172 0
        else
173 0
                return ("(No Waiter?)");
174 11
}
175
176
struct waiter *
177
Waiter_New(const char *name)
178 2038
{
179 2038
        struct waiter *w;
180
181 2038
        AN(waiter);
182 2038
        AN(waiter->name);
183 2038
        AN(waiter->init);
184 2038
        AN(waiter->enter);
185 2038
        AN(waiter->fini);
186
187 2038
        w = calloc(1, sizeof (struct waiter) + waiter->size);
188 2038
        AN(w);
189 2038
        INIT_OBJ(w, WAITER_MAGIC);
190 2038
        w->priv = (void*)(w + 1);
191 2038
        w->impl = waiter;
192 2038
        VTAILQ_INIT(&w->waithead);
193 2038
        w->heap = VBH_new(w, waited_cmp, waited_update);
194
195 2038
        AZ(w->vsc);
196 2038
        w->vsc = VSC_waiter_New(NULL, NULL, "%s", name);
197 2038
        AN(w->vsc);
198
199 2038
        waiter->init(w);
200
201 2038
        return (w);
202 2038
}
203
204
void
205
Waiter_Destroy(struct waiter **wp)
206 2012
{
207 2012
        struct waiter *w;
208 2012
        struct waited *waited;
209 2012
        vtim_real now;
210
211 2012
        TAKE_OBJ_NOTNULL(w, wp, WAITER_MAGIC);
212
213 2012
        AN(w->impl->fini);
214 2012
        w->impl->fini(w);
215 2012
        now = VTIM_real();
216 2041
        while ((waited = VBH_root(w->heap)) != NULL) {
217 29
                (void) Wait_HeapDelete(w, waited);
218 29
                Wait_Call(w, waited, WAITER_TIMEOUT, now);
219 29
        }
220
221 2012
        AZ(VBH_root(w->heap));
222 2012
        FREE_OBJ(w);
223 2012
}