varnish-cache/bin/varnishd/cache/cache_lck.c
1
/*-
2
 * Copyright (c) 2008-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * The geniuses who came up with pthreads did not think operations like
29
 * pthread_assert_mutex_held() were important enough to include them in
30
 * the API.
31
 *
32
 * Build our own locks on top of pthread mutexes and hope that the next
33
 * civilization is better at such crucial details than this one.
34
 */
35
36
#include "config.h"
37
38
#include "cache_varnishd.h"
39
40
#include <errno.h>
41
#include <stdlib.h>
42
#include <stdio.h>
43
44
#include "VSC_lck.h"
45
46
struct ilck {
47
        unsigned                magic;
48
#define ILCK_MAGIC              0x7b86c8a5
49
        int                     held;
50
        pthread_mutex_t         mtx;
51
        pthread_t               owner;
52
        const char              *w;
53
        struct VSC_lck          *stat;
54
};
55
56
static pthread_mutexattr_t attr;
57
58
/*--------------------------------------------------------------------*/
59
60
static void
61 125
Lck_Witness_Lock(const struct ilck *il, const char *p, int l, const char *try)
62
{
63
        char *q, t[10];
64
        int emit;
65
66 125
        AN(p);
67 125
        q = pthread_getspecific(witness_key);
68 125
        if (q == NULL) {
69 18
                q = calloc(1, 1024);
70 18
                AN(q);
71 18
                AZ(pthread_setspecific(witness_key, q));
72
        }
73 125
        emit = *q != '\0';
74 125
        strcat(q, " ");
75 125
        strcat(q, il->w);
76 125
        strcat(q, try);
77 125
        strcat(q, ",");
78 125
        strcat(q, p);
79 125
        strcat(q, ",");
80 125
        bprintf(t, "%d", l);
81 125
        strcat(q, t);
82 125
        if (emit)
83 8
                VSL(SLT_Witness, 0, "%s", q);
84 125
}
85
86
static void
87 127
Lck_Witness_Unlock(const struct ilck *il)
88
{
89
        char *q, *r;
90
91 127
        q = pthread_getspecific(witness_key);
92 127
        if (q == NULL)
93 139
                return;
94 115
        r = strrchr(q, ' ');
95 115
        if (r == NULL)
96 0
                r = q;
97
        else
98 115
                *r++ = '\0';
99 115
        if (memcmp(r, il->w, strlen(il->w)))
100 0
                VSL(SLT_Witness, 0, "Unlock %s @ %s <%s>", il->w, r, q);
101
        else
102 115
                *r = '\0';
103
}
104
105
/*--------------------------------------------------------------------*/
106
107
void v_matchproto_()
108 209943
Lck__Lock(struct lock *lck, const char *p, int l)
109
{
110
        struct ilck *ilck;
111
112 209943
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
113 209943
        if (DO_DEBUG(DBG_WITNESS))
114 124
                Lck_Witness_Lock(ilck, p, l, "");
115 209924
        AZ(pthread_mutex_lock(&ilck->mtx));
116 210026
        AZ(ilck->held);
117 210026
        ilck->stat->locks++;
118 210026
        ilck->owner = pthread_self();
119 210023
        ilck->held = 1;
120 210023
}
121
122
void v_matchproto_()
123 230932
Lck__Unlock(struct lock *lck, const char *p, int l)
124
{
125
        struct ilck *ilck;
126
127
        (void)p;
128
        (void)l;
129
130 230932
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
131 230932
        assert(pthread_equal(ilck->owner, pthread_self()));
132 231075
        AN(ilck->held);
133 231075
        ilck->held = 0;
134
        /*
135
         * #ifdef POSIX_STUPIDITY:
136
         * The pthread_t type has no defined assignment or comparison
137
         * operators, this is why pthread_equal() is necessary.
138
         * Unfortunately POSIX forgot to define a NULL value for pthread_t
139
         * so you can never unset a pthread_t variable.
140
         * We hack it and fill it with zero bits, hoping for sane
141
         * implementations of pthread.
142
         * #endif
143
         */
144 231075
        memset(&ilck->owner, 0, sizeof ilck->owner);
145 231075
        AZ(pthread_mutex_unlock(&ilck->mtx));
146 231023
        if (DO_DEBUG(DBG_WITNESS))
147 127
                Lck_Witness_Unlock(ilck);
148 231014
}
149
150
int v_matchproto_()
151 34638
Lck__Trylock(struct lock *lck, const char *p, int l)
152
{
153
        struct ilck *ilck;
154
        int r;
155
156 34638
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
157 34638
        if (DO_DEBUG(DBG_WITNESS))
158 1
                Lck_Witness_Lock(ilck, p, l, "?");
159 34602
        r = pthread_mutex_trylock(&ilck->mtx);
160 34623
        assert(r == 0 || r == EBUSY);
161 34623
        if (r == 0) {
162 34623
                AZ(ilck->held);
163 34623
                ilck->held = 1;
164 34623
                ilck->stat->locks++;
165 34623
                ilck->owner = pthread_self();
166
        }
167 34645
        return (r);
168
}
169
170
int
171 20444
Lck__Held(const struct lock *lck)
172
{
173
        struct ilck *ilck;
174
175 20444
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
176 20444
        return (ilck->held);
177
}
178
179
int
180 20442
Lck__Owned(const struct lock *lck)
181
{
182
        struct ilck *ilck;
183
184 20442
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
185 20442
        AN(ilck->held);
186 20442
        return (pthread_equal(ilck->owner, pthread_self()));
187
}
188
189
int v_matchproto_()
190 58623
Lck_CondWait(pthread_cond_t *cond, struct lock *lck, double when)
191
{
192
        struct ilck *ilck;
193
        struct timespec ts;
194
        double t;
195
196 58623
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
197 58623
        AN(ilck->held);
198 58623
        assert(pthread_equal(ilck->owner, pthread_self()));
199 58612
        ilck->held = 0;
200 58612
        if (when == 0) {
201 47356
                errno = pthread_cond_wait(cond, &ilck->mtx);
202 36968
                AZ(errno);
203
        } else {
204 11256
                assert(when > 1e9);
205 11256
                ts.tv_nsec = (long)(modf(when, &t) * 1e9);
206 11256
                ts.tv_sec = (long)t;
207 11256
                errno = pthread_cond_timedwait(cond, &ilck->mtx, &ts);
208 8090
                assert(errno == 0 ||
209
                    errno == ETIMEDOUT ||
210
                    errno == EINTR);
211
        }
212 45064
        AZ(ilck->held);
213 45064
        ilck->held = 1;
214 45064
        ilck->owner = pthread_self();
215 45061
        return (errno);
216
}
217
218
void
219 22008
Lck__New(struct lock *lck, struct VSC_lck *st, const char *w)
220
{
221
        struct ilck *ilck;
222
223 22008
        AN(st);
224 22008
        AN(w);
225 22008
        AZ(lck->priv);
226 22008
        ALLOC_OBJ(ilck, ILCK_MAGIC);
227 22008
        AN(ilck);
228 22008
        ilck->w = w;
229 22008
        ilck->stat = st;
230 22008
        ilck->stat->creat++;
231 22008
        AZ(pthread_mutex_init(&ilck->mtx, &attr));
232 22011
        lck->priv = ilck;
233 22011
}
234
235
void
236 2740
Lck_Delete(struct lock *lck)
237
{
238
        struct ilck *ilck;
239
240 2740
        CAST_OBJ_NOTNULL(ilck, lck->priv, ILCK_MAGIC);
241 2740
        ilck->stat->destroy++;
242 2740
        lck->priv = NULL;
243 2740
        AZ(pthread_mutex_destroy(&ilck->mtx));
244 2740
        FREE_OBJ(ilck);
245 2740
}
246
247
struct VSC_lck *
248 12313
Lck_CreateClass(const char *name)
249
{
250 12313
        return(VSC_lck_New(name));
251
}
252
253
void
254 0
Lck_DestroyClass(struct VSC_lck **vsclck)
255
{
256 0
        VSC_lck_Destroy(vsclck);
257 0
}
258
259
#define LOCK(nam) struct VSC_lck *lck_##nam;
260
#include "tbl/locks.h"
261
262
void
263 614
LCK_Init(void)
264
{
265
266 614
        AZ(pthread_mutexattr_init(&attr));
267 614
        AZ(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK));
268
#define LOCK(nam)       lck_##nam = Lck_CreateClass(#nam);
269
#include "tbl/locks.h"
270 614
}