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 136737
obj_getmethods(const struct objcore *oc)
93
{
94
95 136737
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
96 136737
        CHECK_OBJ_NOTNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
97 136737
        AN(oc->stobj->stevedore->methods);
98 136737
        return (oc->stobj->stevedore->methods);
99
}
100
101
static struct boc *
102 1759
obj_newboc(void)
103
{
104
        struct boc *boc;
105
106 1759
        ALLOC_OBJ(boc, BOC_MAGIC);
107 1759
        AN(boc);
108 1759
        Lck_New(&boc->mtx, lck_busyobj);
109 1759
        AZ(pthread_cond_init(&boc->cond, NULL));
110 1759
        boc->refcount = 1;
111 1759
        return (boc);
112
}
113
114
static void
115 1602
obj_deleteboc(struct boc **p)
116
{
117
        struct boc *boc;
118
119 1602
        AN(p);
120 1602
        boc = *p;
121 1602
        *p = NULL;
122 1602
        Lck_Delete(&boc->mtx);
123 1602
        AZ(pthread_cond_destroy(&boc->cond));
124 1602
        if (boc->vary != NULL)
125 93
                free(boc->vary);
126 1602
        FREE_OBJ(boc);
127 1602
}
128
129
/*====================================================================
130
 * ObjNew()
131
 *
132
 */
133
134
struct objcore *
135 1758
ObjNew(const struct worker *wrk)
136
{
137
        struct objcore *oc;
138
139 1758
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
140
141 1758
        ALLOC_OBJ(oc, OBJCORE_MAGIC);
142 1758
        AN(oc);
143 1758
        wrk->stats->n_objectcore++;
144 1758
        oc->last_lru = NAN;
145 1758
        oc->flags = OC_F_BUSY;
146
147 1758
        oc->boc = obj_newboc();
148
149 1759
        return (oc);
150
}
151
152
/*====================================================================
153
 * ObjDestroy()
154
 *
155
 */
156
157
void
158 817
ObjDestroy(const struct worker *wrk, struct objcore **p)
159
{
160
        struct objcore *oc;
161
162 817
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
163 817
        TAKE_OBJ_NOTNULL(oc, p, OBJCORE_MAGIC);
164 817
        if (oc->boc != NULL)
165 24
                obj_deleteboc(&oc->boc);
166 817
        FREE_OBJ(oc);
167 817
        wrk->stats->n_objectcore--;
168 817
}
169
170
/*====================================================================
171
 * ObjIterate()
172
 *
173
 */
174
175
int
176 1289
ObjIterate(struct worker *wrk, struct objcore *oc,
177
    void *priv, objiterate_f *func, int final)
178
{
179 1289
        const struct obj_methods *om = obj_getmethods(oc);
180
181 1289
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
182 1289
        AN(func);
183 1289
        AN(om->objiterator);
184 1289
        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 56003
ObjGetSpace(struct worker *wrk, struct objcore *oc, ssize_t *sz, uint8_t **ptr)
198
{
199 56003
        const struct obj_methods *om = obj_getmethods(oc);
200
201 56003
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
202 56003
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
203 56003
        AN(sz);
204 56003
        AN(ptr);
205 56003
        assert(*sz > 0);
206
207 56003
        AN(om->objgetspace);
208 56003
        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 54872
ObjExtend(struct worker *wrk, struct objcore *oc, ssize_t l)
220
{
221 54872
        const struct obj_methods *om = obj_getmethods(oc);
222
223 54872
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
224 54872
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
225 54872
        assert(l > 0);
226
227 54872
        Lck_Lock(&oc->boc->mtx);
228 54872
        AN(om->objextend);
229 54872
        om->objextend(wrk, oc, l);
230 54872
        oc->boc->len_so_far += l;
231 54872
        Lck_Unlock(&oc->boc->mtx);
232 54872
        AZ(pthread_cond_broadcast(&oc->boc->cond));
233 54872
}
234
235
/*====================================================================
236
 */
237
238
uint64_t
239 1114
ObjWaitExtend(const struct worker *wrk, const struct objcore *oc, uint64_t l)
240
{
241
        uint64_t rv;
242
243 1114
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
244 1114
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
245 1114
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
246 1114
        Lck_Lock(&oc->boc->mtx);
247
        while (1) {
248 1666
                rv = oc->boc->len_so_far;
249 1666
                assert(l <= rv || oc->boc->state == BOS_FAILED);
250 1666
                if (rv > l || oc->boc->state >= BOS_FINISHED)
251
                        break;
252 552
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx, 0);
253 552
        }
254 1114
        rv = oc->boc->len_so_far;
255 1114
        Lck_Unlock(&oc->boc->mtx);
256 1114
        return (rv);
257
}
258
259
/*====================================================================
260
 */
261
262
void
263 3748
ObjSetState(struct worker *wrk, const struct objcore *oc,
264
    enum boc_state_e next)
265
{
266
        const struct obj_methods *om;
267
268 3748
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
269 3748
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
270 3748
        assert(next > oc->boc->state);
271
272 3748
        CHECK_OBJ_ORNULL(oc->stobj->stevedore, STEVEDORE_MAGIC);
273 3748
        assert(next != BOS_STREAM || oc->boc->state == BOS_PREP_STREAM);
274 3748
        assert(next != BOS_FINISHED || (oc->oa_present & (1 << OA_LEN)));
275
276 3748
        if (oc->stobj->stevedore != NULL) {
277 2409
                om = oc->stobj->stevedore->methods;
278 2409
                if (om->objsetstate != NULL)
279 0
                        om->objsetstate(wrk, oc, next);
280
        }
281
282 3748
        Lck_Lock(&oc->boc->mtx);
283 3748
        oc->boc->state = next;
284 3748
        AZ(pthread_cond_broadcast(&oc->boc->cond));
285 3748
        Lck_Unlock(&oc->boc->mtx);
286 3748
}
287
288
/*====================================================================
289
 */
290
291
void
292 1539
ObjWaitState(const struct objcore *oc, enum boc_state_e want)
293
{
294
295 1539
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
296 1539
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
297
298 1539
        Lck_Lock(&oc->boc->mtx);
299
        while (1) {
300 34042
                if (oc->boc->state >= want)
301 1538
                        break;
302 32504
                (void)Lck_CondWait(&oc->boc->cond, &oc->boc->mtx, 0);
303 32503
        }
304 1538
        Lck_Unlock(&oc->boc->mtx);
305 1538
}
306
307
/*====================================================================
308
 * ObjGetlen()
309
 *
310
 * This is a separate function because it may need locking
311
 */
312
313
uint64_t
314 2925
ObjGetLen(struct worker *wrk, struct objcore *oc)
315
{
316
        uint64_t len;
317
318 2925
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
319
320 2925
        AZ(ObjGetU64(wrk, oc, OA_LEN, &len));
321 2925
        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 726
ObjTrimStore(struct worker *wrk, struct objcore *oc)
333
{
334 726
        const struct obj_methods *om = obj_getmethods(oc);
335
336 726
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
337
338 726
        assert(oc->boc == NULL || oc->boc->state < BOS_FINISHED);
339
340 726
        if (om->objtrimstore != NULL)
341 726
                om->objtrimstore(wrk, oc);
342 726
}
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 565
ObjSlim(struct worker *wrk, struct objcore *oc)
353
{
354 565
        const struct obj_methods *om = obj_getmethods(oc);
355
356 565
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
357
358 565
        if (om->objslim != NULL)
359 565
                om->objslim(wrk, oc);
360 565
}
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 1578
ObjBocDone(struct worker *wrk, struct objcore *oc, struct boc **boc)
369
{
370
        const struct obj_methods *m;
371
372 1578
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
373 1578
        AN(boc);
374 1578
        CHECK_OBJ_NOTNULL(*boc, BOC_MAGIC);
375 1578
        if (oc->stobj->stevedore != NULL) {
376 1561
                m = obj_getmethods(oc);
377 1561
                if (m->objbocdone != NULL)
378 1561
                        m->objbocdone(wrk, oc, *boc);
379
        }
380 1578
        obj_deleteboc(boc);
381 1578
}
382
383
/*====================================================================
384
 */
385
void
386 796
ObjFreeObj(struct worker *wrk, struct objcore *oc)
387
{
388 796
        const struct obj_methods *m = obj_getmethods(oc);
389
390 796
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
391
392 796
        AN(m->objfree);
393 796
        m->objfree(wrk, oc);
394 796
        AZ(oc->stobj->stevedore);
395 796
}
396
397
/*====================================================================
398
 * ObjHasAttr()
399
 *
400
 * Check if object has this attribute
401
 */
402
403
int
404 4491
ObjHasAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr)
405
{
406
407 4491
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
408 4491
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
409
410 4491
        if (oc->oa_present)
411 4481
                return (oc->oa_present & (1 << attr));
412
413
        /* resurrected persistent objects don't have oa_present set */
414 10
        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 12866
ObjGetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
429
   ssize_t *len)
430
{
431 12866
        const struct obj_methods *om = obj_getmethods(oc);
432
433 12867
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
434
435 12867
        AN(om->objgetattr);
436 12867
        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 6273
ObjSetAttr(struct worker *wrk, struct objcore *oc, enum obj_attr attr,
463
    ssize_t len, const void *ptr)
464
{
465 6273
        const struct obj_methods *om = obj_getmethods(oc);
466
        void *r;
467
468 6272
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
469 6272
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
470
471 6272
        AN(om->objsetattr);
472 6272
        assert((int)attr < 16);
473 6272
        r = om->objsetattr(wrk, oc, attr, len, ptr);
474 6273
        if (r)
475 6273
                oc->oa_present |= (1 << attr);
476 6273
        return (r);
477
}
478
479
/*====================================================================
480
 * ObjTouch()
481
 */
482
483
void
484 1786
ObjTouch(struct worker *wrk, struct objcore *oc, double now)
485
{
486 1786
        const struct obj_methods *om = obj_getmethods(oc);
487
488 1786
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
489 1786
        if (om->objtouch != NULL)
490 1756
                om->objtouch(wrk, oc, now);
491 1786
}
492
493
/*====================================================================
494
 * Utility functions which work on top of the previous ones
495
 */
496
497
int
498 27
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 27
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
506 27
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
507 27
        CHECK_OBJ_NOTNULL(oc->boc, BOC_MAGIC);
508 27
        CHECK_OBJ_NOTNULL(ocs, OBJCORE_MAGIC);
509
510 27
        vps = ObjGetAttr(wrk, ocs, attr, &l);
511
        // XXX: later we want to have zero-length OA's too
512 27
        if (vps == NULL || l <= 0)
513 0
                return (-1);
514 27
        vpd = ObjSetAttr(wrk, oc, attr, l, vps);
515 27
        if (vpd == NULL)
516 0
                return (-1);
517 27
        return (0);
518
}
519
520
unsigned
521 1232
ObjGetXID(struct worker *wrk, struct objcore *oc)
522
{
523
        uint32_t u;
524
525 1232
        AZ(ObjGetU32(wrk, oc, OA_VXID, &u));
526 1232
        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 1333
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 1333
        memcpy(&u, &t, sizeof u);
545 1333
        vp = ObjSetAttr(wrk, oc, a, sizeof u, NULL);
546 1333
        if (vp == NULL)
547 0
                return (-1);
548 1333
        vbe64enc(vp, u);
549 1333
        return (0);
550
}
551
552
int
553 2
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 2
        vp = ObjGetAttr(wrk, oc, a, &l);
561 2
        if (vp == NULL)
562 0
                return (-1);
563 2
        if (d != NULL) {
564 2
                assert(l == sizeof u);
565 2
                u = vbe64dec(vp);
566 2
                memcpy(d, &u, sizeof *d);
567
        }
568 2
        return (0);
569
}
570
571
/*--------------------------------------------------------------------
572
 */
573
574
int
575 1498
ObjSetU64(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint64_t t)
576
{
577
        void *vp;
578
579 1498
        vp = ObjSetAttr(wrk, oc, a, sizeof t, NULL);
580 1498
        if (vp == NULL)
581 0
                return (-1);
582 1498
        vbe64enc(vp, t);
583 1498
        return (0);
584
}
585
586
int
587 2925
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 2925
        vp = ObjGetAttr(wrk, oc, a, &l);
593 2925
        if (vp == NULL || l != sizeof *d)
594 0
                return (-1);
595 2925
        if (d != NULL)
596 2925
                *d = vbe64dec(vp);
597 2925
        return (0);
598
}
599
600
int
601 1333
ObjSetU32(struct worker *wrk, struct objcore *oc, enum obj_attr a, uint32_t t)
602
{
603
        void *vp;
604
605 1333
        vp = ObjSetAttr(wrk, oc, a, sizeof t, NULL);
606 1333
        if (vp == NULL)
607 0
                return (-1);
608 1333
        vbe32enc(vp, t);
609 1333
        return (0);
610
}
611
612
int
613 1232
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 1232
        vp = ObjGetAttr(wrk, oc, a, &l);
619 1232
        if (vp == NULL || l != sizeof *d)
620 0
                return (-1);
621 1232
        if (d != NULL)
622 1232
                *d = vbe32dec(vp);
623 1232
        return (0);
624
}
625
626
/*--------------------------------------------------------------------
627
 */
628
629
int
630 3992
ObjCheckFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of)
631
{
632
        const uint8_t *fp;
633
634 3992
        fp = ObjGetAttr(wrk, oc, OA_FLAGS, NULL);
635 3992
        AN(fp);
636 3992
        return ((*fp) & of);
637
}
638
639
void
640 313
ObjSetFlag(struct worker *wrk, struct objcore *oc, enum obj_flags of, int val)
641
{
642
        uint8_t *fp;
643
644 313
        fp = ObjSetAttr(wrk, oc, OA_FLAGS, 1, NULL);
645 313
        AN(fp);
646 313
        if (val)
647 313
                (*fp) |= of;
648
        else
649 0
                (*fp) &= ~of;
650 313
}
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 39
ObjSubscribeEvents(obj_event_f *func, void *priv, unsigned mask)
673
{
674
        struct oev_entry *oev;
675
676 39
        AN(func);
677 39
        AZ(mask & ~OEV_MASK);
678
679 39
        ALLOC_OBJ(oev, OEV_MAGIC);
680 39
        AN(oev);
681 39
        oev->func = func;
682 39
        oev->priv = priv;
683 39
        oev->mask = mask;
684 39
        AZ(pthread_rwlock_wrlock(&oev_rwl));
685 39
        VTAILQ_INSERT_TAIL(&oev_list, oev, list);
686 39
        oev_mask |= mask;
687 39
        AZ(pthread_rwlock_unlock(&oev_rwl));
688 39
        return ((uintptr_t)oev);
689
}
690
691
void
692 1
ObjUnsubscribeEvents(uintptr_t *handle)
693
{
694 1
        struct oev_entry *oev, *oev2 = NULL;
695 1
        unsigned newmask = 0;
696
697 1
        AN(handle);
698 1
        AN(*handle);
699 1
        AZ(pthread_rwlock_wrlock(&oev_rwl));
700 2
        VTAILQ_FOREACH(oev, &oev_list, list) {
701 1
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
702 1
                if ((uintptr_t)oev == *handle)
703 1
                        oev2 = oev;
704
                else
705 0
                        newmask |= oev->mask;
706
        }
707 1
        AN(oev2);
708 1
        VTAILQ_REMOVE(&oev_list, oev2, list);
709 1
        oev_mask = newmask;
710 1
        AZ(newmask & ~OEV_MASK);
711 1
        AZ(pthread_rwlock_unlock(&oev_rwl));
712 1
        FREE_OBJ(oev2);
713 1
        *handle = 0;
714 1
}
715
716
void
717 2349
ObjSendEvent(struct worker *wrk, struct objcore *oc, unsigned event)
718
{
719
        struct oev_entry *oev;
720
721 2349
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
722 2349
        CHECK_OBJ_NOTNULL(oc, OBJCORE_MAGIC);
723 2349
        AN(event & OEV_MASK);
724 2349
        AZ(event & ~OEV_MASK);
725 2349
        if (!(event & oev_mask))
726 4611
                return;
727
728 87
        AZ(pthread_rwlock_rdlock(&oev_rwl));
729 190
        VTAILQ_FOREACH(oev, &oev_list, list) {
730 103
                CHECK_OBJ_NOTNULL(oev, OEV_MAGIC);
731 103
                if (event & oev->mask)
732 103
                        oev->func(wrk, oev->priv, oc, event);
733
        }
734 87
        AZ(pthread_rwlock_unlock(&oev_rwl));
735
736
}
737
738
void
739 614
ObjInit(void)
740
{
741 614
        VTAILQ_INIT(&oev_list);
742 614
        AZ(pthread_rwlock_init(&oev_rwl, NULL));
743 614
}