Changeset: 3c9ef8c4fb29 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/3c9ef8c4fb29
Modified Files:
        gdk/gdk.h
        gdk/gdk_aggr.c
        gdk/gdk_align.c
        gdk/gdk_bat.c
        gdk/gdk_batop.c
        gdk/gdk_bbp.c
        gdk/gdk_cand.c
        gdk/gdk_delta.c
        gdk/gdk_logger.c
        gdk/gdk_tm.c
        monetdb5/mal/mal_client.c
        monetdb5/modules/mal/tablet.c
        sql/storage/bat/bat_storage.c
Branch: default
Log Message:

Fixing data races.


diffs (truncated from 418 to 300 lines):

diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -778,6 +778,7 @@ typedef enum {
  * tsorted, trevsorted, twidth, tshift, tnonil, tnil, tnokey, tnosorted,
  * tnorevsorted, tminpos, tmaxpos, and tunique_est; in addition, the
  * value should be set if the BBP field BBP_logical(bid) is changed.
+ * This corresponds with any field that gets saved in the BBP.dir file.
  *
  * theaplock: this lock should be held when reading or writing any of
  * the fields mentioned above for batDirtydesc, and also when reading or
@@ -1297,17 +1298,15 @@ gdk_export BAT *BATsetaccess(BAT *b, res
 gdk_export restrict_t BATgetaccess(BAT *b);
 
 
-#define BATdirty(b)    (!(b)->batCopiedtodisk ||                       \
-                        (b)->batDirtydesc ||                           \
+#define BATdirtydata(b)        (!(b)->batCopiedtodisk ||                       
\
                         (b)->theap->dirty ||                           \
                         ((b)->tvheap != NULL && (b)->tvheap->dirty))
+#define BATdirty(b)    (BATdirtydata(b) ||     \
+                        (b)->batDirtydesc)
 #define BATdirtybi(bi) (!(bi).copiedtodisk ||  \
                         (bi).dirtydesc ||      \
                         (bi).hdirty ||         \
                         (bi).vhdirty)
-#define BATdirtydata(b)        (!(b)->batCopiedtodisk ||                       
\
-                        (b)->theap->dirty ||                           \
-                        ((b)->tvheap != NULL && (b)->tvheap->dirty))
 
 #define BATcapacity(b) (b)->batCapacity
 /*
diff --git a/gdk/gdk_aggr.c b/gdk/gdk_aggr.c
--- a/gdk/gdk_aggr.c
+++ b/gdk/gdk_aggr.c
@@ -3816,13 +3816,15 @@ BATmin_skipnil(BAT *b, void *aggr, bit s
                        if (bi.count == BATcount(b) && bi.h == b->theap)
                                b->tminpos = bi.minpos;
                        bat pbid = VIEWtparent(b);
+                       MT_lock_unset(&b->theaplock);
                        if (pbid) {
                                BAT *pb = BBP_cache(pbid);
+                               MT_lock_set(&pb->theaplock);
                                if (bi.count == BATcount(pb) &&
                                    bi.h == pb->theap)
                                        pb->tminpos = bi.minpos;
+                               MT_lock_unset(&pb->theaplock);
                        }
-                       MT_lock_unset(&b->theaplock);
                }
        }
        if (aggr == NULL) {
diff --git a/gdk/gdk_align.c b/gdk/gdk_align.c
--- a/gdk/gdk_align.c
+++ b/gdk/gdk_align.c
@@ -263,7 +263,6 @@ BATmaterialize(BAT *b, BUN cap)
        b->tunique_est = is_oid_nil(t) ? 1.0 : (double) b->batCount;
        b->ttype = TYPE_oid;
        BATsetdims(b, 0);
-       b->batDirtydesc = true;
        BATsetcount(b, b->batCount);
        BATsetcapacity(b, cap);
        MT_lock_unset(&b->theaplock);
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -641,8 +641,7 @@ BATclear(BAT *b, bool force)
                }
        }
 
-       if (force)
-               b->batInserted = 0;
+       b->batInserted = 0;
        b->batCount = 0;
        if (b->ttype == TYPE_void)
                b->batCapacity = 0;
diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -90,7 +90,6 @@ insert_string_bat(BAT *b, BATiter *ni, s
                HEAPdecref(b->tvheap, b->tvheap->parentid == b->batCacheid);
                HEAPincref(ni->vh);
                b->tvheap = ni->vh;
-               b->batDirtydesc = true;
                MT_lock_unset(&b->theaplock);
                BBPshare(ni->vh->parentid);
                toff = 0;
@@ -359,7 +358,6 @@ append_varsized_bat(BAT *b, BATiter *ni,
                HEAPdecref(b->tvheap, true);
                HEAPincref(ni->vh);
                b->tvheap = ni->vh;
-               b->batDirtydesc = true;
                MT_lock_unset(&b->theaplock);
        }
        if (b->tvheap == ni->vh) {
@@ -2849,7 +2847,6 @@ BATsetprop_nolock(BAT *b, enum prop_t id
                GDKclrerr();
                p = NULL;
        }
-       b->batDirtydesc = true;
        return p ? &p->v : NULL;
 }
 
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -2829,46 +2829,6 @@ incref(bat i, bool logical, bool lock)
        return refs;
 }
 
-BAT *
-BATdescriptor(bat i)
-{
-       BAT *b = NULL;
-
-       if (BBPcheck(i)) {
-               b = BBP_desc(i);
-               MT_lock_set(&b->theaplock);
-               int tp = b->theap->parentid;
-               int tvp = b->tvheap ? b->tvheap->parentid : 0;
-               MT_lock_unset(&b->theaplock);
-               if (tp != b->batCacheid &&
-                   BATdescriptor(tp) == NULL) {
-                       return NULL;
-               }
-               if (tvp != 0 &&
-                   tvp != b->batCacheid &&
-                   BATdescriptor(tvp) == NULL) {
-                       if (tp != b->batCacheid)
-                               BBPunfix(tp);
-                       return NULL;
-               }
-               for (;;) {
-                       MT_lock_set(&GDKswapLock(i));
-                       if (!(BBP_status(i) & (BBPUNSTABLE|BBPLOADING)))
-                               break;
-                       /* the BATs is "unstable", try again */
-                       MT_lock_unset(&GDKswapLock(i));
-                       BBPspin(i, __func__, BBPUNSTABLE|BBPLOADING);
-               }
-               if (incref(i, false, false) <= 0)
-                       return NULL;
-               b = BBP_cache(i);
-               if (b == NULL)
-                       b = getBBPdescriptor(i);
-               MT_lock_unset(&GDKswapLock(i));
-       }
-       return b;
-}
-
 /* increment the physical reference counter for the given bat
  * returns the new reference count
  * also increments the physical reference count of the parent bat(s) (if
@@ -2915,7 +2875,7 @@ BBPshare(bat parent)
 }
 
 static inline int
-decref(bat i, bool logical, bool releaseShare, bool lock, const char *func)
+decref(bat i, bool logical, bool releaseShare, bool recurse, bool lock, const 
char *func)
 {
        int refs = 0, lrefs;
        bool swap = false;
@@ -3062,23 +3022,25 @@ decref(bat i, bool logical, bool release
                        BBP_status_off(i, BBPUNLOADING);
                }
        }
-       if (tp)
-               decref(tp, false, false, lock, func);
-       if (tvp)
-               decref(tvp, false, false, lock, func);
+       if (recurse) {
+               if (tp)
+                       decref(tp, false, false, true, lock, func);
+               if (tvp)
+                       decref(tvp, false, false, true, lock, func);
+       }
        return refs;
 }
 
 int
 BBPunfix(bat i)
 {
-       return decref(i, false, false, true, __func__);
+       return decref(i, false, false, true, true, __func__);
 }
 
 int
 BBPrelease(bat i)
 {
-       return decref(i, true, false, true, __func__);
+       return decref(i, true, false, true, true, __func__);
 }
 
 /*
@@ -3105,16 +3067,60 @@ BBPkeepref(BAT *b)
        if (BATsetaccess(b, BAT_READ) == NULL)
                return;         /* already decreffed */
 
-       refs = decref(i, false, false, lock, __func__);
+       refs = decref(i, false, false, true, lock, __func__);
        (void) refs;
        assert(refs >= 0);
 }
 
+BAT *
+BATdescriptor(bat i)
+{
+       BAT *b = NULL;
+
+       if (BBPcheck(i)) {
+               for (;;) {
+                       MT_lock_set(&GDKswapLock(i));
+                       if (!(BBP_status(i) & (BBPUNSTABLE|BBPLOADING)))
+                               break;
+                       /* the BATs is "unstable", try again */
+                       MT_lock_unset(&GDKswapLock(i));
+                       BBPspin(i, __func__, BBPUNSTABLE|BBPLOADING);
+               }
+               if (incref(i, false, false) <= 0)
+                       return NULL;
+               b = BBP_cache(i);
+               if (b == NULL)
+                       b = getBBPdescriptor(i);
+               MT_lock_unset(&GDKswapLock(i));
+               if (b != NULL) {
+                       MT_lock_set(&b->theaplock);
+                       int tp = b->theap->parentid;
+                       int tvp = b->tvheap ? b->tvheap->parentid : 0;
+                       MT_lock_unset(&b->theaplock);
+                       if (tp != b->batCacheid &&
+                           BATdescriptor(tp) == NULL) {
+                               decref(i, false, false, false, false, __func__);
+                               return NULL;
+                       }
+                       if (tvp != 0 &&
+                           tvp != b->batCacheid &&
+                           BATdescriptor(tvp) == NULL) {
+                               if (tp != b->batCacheid)
+                                       decref(tp, false, false, true, true, 
__func__);
+
+                               decref(i, false, false, false, false, __func__);
+                               return NULL;
+                       }
+               }
+       }
+       return b;
+}
+
 static inline void
 GDKunshare(bat parent)
 {
-       (void) decref(parent, false, true, true, __func__);
-       (void) decref(parent, true, false, true, __func__);
+       (void) decref(parent, false, true, true, true, __func__);
+       (void) decref(parent, true, false, true, true, __func__);
 }
 
 void
@@ -3144,7 +3150,7 @@ BBPreclaim(BAT *b)
 
        assert(BBP_refs(i) == 1);
 
-       return decref(i, false, false, lock, __func__) <0;
+       return decref(i, false, false, true, lock, __func__) <0;
 }
 
 /*
@@ -3348,8 +3354,6 @@ BBPquickdesc(bat bid)
                }
                return NULL;
        }
-       if ((b = BBP_cache(bid)) != NULL)
-               return b;       /* already cached */
        b = BBP_desc(bid);
        if (b && b->ttype < 0) {
                const char *aname = ATOMunknown_name(b->ttype);
@@ -3896,7 +3900,7 @@ BBPsync(int cnt, bat *restrict subcommit
                        if (BBP_status(i) & BBPPERSISTENT) {
                                BAT *b = dirty_bat(&i, subcommit != NULL);
                                if (i <= 0) {
-                                       decref(-i, false, false, locked_by == 0 
|| locked_by != MT_getpid(), __func__);
+                                       decref(-i, false, false, true, 
locked_by == 0 || locked_by != MT_getpid(), __func__);
                                        break;
                                }
                                bi = bat_iterator(BBP_desc(i));
@@ -3934,7 +3938,7 @@ BBPsync(int cnt, bat *restrict subcommit
                        bat_iterator_end(&bi);
                        /* can't use BBPunfix because of the "lock"
                         * argument: locked_by may be set here */
-                       decref(i, false, false, lock, __func__);
+                       decref(i, false, false, true, lock, __func__);
                        if (n == -2)
                                break;
                        /* we once again have a saved heap */
diff --git a/gdk/gdk_cand.c b/gdk/gdk_cand.c
--- a/gdk/gdk_cand.c
+++ b/gdk/gdk_cand.c
@@ -1330,7 +1330,6 @@ BATnegcands(BUN nr, BAT *odels)
                memcpy(r, (const oid *) bi.base + lo, sizeof(oid) * (hi - lo));
        }
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to