varnish-cache/bin/varnishd/cache/cache_obj.c
1
/*-
2
 * Copyright (c) 2013-2016 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
 * Lifetime of an objcore:
29
 *      phase 0 - nonexistent
30
 *      phase 1 - created, but no stevedore associated
31
 *      phase 2 - stevedore associated, being filled out
32
 *      phase 3 - stable, no changes happening
33
 *      phase 4 - unavailable, being dismantled
34
 *      phase 5 - stevedore disassociated
35
 *      phase 6 - nonexistent
36
 *
37
 * 0->1 ObjNew()        creates objcore
38
 *
39
 * 1->2 STV_NewObject() associates a stevedore
40
 *
41
 * 2    ObjSetState()   sets state
42
 * 2    ObjWaitState()  waits for particular state
43
 *                      INVALID->REQ_DONE->STREAM->FINISHED->FAILED
44
 *
45
 * 2    ObjGetSpace()   allocates space
46
 * 2    ObjExtend()     commits content
47
 * 2    ObjWaitExtend() waits for content - used to implement ObjIterate())
48
 * 2    ObjTrimStore()  signals end of content addition
49
 *
50
 * 2    ObjSetAttr()
51
 * 2      ObjCopyAttr()
52
 * 2      ObjSetFlag()
53
 * 2      ObjSetDouble()
54
 * 2      ObjSetU32()
55
 * 2      ObjSetU64()
56
 *
57
 * 2->3 ObjBocDone()    Boc removed from OC, clean it up
58
 *
59
 * 23   ObjHasAttr()
60
 * 23   ObjGetAttr()
61
 * 23     ObjCheckFlag()
62
 * 23     ObjGetDouble()
63
 * 23     ObjGetU32()
64
 * 23     ObjGetU64()
65
 * 23     ObjGetLen()
66
 * 23     ObjGetXID()
67
 *
68
 * 23   ObjIterate()    ... over body
69
 *
70
 * 23   ObjTouch()      Signal to LRU(-like) facilities
71
 *
72
 * 3->4 HSH_Snipe()     kill if not in use
73
 * 3->4 HSH_Kill()      make unavailable
74
 *
75
 * 234  ObjSlim()       Release body storage (but retain attribute storage)
76
 *
77
 * 4->5 ObjFreeObj()    disassociates stevedore
78
 *
79
 * 5->6 FREE_OBJ()      ...in HSH_DerefObjCore()
80
 */
81
82
#include "config.h"
83
84
#include <stdlib.h>
85
86
#include "cache_varnishd.h"
87
#include "cache_obj.h"
88
#include "vend.h"
89
#include "storage/storage.h"
90
91
static const struct obj_methods *
92 286023
obj_getmethods(const struct objcore *oc)
93
{
94
95 286023
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
96 286023
        CHECK_OBJ_NOTNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
97 286023
        AN(oc->stobj->stevedore->methods);
98 286023
        return (oc->stobj->stevedore->methods);
99
}
100
101
static struct boc *
102 4354
obj_newboc(void)
103
{
104
        struct boc *boc;
105
106 4354
        ALLOC_OBJ(boc, BOC_MAGIC);
107 4354
        AN(boc);
108 4354
        Lck_New(&boc->mtx, lck_busyobj);
109 4353
        AZ(pthread_cond_init(&boc->cond, NULL));
110 4354
        boc->refcount = 1;
111 4354
        return (boc);
112
}
113
114
static void
115 3986
obj_deleteboc(struct boc **p)
116
{
117
        struct boc *boc;
118
119 3986
        AN(p);
120 3986
        boc = *p;
121 3986
        *p = NULL;
122 3986
        Lck_Delete(&boc->mtx);
123 3986
        AZ(pthread_cond_destroy(&boc->cond));
124 3986
        if (boc->vary != NULL)
125 192
                free(boc->vary);
126 3986
        FREE_OBJ(boc);
127 3986
}
128
129
/*====================================================================
130
 * ObjNew()
131
 *
132
 */
133
134
struct objcore *
135 4354
ObjNew(const struct worker *wrk)
136
{
137
        struct objcore *oc;
138
139 4354
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
140
141 4354
        ALLOC_OBJ(oc, OBJCORE_MAGIC);
142 4354
        AN(oc);
143 4354
        wrk->stats->n_objectcore++;
144 4354
        oc->last_lru = NAN;
145 4354
        oc->flags = OC_F_BUSY;
146
147 4354
        oc->boc = obj_newboc();
148
149 4354
        return (oc);
150
}
151
152
/*====================================================================
153
 * ObjDestroy()
154
 *
155
 */
156
157
void
158 2282
ObjDestroy(const struct worker *wrk, struct objcore **p)
159
{
160
        struct objcore *oc;
161
162 2282
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
163 2282
        TAKE_OBJ_NOTNULL(oc, p, OBJCORE_MAGIC);
164 2282
        if (oc->boc != NULL)
165 58
                obj_deleteboc(&oc->boc);
166 2282
        FREE_OBJ(oc);
167 2282
        wrk->stats->n_objectcore--;
168 2282
}
169
170
/*====================================================================
171
 * ObjIterate()
172
 *
173
 */
174
175
int
176 3364
ObjIterate(struct worker *wrk, struct objcore *oc,
177
    void *priv, objiterate_f *func, int final)
178
{
179 3364
        const struct obj_methods *om = obj_getmethods(oc);
180
181 3364
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
182 3364
        AN(func);
183 3364
        AN(om->objiterator);
184 3364
        return (om->objiterator(wrk, oc, priv, func, final));
185
}
186
187
/*====================================================================
188
 * ObjGetSpace()
189
 *
190
 * This function returns a pointer and length of free space.  If there
191
 * is no free space, some will be added first.
192
 *
193
 * The "sz" argument is an input hint of how much space is desired.
194
 */
195
196
int
197 112560
ObjGetSpace(struct worker *wrk, struct objcore *oc, ssize_t *sz, uint8_t **ptr)
198
{
199 112560
        const struct obj_methods *om = obj_getmethods(oc);
200
201 112560
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
202 112560
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
203 112560
        AN(sz);
204 112560
        AN(ptr);
205 112560
        assert(*sz > 0);
206
207 112560
        AN(om->objgetspace);
208 112560
        return (om->objgetspace(wrk, oc, sz, ptr));
209
}
210
211
/*====================================================================
212
 * ObjExtend()
213
 *
214
 * This function extends the used part of the object a number of bytes
215
 * into the last space returned by ObjGetSpace()
216
 */
217
218
void
219 110199
ObjExtend(struct worker *wrk, struct objcore *oc, ssize_t l)
220
{
221 110199
        const struct obj_methods *om = obj_getmethods(oc);
222
223 110199
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
224 110199
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
225 110199
        assert(l > 0);
226
227 110199
        Lck_Lock(&oc->boc->mtx);
228 110199
        AN(om->objextend);
229 110199
        om->objextend(wrk, oc, l);
230 110199
        oc->boc->len_so_far += l;
231 110199
        Lck_Unlock(&oc->boc->mtx);
232 110199
        AZ(pthread_cond_broadcast(&oc->boc->cond));
233 110199
}
234
235
/*====================================================================
236
 */
237
238
uint64_t
239 2942
ObjWaitExtend(const struct worker *wrk, const struct objcore *oc, uint64_t l)
240
{
241
        uint64_t rv;
242
243 2942
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
244 2942
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
245 2942
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
246 2942
        Lck_Lock(&oc->boc->mtx);
247
        while (1) {
248 5918
                rv = oc->boc->len_so_far;
249 4430
                assert(l <= rv || oc->boc->state == BOS_FAILED);
250 4430
                if (rv > l || oc->boc->state >= BOS_FINISHED)
251
                        break;
252 1488
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx, 0);
253
        }
254 2942
        rv = oc->boc->len_so_far;
255 2942
        Lck_Unlock(&oc->boc->mtx);
256 2942
        return (rv);
257
}
258
259
/*====================================================================
260
 */
261
262
void
263 9412
ObjSetState(struct worker *wrk, const struct objcore *oc,
264
    enum boc_state_e next)
265
{
266
        const struct obj_methods *om;
267
268 9412
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
269 9412
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
270 9412
        assert(next > oc->boc->state);
271
272 9412
        CHECK_OBJ_ORNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
273 9412
        assert(next != BOS_STREAM || oc->boc->state == BOS_PREP_STREAM);
274 9412
        assert(next != BOS_FINISHED || (oc->oa_present & (1 << OA_LEN)));
275
276 9412
        if (oc->stobj->stevedore != NULL) {
277 6108
                om = oc->stobj->stevedore->methods;
278 6108
                if (om->objsetstate != NULL)
279 0
                        om->objsetstate(wrk, oc, next);
280
        }
281
282 9412
        Lck_Lock(&oc->boc->mtx);
283 9412
        oc->boc->state = next;
284 9412
        AZ(pthread_cond_broadcast(&oc->boc->cond));
285 9411
        Lck_Unlock(&oc->boc->mtx);
286 9412
}
287
288
/*====================================================================
289
 */
290
291
void
292 3961
ObjWaitState(const struct objcore *oc, enum boc_state_e want)
293
{
294
295 3961
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
296 3961
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
297
298 3961
        Lck_Lock(&oc->boc->mtx);
299
        while (1) {
300 140429
                if (oc->boc->state >= want)
301 3959
                        break;
302 68236
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx, 0);
303
        }
304 3959
        Lck_Unlock(&oc->boc->mtx);
305 3959
}
306
307
/*====================================================================
308
 * ObjGetlen()
309
 *
310
 * This is a separate function because it may need locking
311
 */
312
313
uint64_t
314 7086
ObjGetLen(struct worker *wrk, struct objcore *oc)
315
{
316
        uint64_t len;
317
318 7086
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
319
320 7086
        AZ(ObjGetU64(wrk, oc, OA_LEN, &len));
321 7086
        return(len);
322
}
323
324
/*====================================================================
325
 * ObjTrimStore()
326
 *
327
 * Release any surplus space allocated, we promise not to call ObjExtend()
328
 * any more.
329
 */
330
331
void
332 1792
ObjTrimStore(struct worker *wrk, struct objcore *oc)
333
{
334 1792
        const struct obj_methods *om = obj_getmethods(oc);
335
336 1792
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
337
338 1792
        assert(oc->boc == NULL || oc->boc->state < BOS_FINISHED);
339
340 1792
        if (om->objtrimstore != NULL)
341 1792
                om->objtrimstore(wrk, oc);
342 1792
}
343
344
/*====================================================================
345
 * ObjSlim()
346
 *
347
 * Free the whatever storage can be freed, without freeing the actual
348
 * object yet.
349
 */
350
351
void
352 1708
ObjSlim(struct worker *wrk, struct objcore *oc)
353
{
354 1708
        const struct obj_methods *om = obj_getmethods(oc);
355
356 1708
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
357
358 1708
        if (om->objslim != NULL)
359 1708
                om->objslim(wrk, oc);
360 1708
}
361
362
/*====================================================================
363
 * Called when the boc used to populate the objcore is going away.
364
 * Useful for releasing any leftovers from Trim.
365
 */
366
367
void
368 3928
ObjBocDone(struct worker *wrk, struct objcore *oc, struct boc **boc)
369
{
370
        const struct obj_methods *m;
371
372 3928
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
373 3928
        AN(boc);
374 3928
        CHECK_OBJ_NOTNULL(*boc, BOC_MAGIC);
375 3928
        if (oc->stobj->stevedore != NULL) {
376 3890
                m = obj_getmethods(oc);
377 3890
                if (m->objbocdone != NULL)
378 3890
                        m->objbocdone(wrk, oc, *boc);
379
        }
380 3928
        obj_deleteboc(boc);
381 3928
}
382
383
/*====================================================================
384
 */
385
void
386 2226
ObjFreeObj(struct worker *wrk, struct objcore *oc)
387
{
388 2226
        const struct obj_methods *m = obj_getmethods(oc);
389
390 2226
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
391
392 2226
        AN(m->objfree);
393 2226
        m->objfree(wrk, oc);
394 2226
        AZ(oc->stobj->stevedore);
395 2226
}
396
397
/*====================================================================
398
 * ObjHasAttr()
399
 *
400
 * Check if object has this attribute
401
 */
402
403
int
404 10104
ObjHasAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr)
405
{
406
407 10104
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
408 10104
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
409
410 10104
        if (oc->oa_present)
411 10084
                return (oc->oa_present & (1 << attr));
412
413
        /* resurrected persistent objects don't have oa_present set */
414 20
        return (ObjGetAttr(wrk, oc, attr, NULL) != NULL ? 1 : 0);
415
}
416
417
/*====================================================================
418
 * ObjGetAttr()
419
 *
420
 * Get an attribute of the object.
421
 *
422
 * Returns NULL on unset or zero length attributes and len set to
423
 * zero. Returns Non-NULL otherwise and len is updated with the attributes
424
 * length.
425
 */
426
427
const void *
428 30493
ObjGetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
429
   ssize_t *len)
430
{
431 30493
        const struct obj_methods *om = obj_getmethods(oc);
432
433 30492
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
434
435 30492
        AN(om->objgetattr);
436 30492
        return (om->objgetattr(wrk, oc, attr, len));
437
}
438
439
/*====================================================================
440
 * ObjSetAttr()
441
 *
442
 * Setting fixed size attributes always succeeds.
443
 *
444
 * Setting a variable size attribute asserts if the combined size of the
445
 * variable attributes exceeds the total variable attribute space set at
446
 * object creation. If there is space it always succeeds.
447
 *
448
 * Setting an auxiliary attribute can fail.
449
 *
450
 * Resetting any variable asserts if the new length does not match the
451
 * previous length exactly.
452
 *
453
 * If ptr is Non-NULL, it points to the new content which is copied into
454
 * the attribute.  Otherwise the caller will have to do the copying.
455
 *
456
 * Return value is non-NULL on success and NULL on failure. If ptr was
457
 * non-NULL, it is an error to use the returned pointer to set the
458
 * attribute data, it is only a success indicator in that case.
459
 */
460
461
void *
462 15334
ObjSetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
463
    ssize_t len, const void *ptr)
464
{
465 15334
        const struct obj_methods *om = obj_getmethods(oc);
466
        void *r;
467
468 15332
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
469 15332
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
470
471 15332
        AN(om->objsetattr);
472 15332
        assert((int)attr < 16);
473 15332
        r = om->objsetattr(wrk, oc, attr, len, ptr);
474 15333
        if (r)
475 15333
                oc->oa_present |= (1 << attr);
476 15333
        return (r);
477
}
478
479
/*====================================================================
480
 * ObjTouch()
481
 */
482
483
void
484 4478
ObjTouch(struct worker *wrk, struct objcore *oc, double now)
485
{
486 4478
        const struct obj_methods *om = obj_getmethods(oc);
487
488 4478
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
489 4478
        if (om->objtouch != NULL)
490 4418
                om->objtouch(wrk, oc, now);
491 4478
}
492
493
/*====================================================================
494
 * Utility functions which work on top of the previous ones
495
 */
496
497
int
498 70
ObjCopyAttr(struct worker *wrk, struct objcore *oc, struct objcore *ocs,
499
    enum obj_attr attr)
500
{
501
        const void *vps;
502
        void *vpd;
503
        ssize_t l;
504
505 70
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
506 70
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
507 70
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
508 70
        CHECK_OBJ_NOTNULL(ocs, OBJCORE_MAGIC);
509
510 70
        vps = ObjGetAttr(wrk, ocs, attr, &l);
511
        // XXX: later we want to have zero-length OA's too
512 70
        if (vps == NULL || l <= 0)
513 0
                return (-1);
514 70
        vpd = ObjSetAttr(wrk, oc, attr, l, vps);
515 70
        if (vpd == NULL)
516 0
                return (-1);
517 70
        return (0);
518
}
519
520
unsigned
521 3006
ObjGetXID(struct worker *wrk, struct objcore *oc)
522
{
523
        uint32_t u;
524
525 3006
        AZ(ObjGetU32(wrk, oc, OA_VXID, &u));
526 3006
        return (u);
527
}
528
529
/*--------------------------------------------------------------------
530
 * There is no well-defined byteorder for IEEE-754 double and the
531
 * correct solution (frexp(3) and manual encoding) is more work
532
 * than our (weak) goal of being endian-agnostic requires at this point.
533
 * We give it a shot by memcpy'ing doubles over a uint64_t and then
534
 * BE encode that.
535
 */
536
537
int
538 3284
ObjSetDouble(struct worker *wrk, struct objcore *oc, enum obj_attr a, double t)
539
{
540
        void *vp;
541
        uint64_t u;
542
543
        assert(sizeof t == sizeof u);
544 3284
        memcpy(&u, &t, sizeof u);
545 3284
        vp = ObjSetAttr(wrk, oc, a, sizeof u, NULL);
546 3284
        if (vp == NULL)
547 0
                return (-1);
548 3284
        vbe64enc(vp, u);
549 3284
        return (0);
550
}
551
552
int
553 4
ObjGetDouble(struct worker *wrk, struct objcore *oc, enum obj_attr a, double *d)
554
{
555
        const void *vp;
556
        uint64_t u;
557
        ssize_t l;
558
559
        assert(sizeof *d == sizeof u);
560 4
        vp = ObjGetAttr(wrk, oc, a, &l);
561 4
        if (vp == NULL)
562 0
                return (-1);
563 4
        if (d != NULL) {
564 4
                assert(l == sizeof u);
565 4
                u = vbe64dec(vp);
566 4
                memcpy(d, &u, sizeof *d);
567
        }
568 4
        return (0);
569
}
570
571
/*--------------------------------------------------------------------
572
 */
573
574
int
575 3752
ObjSetU64(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint64_t t)
576
{
577
        void *vp;
578
579 3752
        vp = ObjSetAttr(wrk, oc, a, sizeof t, NULL);
580 3752
        if (vp == NULL)
581 0
                return (-1);
582 3752
        vbe64enc(vp, t);
583 3752
        return (0);
584
}
585
586
int
587 7086
ObjGetU64(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint64_t *d)
588
{
589
        const void *vp;
590
        ssize_t l;
591
592 7086
        vp = ObjGetAttr(wrk, oc, a, &l);
593 7086
        if (vp == NULL || l != sizeof *d)
594 0
                return (-1);
595 7086
        if (d != NULL)
596 7086
                *d = vbe64dec(vp);
597 7086
        return (0);
598
}
599
600
int
601 3284
ObjSetU32(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint32_t t)
602
{
603
        void *vp;
604
605 3284
        vp = ObjSetAttr(wrk, oc, a, sizeof t, NULL);
606 3284
        if (vp == NULL)
607 0
                return (-1);
608 3284
        vbe32enc(vp, t);
609 3284
        return (0);
610
}
611
612
int
613 3006
ObjGetU32(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint32_t *d)
614
{
615
        const void *vp;
616
        ssize_t l;
617
618 3006
        vp = ObjGetAttr(wrk, oc, a, &l);
619 3006
        if (vp == NULL || l != sizeof *d)
620 0
                return (-1);
621 3006
        if (d != NULL)
622 3006
                *d = vbe32dec(vp);
623 3006
        return (0);
624
}
625
626
/*--------------------------------------------------------------------
627
 */
628
629
int
630 9988
ObjCheckFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of)
631
{
632
        const uint8_t *fp;
633
634 9988
        fp = ObjGetAttr(wrk, oc, OA_FLAGS, NULL);
635 9988
        AN(fp);
636 9988
        return ((*fp) & of);
637
}
638
639
void
640 720
ObjSetFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of, int val)
641
{
642
        uint8_t *fp;
643
644 720
        fp = ObjSetAttr(wrk, oc, OA_FLAGS, 1, NULL);
645 720
        AN(fp);
646 720
        if (val)
647 686
                (*fp) |= of;
648
        else
649 34
                (*fp) &= ~of;
650 720
}
651
652
/*====================================================================
653
 * Object event subscribtion mechanism.
654
 *
655
 * XXX: it is extremely unclear what the locking circumstances are here.
656
 */
657
658
struct oev_entry {
659
        unsigned                        magic;
660
#define OEV_MAGIC                       0xb0b7c5a1
661
        unsigned                        mask;
662
        obj_event_f                     *func;
663
        void                            *priv;
664
        VTAILQ_ENTRY(oev_entry)         list;
665
};
666
667
static VTAILQ_HEAD(,oev_entry)          oev_list;
668
static pthread_rwlock_t                 oev_rwl;
669
static unsigned                         oev_mask;
670
671
uintptr_t
672 78
ObjSubscribeEvents(obj_event_f *func, void *priv, unsigned mask)
673
{
674
        struct oev_entry *oev;
675
676 78
        AN(func);
677 78
        AZ(mask & ~OEV_MASK);
678
679 78
        ALLOC_OBJ(oev, OEV_MAGIC);
680 78
        AN(oev);
681 78
        oev->func = func;
682 78
        oev->priv = priv;
683 78
        oev->mask = mask;
684 78
        AZ(pthread_rwlock_wrlock(&oev_rwl));
685 78
        VTAILQ_INSERT_TAIL(&oev_list, oev, list);
686 78
        oev_mask |= mask;
687 78
        AZ(pthread_rwlock_unlock(&oev_rwl));
688 78
        return ((uintptr_t)oev);
689
}
690
691
void
692 2
ObjUnsubscribeEvents(uintptr_t *handle)
693
{
694 2
        struct oev_entry *oev, *oev2 = NULL;
695 2
        unsigned newmask = 0;
696
697 2
        AN(handle);
698 2
        AN(*handle);
699 2
        AZ(pthread_rwlock_wrlock(&oev_rwl));
700 4
        VTAILQ_FOREACH(oev, &oev_list, list) {
701 2
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
702 2
                if ((uintptr_t)oev == *handle)
703 2
                        oev2 = oev;
704
                else
705 0
                        newmask |= oev->mask;
706
        }
707 2
        AN(oev2);
708 2
        VTAILQ_REMOVE(&oev_list, oev2, list);
709 2
        oev_mask = newmask;
710 2
        AZ(newmask & ~OEV_MASK);
711 2
        AZ(pthread_rwlock_unlock(&oev_rwl));
712 2
        FREE_OBJ(oev2);
713 2
        *handle = 0;
714 2
}
715
716
void
717 5118
ObjSendEvent(struct worker *wrk, struct objcore *oc, unsigned event)
718
{
719
        struct oev_entry *oev;
720
721 5118
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
722 5118
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
723 5118
        AN(event & OEV_MASK);
724 5118
        AZ(event & ~OEV_MASK);
725 5118
        if (!(event & oev_mask))
726 4944
                return;
727
728 174
        AZ(pthread_rwlock_rdlock(&oev_rwl));
729 380
        VTAILQ_FOREACH(oev, &oev_list, list) {
730 206
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
731 206
                if (event & oev->mask)
732 206
                        oev->func(wrk, oev->priv, oc, event);
733
        }
734 174
        AZ(pthread_rwlock_unlock(&oev_rwl));
735
736
}
737
738
void
739 1376
ObjInit(void)
740
{
741 1376
        VTAILQ_INIT(&oev_list);
742 1376
        AZ(pthread_rwlock_init(&oev_rwl, NULL));
743 1376
}