Changeset: 0a1c94920c84 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/0a1c94920c84
Modified Files:
        gdk/gdk.h
        gdk/gdk_aggr.c
        gdk/gdk_analytic_bounds.c
        gdk/gdk_analytic_func.c
Branch: Jul2021
Log Message:

Start using bat iterators to protect access to heaps.
Initialize an iterator, then use the base field instead of Tloc.


diffs (truncated from 3653 to 300 lines):

diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -803,8 +803,8 @@ typedef struct BAT {
 typedef struct BATiter {
        BAT *b;
        Heap *h;
+       void *base;
        Heap *vh;
-       BUN baseoff;
        BUN count;
        uint16_t width;
        uint8_t shift;
@@ -900,8 +900,8 @@ bat_iterator(BAT *b)
                bi = (BATiter) {
                        .b = b,
                        .h = b->theap,
+                       .base = b->theap->base ? b->theap->base + (b->tbaseoff 
<< b->tshift) : NULL,
                        .vh = b->tvheap,
-                       .baseoff = b->tbaseoff,
                        .count = b->batCount,
                        .width = b->twidth,
                        .shift = b->tshift,
@@ -947,8 +947,8 @@ bat_iterator_nolock(BAT *b)
                return (BATiter) {
                        .b = b,
                        .h = b->theap,
+                       .base = b->theap->base ? b->theap->base + (b->tbaseoff 
<< b->tshift) : NULL,
                        .vh = b->tvheap,
-                       .baseoff = b->tbaseoff,
                        .count = b->batCount,
                        .width = b->twidth,
                        .shift = b->tshift,
@@ -1094,9 +1094,9 @@ typedef var_t stridx_t;
 #define SIZEOF_STRIDX_T SIZEOF_VAR_T
 #define GDK_VARALIGN SIZEOF_STRIDX_T
 
-#define BUNtvaroff(bi,p) VarHeapVal((bi).h->base + ((bi).baseoff << 
(bi).shift), (p), (bi).width)
+#define BUNtvaroff(bi,p) VarHeapVal((bi).base, (p), (bi).width)
 
-#define BUNtloc(bi,p)  (ATOMstorage((bi).type) == TYPE_msk ? Tmsk(&(bi), p) : 
(bi).h->base + (((bi).baseoff + (p)) << (bi).shift))
+#define BUNtloc(bi,p)  (ATOMstorage((bi).type) == TYPE_msk ? Tmsk(&(bi), p) : 
(void *) ((char *) (bi).base + ((p) << (bi).shift)))
 #define BUNtpos(bi,p)  Tpos(&(bi),p)
 #define BUNtvar(bi,p)  (assert((bi).type && (bi).b->tvarsized), (void *) 
((bi).vh->base+BUNtvaroff(bi,p)))
 #define BUNtail(bi,p)  
((bi).type?(bi).b->tvarsized?BUNtvar(bi,p):BUNtloc(bi,p):BUNtpos(bi,p))
diff --git a/gdk/gdk_aggr.c b/gdk/gdk_aggr.c
--- a/gdk/gdk_aggr.c
+++ b/gdk/gdk_aggr.c
@@ -927,15 +927,11 @@ BATgroupsum(BAT *b, BAT *g, BAT *e, BAT 
        else
                gids = (const oid *) Tloc(g, 0);
 
-       MT_lock_set(&b->theaplock);
-       Heap *h = b->theap;
-       HEAPincref(h);
-       BUN baseoff = b->tbaseoff;
-       MT_lock_unset(&b->theaplock);
-       nils = dosum(h->base + (baseoff << b->tshift), b->tnonil, b->hseqbase, 
&ci, ncand,
+       BATiter bi = bat_iterator(b);
+       nils = dosum(bi.base, b->tnonil, b->hseqbase, &ci, ncand,
                     Tloc(bn, 0), ngrp, b->ttype, tp, gids, min, max,
                     skip_nils, abort_on_error, true, __func__, &algo);
-       HEAPdecref(h, false);
+       bat_iterator_end(&bi);
 
        if (nils < BUN_NONE) {
                BATsetcount(bn, ngrp);
@@ -968,19 +964,15 @@ mskCountOnes(BAT *b, struct canditer *ci
        if (ci->s == NULL && mask_cand(b))
                return BATcount(b);
        if (ci->tpe == cand_dense && ncand > 0 && !mask_cand(b)) {
-               MT_lock_set(&b->theaplock);
-               Heap *h = b->theap;
-               BUN baseoff = b->tbaseoff;
-               HEAPincref(h);
-               MT_lock_unset(&b->theaplock);
-               const uint32_t *restrict src = (const uint32_t *) (h->base + 
baseoff + 4 * ((ci->seq - b->hseqbase) / 32));
+               BATiter bi = bat_iterator(b);
+               const uint32_t *restrict src = (const uint32_t *) bi.base + 
(ci->seq - b->hseqbase) / 32;
                int bits = (ci->seq - b->hseqbase) % 32;
                if (bits + ncand <= 32) {
                        if (ncand == 32)
                                cnt = candmask_pop(src[0]);
                        else
                                cnt = candmask_pop(src[0] & (((1U << ncand) - 
1) << bits));
-                       HEAPdecref(h, false);
+                       bat_iterator_end(&bi);
                        return cnt;
                }
                if (bits != 0) {
@@ -995,7 +987,7 @@ mskCountOnes(BAT *b, struct canditer *ci
                }
                if (ncand > 0)
                        cnt += candmask_pop(*src & ((1U << ncand) - 1));
-               HEAPdecref(h, false);
+               bat_iterator_end(&bi);
                return cnt;
        }
        for (BUN i = 0; i < ncand; i++) {
@@ -1167,15 +1159,11 @@ BATsum(void *res, int tp, BAT *b, BAT *s
        }
        if (ncand == 0)
                return GDK_SUCCEED;
-       MT_lock_set(&b->theaplock);
-       Heap *h = b->theap;
-       HEAPincref(h);
-       BUN baseoff = b->tbaseoff;
-       MT_lock_unset(&b->theaplock);
-       nils = dosum(h->base + (baseoff << b->tshift), b->tnonil, b->hseqbase, 
&ci, ncand,
+       BATiter bi = bat_iterator(b);
+       nils = dosum(bi.base, b->tnonil, b->hseqbase, &ci, ncand,
                     res, true, b->ttype, tp, &min, min, max,
                     skip_nils, abort_on_error, nil_if_empty, __func__, &algo);
-       HEAPdecref(h, false);
+       bat_iterator_end(&bi);
        if (algo)
                MT_thread_setalgorithm(algo);
        TRC_DEBUG(ALGO, "b=" ALGOBATFMT ",s=" ALGOOPTBATFMT "; "
@@ -1615,15 +1603,11 @@ BATgroupprod(BAT *b, BAT *g, BAT *e, BAT
        else
                gids = (const oid *) Tloc(g, 0);
 
-       MT_lock_set(&b->theaplock);
-       Heap *h = b->theap;
-       HEAPincref(h);
-       BUN baseoff = b->tbaseoff;
-       MT_lock_unset(&b->theaplock);
-       nils = doprod(h->base + (baseoff << b->tshift), b->hseqbase, &ci, 
ncand, Tloc(bn, 0), ngrp,
+       BATiter bi = bat_iterator(b);
+       nils = doprod(bi.base, b->hseqbase, &ci, ncand, Tloc(bn, 0), ngrp,
                      b->ttype, tp, gids, true, min, max, skip_nils,
                      abort_on_error, true, __func__);
-       HEAPdecref(h, false);
+       bat_iterator_end(&bi);
 
        if (nils < BUN_NONE) {
                BATsetcount(bn, ngrp);
@@ -1695,15 +1679,11 @@ BATprod(void *res, int tp, BAT *b, BAT *
        }
        if (ncand == 0)
                return GDK_SUCCEED;
-       MT_lock_set(&b->theaplock);
-       Heap *h = b->theap;
-       HEAPincref(h);
-       BUN baseoff = b->tbaseoff;
-       MT_lock_unset(&b->theaplock);
-       nils = doprod(h->base + (baseoff << b->tshift), b->hseqbase, &ci, 
ncand, res, true,
+       BATiter bi = bat_iterator(b);
+       nils = doprod(bi.base, b->hseqbase, &ci, ncand, res, true,
                      b->ttype, tp, &min, false, min, max,
                      skip_nils, abort_on_error, nil_if_empty, __func__);
-       HEAPdecref(h, false);
+       bat_iterator_end(&bi);
        TRC_DEBUG(ALGO, "b=" ALGOBATFMT ",s=" ALGOOPTBATFMT "; "
                  "start " OIDFMT ", count " BUNFMT " (" LLFMT " usec)\n",
                  ALGOBATPAR(b), ALGOOPTBATPAR(s),
@@ -1716,10 +1696,12 @@ BATprod(void *res, int tp, BAT *b, BAT *
 
 #define AGGR_AVG(TYPE)                                                 \
        do {                                                            \
-               const TYPE *restrict vals = (const TYPE *) h->base + baseoff; \
+               const TYPE *restrict vals = (const TYPE *) bi.base;     \
                TYPE *restrict avgs = GDKzalloc(ngrp * sizeof(TYPE));   \
-               if (avgs == NULL)                                       \
+               if (avgs == NULL) {                                     \
+                       bat_iterator_end(&bi);                          \
                        goto alloc_fail;                                \
+               }                                                       \
                while (ncand > 0) {                                     \
                        ncand--;                                        \
                        i = canditer_next(&ci) - b->hseqbase;           \
@@ -1754,7 +1736,7 @@ BATprod(void *res, int tp, BAT *b, BAT *
 
 #define AGGR_AVG_FLOAT(TYPE)                                           \
        do {                                                            \
-               const TYPE *restrict vals = (const TYPE *) h->base + baseoff; \
+               const TYPE *restrict vals = (const TYPE *) bi.base;     \
                for (i = 0; i < ngrp; i++)                              \
                        dbls[i] = 0;                                    \
                while (ncand > 0) {                                     \
@@ -1894,14 +1876,8 @@ BATgroupavg(BAT **bnp, BAT **cntsp, BAT 
        else
                gids = (const oid *) Tloc(g, 0);
 
-       MT_lock_set(&b->theaplock);
-       Heap *h;
-       h = b->theap;
-       HEAPincref(h);
-       BUN baseoff;
-       baseoff = b->tbaseoff;
-       MT_lock_unset(&b->theaplock);
-
+       BATiter bi;
+       bi = bat_iterator(b);
        switch (b->ttype) {
        case TYPE_bte:
                AGGR_AVG(bte);
@@ -1927,7 +1903,7 @@ BATgroupavg(BAT **bnp, BAT **cntsp, BAT 
                AGGR_AVG_FLOAT(dbl);
                break;
        default:
-               HEAPdecref(h, false);
+               bat_iterator_end(&bi);
                GDKfree(rems);
                if (cn)
                        BBPreclaim(cn);
@@ -1937,7 +1913,7 @@ BATgroupavg(BAT **bnp, BAT **cntsp, BAT 
                GDKerror("type (%s) not supported.\n", ATOMname(b->ttype));
                return GDK_FAIL;
        }
-       HEAPdecref(h, false);
+       bat_iterator_end(&bi);
        GDKfree(rems);
        if (cn == NULL)
                GDKfree(cnts);
@@ -2043,15 +2019,11 @@ BATgroupavg3(BAT **avgp, BAT **remp, BAT
        const oid *gids = g && !BATtdense(g) ? Tloc(g, 0) : NULL;
        oid gid = ngrp == 1 && gids ? gids[0] - min : 0;
 
-       MT_lock_set(&b->theaplock);
-       Heap *h = b->theap;
-       HEAPincref(h);
-       BUN baseoff = b->tbaseoff;
-       MT_lock_unset(&b->theaplock);
+       BATiter bi = bat_iterator(b);
 
        switch (ATOMbasetype(b->ttype)) {
        case TYPE_bte: {
-               const bte *vals = (const bte *) h->base + baseoff;
+               const bte *vals = (const bte *) bi.base;
                bte *avgs = Tloc(bn, 0);
                for (i = 0; i < ncand; i++) {
                        o = canditer_next(&ci) - b->hseqbase;
@@ -2099,7 +2071,7 @@ BATgroupavg3(BAT **avgp, BAT **remp, BAT
                break;
        }
        case TYPE_sht: {
-               const sht *vals = (const sht *) h->base + baseoff;
+               const sht *vals = (const sht *) bi.base;
                sht *avgs = Tloc(bn, 0);
                for (i = 0; i < ncand; i++) {
                        o = canditer_next(&ci) - b->hseqbase;
@@ -2147,7 +2119,7 @@ BATgroupavg3(BAT **avgp, BAT **remp, BAT
                break;
        }
        case TYPE_int: {
-               const int *vals = (const int *) h->base + baseoff;
+               const int *vals = (const int *) bi.base;
                int *avgs = Tloc(bn, 0);
                for (i = 0; i < ncand; i++) {
                        o = canditer_next(&ci) - b->hseqbase;
@@ -2195,7 +2167,7 @@ BATgroupavg3(BAT **avgp, BAT **remp, BAT
                break;
        }
        case TYPE_lng: {
-               const lng *vals = (const lng *) h->base + baseoff;
+               const lng *vals = (const lng *) bi.base;
                lng *avgs = Tloc(bn, 0);
                for (i = 0; i < ncand; i++) {
                        o = canditer_next(&ci) - b->hseqbase;
@@ -2244,7 +2216,7 @@ BATgroupavg3(BAT **avgp, BAT **remp, BAT
        }
 #ifdef HAVE_HGE
        case TYPE_hge: {
-               const hge *vals = (const hge *) h->base + baseoff;
+               const hge *vals = (const hge *) bi.base;
                hge *avgs = Tloc(bn, 0);
                for (i = 0; i < ncand; i++) {
                        o = canditer_next(&ci) - b->hseqbase;
@@ -2293,7 +2265,7 @@ BATgroupavg3(BAT **avgp, BAT **remp, BAT
        }
 #endif
        }
-       HEAPdecref(h, false);
+       bat_iterator_end(&bi);
        BATsetcount(bn, ngrp);
        BATsetcount(rn, ngrp);
        BATsetcount(cn, ngrp);
@@ -2669,15 +2641,11 @@ BATgroupavg3combine(BAT *avg, BAT *rem, 
        const oid *gids = g && !BATtdense(g) ? Tloc(g, 0) : NULL;
        oid gid = ngrp == 1 && gids ? gids[0] - min : 0;
 
-       MT_lock_set(&avg->theaplock);
-       Heap *h = avg->theap;
-       HEAPincref(h);
-       BUN baseoff = avg->tbaseoff;
-       MT_lock_unset(&avg->theaplock);
+       BATiter bi = bat_iterator(avg);
 
        switch (ATOMbasetype(avg->ttype)) {
        case TYPE_bte: {
-               const bte *vals = (const bte *) h->base + baseoff;
+               const bte *vals = (const bte *) bi.base;
                bte *avgs = Tloc(bn, 0);
                for (i = 0; i < ncand; i++) {
                        if (ngrp > 1)
@@ -2718,7 +2686,7 @@ BATgroupavg3combine(BAT *avg, BAT *rem, 
                break;
        }
        case TYPE_sht: {
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to