void VBP_Init(void) { pthread_t thr; Lck_New(&vbp_mtx, lck_backend); vbp_heap = binheap_new(NULL, vbp_cmp, vbp_update); AN(vbp_heap); AZ(pthread_cond_init(&vbp_cond, NULL)); WRK_BgThread(&thr, "backend-poller", vbp_thread, NULL); }
static void test_peek_empty(void) { struct binheap *h = binheap_new(1); uint32_t v; int ret; v = magic; ret = binheap_peek(h, &v); if (!ret) { FAIL("binheap_peek returned %d on empty heap\n", ret); } if (v != magic) { FAIL("binheap_peek set v=%d expected unchanged\n", v); } free(h); }
exp_thread(struct worker *wrk, void *priv) { struct objcore *oc; double t = 0, tnext = 0; struct exp_priv *ep; unsigned flags = 0; CAST_OBJ_NOTNULL(ep, priv, EXP_PRIV_MAGIC); ep->wrk = wrk; VSL_Setup(&ep->vsl, NULL, 0); ep->heap = binheap_new(NULL, object_cmp, object_update); AN(ep->heap); while (1) { Lck_Lock(&ep->mtx); oc = VSTAILQ_FIRST(&ep->inbox); CHECK_OBJ_ORNULL(oc, OBJCORE_MAGIC); if (oc != NULL) { assert(oc->refcnt >= 1); VSTAILQ_REMOVE(&ep->inbox, oc, objcore, exp_list); VSC_C_main->exp_received++; tnext = 0; flags = oc->exp_flags; if (flags & OC_EF_REMOVE) oc->exp_flags = 0; else oc->exp_flags &= OC_EF_REFD; } else if (tnext > t) { VSL_Flush(&ep->vsl, 0); Pool_Sumstat(wrk); (void)Lck_CondWait(&ep->condvar, &ep->mtx, tnext); } Lck_Unlock(&ep->mtx); t = VTIM_real(); if (oc != NULL) exp_inbox(ep, oc, flags); else tnext = exp_expire(ep, t); } NEEDLESS(return NULL); }
static void test_pop_right_smallest(void) { struct binheap *h = binheap_new(4); uint32_t expected[] = {1, 2, 3}; uint32_t v, *p, *end; binheap_insert(h, 2); binheap_insert(h, 3); binheap_insert(h, 1); for (p = expected, end = expected + array_size(expected); p != end; ++p) { binheap_peek(h, &v); if (v != *p) { FAIL("binheap_peek set v=%d expected %d\n", v, *p); } binheap_pop(h); } free(h); }
struct waiter * Waiter_New(void) { struct waiter *w; AN(waiter); AN(waiter->name); AN(waiter->init); AN(waiter->enter); AN(waiter->fini); w = calloc(1, sizeof (struct waiter) + waiter->size); AN(w); INIT_OBJ(w, WAITER_MAGIC); w->priv = (void*)(w + 1); w->impl = waiter; VTAILQ_INIT(&w->waithead); w->heap = binheap_new(w, waited_cmp, waited_update); waiter->init(w); return (w); }
static void test_insert_one(void) { struct binheap *h = binheap_new(1); uint32_t v; int ret; binheap_insert(h, magic); ret = binheap_peek(h, &v); if (ret) { FAIL("binheap_peek returned %d after element inserted\n", ret); } if (v != magic) { FAIL("binheap_peek set v=%d expected %d\n", v, magic); } ret = binheap_pop(h); if (ret != 0) { FAIL("binheap_pop returned %d expected 0\n", v); } free(h); }
static void test_insert_reverse(void) { struct binheap *h = binheap_new(8); uint32_t i, v; binheap_insert(h, 8); binheap_insert(h, 7); binheap_insert(h, 6); binheap_insert(h, 5); binheap_insert(h, 4); binheap_insert(h, 3); binheap_insert(h, 2); binheap_insert(h, 1); for (i = 1; i <= 8; ++i) { binheap_peek(h, &v); if (v != i) { FAIL("binheap_peek set v=%d expected %d\n", v, i); } binheap_pop(h); } free(h); }