Code Coverage: vinyl-cache/bin/vinyld/cache/cache_lck.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2011 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * The geniuses who came up with pthreads did not think operations like
30
 * pthread_assert_mutex_held() were important enough to include them in
31
 * the API.
32
 *
33
 * Build our own locks on top of pthread mutexes and hope that the next
34
 * civilization is better at such crucial details than this one.
35
 */
36
37
#include "config.h"
38
39
#include "cache_int.h"
40
41
#include <stdlib.h>
42
#include <stdio.h>
43
44
#include "vtim.h"
45
46
#include "VSC_lck.h"
47
48
struct ilck {
49
        unsigned                magic;
50
#define ILCK_MAGIC              0x7b86c8a5
51
        int                     held;
52
        pthread_mutex_t         mtx;
53
        pthread_t               owner;
54
#ifdef ENABLE_WITNESS
55
        const char              *w;
56
#endif
57
        struct VSC_lck          *stat;
58
};
59
60
/*--------------------------------------------------------------------*/
61
62
#ifdef ENABLE_WITNESS
63
static void
64
Lck_Witness_Lock(const struct ilck *il, const char *p, int l,
65
    const char *attempt)
66 434
{
67 434
        char *q, t[10]; //lint !e429
68 434
        int emit;
69
70 434
        AN(p);
71 434
        q = pthread_getspecific(witness_key);
72 434
        if (q == NULL) {
73 58
                q = calloc(1, 1024);
74 58
                AN(q);
75 58
                PTOK(pthread_setspecific(witness_key, q));
76 58
        }
77 434
        emit = *q != '\0';
78 434
        strcat(q, " ");
79 434
        strcat(q, il->w);
80 434
        strcat(q, attempt);
81 434
        strcat(q, ",");
82 434
        strcat(q, p);
83 434
        strcat(q, ",");
84 434
        bprintf(t, "%d", l);
85 434
        strcat(q, t);
86 434
        if (emit)
87 28
                VSL(SLT_Witness, NO_VXID, "%s", q);
88 434
}
89
90
static void
91
Lck_Witness_Unlock(const struct ilck *il)
92 477
{
93 477
        char *q, *r;
94
95 477
        q = pthread_getspecific(witness_key);
96 477
        if (q == NULL)
97 44
                return;
98 433
        r = strrchr(q, ' ');
99 433
        if (r == NULL)
100 0
                r = q;
101 433
        else
102 433
                *r++ = '\0';
103 433
        if (vmemcmp(r, il->w, vstrlen(il->w)))
104 0
                VSL(SLT_Witness, NO_VXID, "Unlock %s @ %s <%s>", il->w, r, q);
105 433
        else
106 433
                *r = '\0';
107 433
}
108
#endif
109
110
/*--------------------------------------------------------------------*/
111
112
void v_matchproto_()
113
Lck__Lock(struct lock *lck, const char *p, int l)
114 547350
{
115 547350
        struct ilck *ilck;
116 547350
        int r = EINVAL;
117
118 547350
        AN(lck);
119 547350
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
120 547350
#ifdef ENABLE_WITNESS
121 547350
        if (DO_DEBUG(DBG_WITNESS))
122 429
                Lck_Witness_Lock(ilck, p, l, "");
123
#else
124
        (void)p;
125
        (void)l;
126
#endif
127 547350
        if (DO_DEBUG(DBG_LCK)) {
128 0
                r = pthread_mutex_trylock(&ilck->mtx);
129 0
                assert(r == 0 || r == EBUSY);
130 0
        }
131 547350
        if (r)
132 547350
                PTOK(pthread_mutex_lock(&ilck->mtx));
133 547519
        AZ(ilck->held);
134 547519
        if (r == EBUSY)
135 0
                ilck->stat->dbg_busy++;
136 547519
        ilck->stat->locks++;
137 547519
        ilck->owner = pthread_self();
138 547519
        ilck->held = 1;
139 547519
}
140
141
void v_matchproto_()
142
Lck__Unlock(struct lock *lck, const char *p, int l)
143 600191
{
144 600191
        struct ilck *ilck;
145
146 600191
        (void)p;
147 600191
        (void)l;
148
149 600191
        AN(lck);
150 600191
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
151 600191
        assert(pthread_equal(ilck->owner, pthread_self()));
152 600191
        AN(ilck->held);
153 600191
        ilck->held = 0;
154
        /*
155
         * #ifdef POSIX_STUPIDITY:
156
         * The pthread_t type has no defined assignment or comparison
157
         * operators, this is why pthread_equal() is necessary.
158
         * Unfortunately POSIX forgot to define a NULL value for pthread_t
159
         * so you can never unset a pthread_t variable.
160
         * We hack it and fill it with zero bits, hoping for sane
161
         * implementations of pthread.
162
         * #endif
163
         */
164
#ifdef PTHREAD_NULL
165
        ilck->owner = PTHREAD_NULL;
166
#else
167 600191
        memset(&ilck->owner, 0, sizeof ilck->owner);
168 600191
#endif
169 600191
        PTOK(pthread_mutex_unlock(&ilck->mtx));
170 601188
#ifdef ENABLE_WITNESS
171 601188
        if (DO_DEBUG(DBG_WITNESS))
172 477
                Lck_Witness_Unlock(ilck);
173 601188
#endif
174 601188
}
175
176
int v_matchproto_()
177
Lck__Trylock(struct lock *lck, const char *p, int l)
178 55875
{
179 55875
        struct ilck *ilck;
180 55875
        int r;
181
182 55875
        AN(lck);
183 55875
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
184 55875
#ifdef ENABLE_WITNESS
185 55875
        if (DO_DEBUG(DBG_WITNESS))
186 5
                Lck_Witness_Lock(ilck, p, l, "?");
187
#else
188
        (void)p;
189
        (void)l;
190
#endif
191 55875
        r = pthread_mutex_trylock(&ilck->mtx);
192 55875
        assert(r == 0 || r == EBUSY);
193 55876
        if (r == 0) {
194 55876
                AZ(ilck->held);
195 55876
                ilck->held = 1;
196 55876
                ilck->stat->locks++;
197 55876
                ilck->owner = pthread_self();
198 18446744073709551615
        } else if (DO_DEBUG(DBG_LCK))
199 0
                ilck->stat->dbg_try_fail++;
200 55875
        return (r);
201 55875
}
202
203
int
204
Lck__Held(const struct lock *lck)
205 96329
{
206 96329
        struct ilck *ilck;
207
208 96329
        AN(lck);
209 96329
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
210 96329
        return (ilck->held);
211 96329
}
212
213
int
214
Lck__Owned(const struct lock *lck)
215 96331
{
216 96331
        struct ilck *ilck;
217
218 96331
        AN(lck);
219 96331
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
220 96331
        AN(ilck->held);
221 96331
        return (pthread_equal(ilck->owner, pthread_self()));
222 96331
}
223
224
int v_matchproto_()
225
Lck_CondWait(pthread_cond_t *cond, struct lock *lck)
226 31937
{
227 31937
       return (Lck_CondWaitUntil(cond, lck, INFINITY));
228 31937
}
229
230
int v_matchproto_()
231
Lck_CondWaitTimeout(pthread_cond_t *cond, struct lock *lck, vtim_dur timeout)
232 10166
{
233
234 10166
        if (isinf(timeout))
235 0
                return (Lck_CondWaitUntil(cond, lck, INFINITY));
236
237 10166
        assert(timeout >= 0);
238 10166
        assert(timeout <= 3600);
239 10166
        timeout = vmax(timeout, 1e-3);
240 10166
        return (Lck_CondWaitUntil(cond, lck, VTIM_real() + timeout));
241 10166
}
242
243
int v_matchproto_()
244
Lck_CondWaitUntil(pthread_cond_t *cond, struct lock *lck, vtim_real when)
245 77754
{
246 77754
        struct ilck *ilck;
247 77754
        struct timespec ts;
248
249 77754
        AN(lck);
250 77754
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
251 77754
        AN(ilck->held);
252 77754
        assert(pthread_equal(ilck->owner, pthread_self()));
253 77754
        ilck->held = 0;
254 77754
        if (isinf(when)) {
255 59325
                errno = pthread_cond_wait(cond, &ilck->mtx);
256 59325
                AZ(errno);
257 59325
        } else {
258 18429
                assert(when > 1e9);
259 18429
                ts = VTIM_timespec(when);
260 18429
                assert(ts.tv_nsec >= 0 && ts.tv_nsec <= 999999999);
261 18429
                errno = pthread_cond_timedwait(cond, &ilck->mtx, &ts);
262
#if defined (__APPLE__)
263
                /*
264
                 * I hate woo-doo programming in all it's forms and all it's
265
                 * manifestations, but for reasons I utterly fail to isolate,
266
                 * OSX sometimes throws an EINVAL.
267
                 *
268
                 * I have tried very hard to determine if any of the three
269
                 * arguments are in fact invalid, and found nothing which
270
                 * even hints that it might be the case.
271
                 *
272
                 * So far I have yet to see a failure if the exact same
273
                 * call is repeated after a very short sleep.
274
                 *
275
                 * Calling pthread_yield_np() instead of sleeping /mostly/
276
                 * works as well, but still fails sometimes.
277
                 *
278
                 * Env:
279
                 *      Darwin Kernel Version 20.5.0:
280
                 *      Sat May  8 05:10:31 PDT 2021;
281
                 *      root:xnu-7195.121.3~9/RELEASE_ARM64_T8101 arm64
282
                 *
283
                 * 20220329 /phk
284
                 */
285
                if (errno == EINVAL) {
286
                        VTIM_sleep(0.0001);
287
                        errno = pthread_cond_timedwait(cond, &ilck->mtx, &ts);
288
                }
289
#endif
290
                /* We should never observe EINTR, but we have in the past. For
291
                 * example when sanitizers are enabled.
292
                 */
293 18429
                assert(errno == 0 ||
294 18429
                    errno == ETIMEDOUT ||
295 18429
                    errno == EINTR);
296 18429
        }
297 77754
        AZ(ilck->held);
298 77754
        ilck->held = 1;
299 77754
        ilck->owner = pthread_self();
300 77754
        return (errno);
301 77754
}
302
303
void
304
Lck__New(struct lock *lck, struct VSC_lck *st, const char *w)
305 174238
{
306 174238
        struct ilck *ilck;
307
308 174238
        AN(st);
309 174238
        AN(w);
310 174238
        AN(lck);
311 174238
        AZ(lck->priv);
312 174238
        ALLOC_OBJ(ilck, ILCK_MAGIC);
313 174238
        AN(ilck);
314 174238
#ifdef ENABLE_WITNESS
315 174238
        ilck->w = w;
316
#else
317
        (void)w;
318
#endif
319 174238
        ilck->stat = st;
320 174238
        ilck->stat->creat++;
321 174238
        PTOK(pthread_mutex_init(&ilck->mtx, &mtxattr_errorcheck));
322 174239
        lck->priv = ilck;
323 174239
}
324
325
void
326
Lck_Delete(struct lock *lck)
327 14515
{
328 14515
        struct ilck *ilck;
329
330 14515
        AN(lck);
331 14515
        TAKE_OBJ_NOTNULL(ilck, &lck->priv, ILCK_MAGIC);
332 14515
        ilck->stat->destroy++;
333 14515
        PTOK(pthread_mutex_destroy(&ilck->mtx));
334 14515
        FREE_OBJ(ilck);
335 14515
}
336
337
struct VSC_lck *
338
Lck_CreateClass(struct vsc_seg **sg, const char *name)
339 24656
{
340 24656
        return (VSC_lck_New(NULL, sg, name));
341 24656
}
342
343
void
344
Lck_DestroyClass(struct vsc_seg **sg)
345 0
{
346 0
        VSC_lck_Destroy(sg);
347 0
}
348
349
#define LOCK(nam) struct VSC_lck *lck_##nam;
350
#include "tbl/locks.h"
351
352
void
353
LCK_Init(void)
354 1026
{
355
356 23598
#define LOCK(nam)       lck_##nam = Lck_CreateClass(NULL, #nam);
357 1026
#include "tbl/locks.h"
358 1026
}