Changeset: 6994f4662982 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=6994f4662982
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk.h
        gdk/gdk_bbp.c
        gdk/gdk_bbp.h
        gdk/gdk_tm.c
        monetdb5/mal/mal_debugger.c
        monetdb5/mal/mal_profiler.c
        monetdb5/modules/kernel/status.c
        monetdb5/modules/mal/bbp.c
        monetdb5/modules/mal/transaction.c
Branch: default
Log Message:

Avoid data races when accessing BBPsize.


diffs (truncated from 665 to 300 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -234,7 +234,6 @@ void BBPreleaseref(bat i);
 int BBPrename(bat bid, const char *nme);
 int BBPsave(BAT *b);
 void BBPshare(bat b);
-bat BBPsize;
 int BBPsync(int cnt, bat *subcommit);
 void BBPunlock(const char *s);
 BAT *BUNappend(BAT *b, const void *right, bit force);
@@ -412,6 +411,7 @@ int escapedStrlen(const char *src, const
 int fltFromStr(const char *src, int *len, flt **dst);
 int fltToStr(str *dst, int *len, const flt *src);
 const flt flt_nil;
+bat getBBPsize(void);
 char *get_bin_path(void);
 int gettimeofday(struct timeval *tv, int *ignore_zone);
 int gprof_pthread_create(pthread_t *__restrict, __const pthread_attr_t 
*__restrict, void *( *fcn)(void *), void *__restrict);
diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -2630,7 +2630,7 @@ BBPcheck(register bat x, register const 
        if (x && x != bat_nil) {
                register bat z = ABS(x);
 
-               if (z >= BBPsize || BBP_logical(z) == NULL) {
+               if (z >= getBBPsize() || BBP_logical(z) == NULL) {
                        CHECKDEBUG THRprintf(GDKstdout,"#%s: range error %d\n", 
y, (int) x);
                } else {
                        return z;
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -98,7 +98,10 @@
  */
 BBPrec *BBP[N_BBPINIT];                /* fixed base VM address of BBP array */
 bat BBPlimit = 0;              /* current committed VM BBP array */
-bat BBPsize = 0;               /* current used size of BBP array */
+#ifdef ATOMIC_LOCK
+static MT_Lock BBPsizeLock MT_LOCK_INITIALIZER("BBPsizeLock");
+#endif
+static volatile ATOMIC_TYPE BBPsize = 0; /* current used size of BBP array */
 
 #define KITTENNAP 4    /* used to suspend processing */
 #define BBPNONAME "."          /* filler for no name in BBP.dir */
@@ -161,6 +164,13 @@ BBP_delete(bat i)
        }
 }
 
+bat
+getBBPsize(void)
+{
+       return (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "getBBPsize");
+}
+
+
 /*
  * other globals
  */
@@ -298,7 +308,7 @@ BBPunlock(const char *nme)
 static void
 BBPinithash(int j)
 {
-       bat i = BBPsize;
+       bat i = (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPinithash");
 
        assert(j >= 0 && j <= BBP_THREADMASK);
        for (BBP_mask = 1; (BBP_mask << 1) <= BBPlimit; BBP_mask <<= 1)
@@ -339,12 +349,12 @@ BBPextend(int idx, int buildhash)
 {
        BBP_notrim = MT_getpid();
 
-       if (BBPsize >= N_BBPINIT * BBPINIT)
+       if ((bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPextend") >= N_BBPINIT * 
BBPINIT)
                GDKfatal("BBPextend: trying to extend BAT pool beyond the "
                         "limit (%d)\n", N_BBPINIT * BBPINIT);
 
        /* make sure the new size is at least BBPsize large */
-       while (BBPlimit < BBPsize) {
+       while (BBPlimit < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPextend")) {
                assert(BBP[BBPlimit >> BBPINITLOG] == NULL);
                BBP[BBPlimit >> BBPINITLOG] = GDKzalloc(BBPINIT * 
sizeof(BBPrec));
                if (BBP[BBPlimit >> BBPINITLOG] == NULL)
@@ -606,7 +616,7 @@ fixoidheap(void)
                "# upgrading database from 32 bit OIDs to 64 bit OIDs\n");
        fflush(stderr);
 
-       for (bid = 1; bid < BBPsize; bid++) {
+       for (bid = 1; bid < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, 
"fixoidheap"); bid++) {
                if ((bs = BBP_desc(bid)) == NULL)
                        continue;       /* not a valid BAT */
                if (BBP_logical(bid) &&
@@ -840,9 +850,9 @@ BBPreadEntries(FILE *fp, int *min_stamp,
 #endif
 
                bid = (bat) batid;
-               if ((bat) batid >= BBPsize) {
-                       BBPsize = (bat) batid + 1;
-                       if (BBPsize >= BBPlimit)
+               if ((bat) batid >= (bat) ATOMIC_GET(BBPsize, BBPsizeLock, 
"BBPreadEntries")) {
+                       ATOMIC_SET(BBPsize, batid + 1, BBPsizeLock, 
"BBPreadEntries");
+                       if ((bat) ATOMIC_GET(BBPsize, BBPsizeLock, 
"BBPreadEntries") >= BBPlimit)
                                BBPextend(0, FALSE);
                }
                if (BBP_desc(bid) != NULL)
@@ -956,8 +966,8 @@ BBPheader(FILE *fp, oid *BBPoid, int *OI
        if ((s = strstr(buf, "BBPsize")) != NULL) {
                sscanf(s, "BBPsize=%d", &sz);
                sz = (int) (sz * BATMARGIN);
-               if (sz > BBPsize)
-                       BBPsize = sz;
+               if (sz > (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPheader"))
+                       ATOMIC_SET(BBPsize, sz, BBPsizeLock, "BBPheader");
        }
        return bbpversion;
 }
@@ -977,6 +987,7 @@ BBPinit(void)
 #ifdef NEED_MT_LOCK_INIT
        MT_lock_init(&GDKunloadLock, "GDKunloadLock");
        ATOMIC_INIT(stampLock, "stampLock");
+       ATOMIC_INIT(BBPsizeLock, "BBPsizeLock");
 #endif
 
        /* first move everything from SUBDIR to BAKDIR (its parent) */
@@ -1014,19 +1025,19 @@ BBPinit(void)
        /* scan the BBP.dir to obtain current size */
        BBPlimit = 0;
        memset(BBP, 0, sizeof(BBP));
-       BBPsize = 1;
+       ATOMIC_SET(BBPsize, 1, BBPsizeLock, "BBPinit");
 
        bbpversion = BBPheader(fp, &BBPoid, &oidsize);
 
        BBPextend(0, FALSE);            /* allocate BBP records */
-       BBPsize = 1;
+       ATOMIC_SET(BBPsize, 1, BBPsizeLock, "BBPinit");
 
        BBPreadEntries(fp, &min_stamp, &max_stamp, oidsize, bbpversion);
        fclose(fp);
 
        /* normalize saved LRU stamps */
        if (min_stamp <= max_stamp) {
-               for (bid = 1; bid < BBPsize; bid++)
+               for (bid = 1; bid < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, 
"BBPinit"); bid++)
                        if (BBPvalid(bid))
                                BBP_lastused(bid) -= min_stamp;
                BBPsetstamp(max_stamp - min_stamp);
@@ -1079,7 +1090,7 @@ BBPexit(void)
        /* free all memory (just for leak-checking in Purify) */
        do {
                skipped = 0;
-               for (i = 0; i < BBPsize; i++) {
+               for (i = 0; i < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, 
"BBPexit"); i++) {
                        if (BBPvalid(i)) {
                                BAT *b = BBP_cache(i);
 
@@ -1150,7 +1161,7 @@ new_bbpentry(stream *s, bat i)
        int t;
 
        assert(i > 0);
-       assert(i < BBPsize);
+       assert(i < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "new_bbpentry"));
        assert(BBP_desc(i));
        assert(BBP_desc(i)->B.batCacheid == i);
 
@@ -1255,7 +1266,7 @@ BBPdir_subcommit(int cnt, bat *subcommit
                goto bailout;
        fp = NULL;
 
-       n = BBPsize;
+       n = (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPdir_subcommit");
 
        /* we need to copy the backup BBP.dir to the new, but
         * replacing the entries for the subcommitted bats */
@@ -1273,8 +1284,8 @@ BBPdir_subcommit(int cnt, bat *subcommit
        /* third line contains BBPsize */
        if ((p = strstr(buf, "BBPsize")) != NULL)
                sscanf(p, "BBPsize=%d", &n);
-       if (n < BBPsize)
-               n = BBPsize;
+       if (n < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPdir_subcommit"))
+               n = (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPdir_subcommit");
 
        if (GDKdebug & (IOMASK | THRDMASK))
                THRprintf(GDKstdout, "#BBPdir: writing BBP.dir (%d bats).\n", 
n);
@@ -1355,7 +1366,7 @@ BBPdir(int cnt, bat *subcommit)
                return BBPdir_subcommit(cnt, subcommit);
 
        if (GDKdebug & (IOMASK | THRDMASK))
-               THRprintf(GDKstdout, "#BBPdir: writing BBP.dir (%d bats).\n", 
(int) BBPsize);
+               THRprintf(GDKstdout, "#BBPdir: writing BBP.dir (%d bats).\n", 
(int) (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPdir"));
        IODEBUG {
                THRprintf(GDKstdout, "#BBPdir start oid=");
                OIDwrite(GDKstdout);
@@ -1368,10 +1379,10 @@ BBPdir(int cnt, bat *subcommit)
                goto bailout;
        }
 
-       if (BBPdir_header(s, BBPsize) < 0)
+       if (BBPdir_header(s, (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPdir")) 
< 0)
                goto bailout;
 
-       for (i = 1; i < BBPsize; i++) {
+       for (i = 1; i < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPdir"); i++) {
                /* write the entry
                 * BBP.dir consists of all persistent bats */
                if (BBP_status(i) & BBPPERSISTENT) {
@@ -1390,7 +1401,7 @@ BBPdir(int cnt, bat *subcommit)
 
        IODEBUG THRprintf(GDKstdout, "#BBPdir end\n");
 
-       if (i < BBPsize)
+       if (i < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPdir"))
                goto bailout;
 
        return 0;
@@ -1413,7 +1424,7 @@ BBPdump(void)
        size_t cmem = 0, cvm = 0;
        int n = 0, nc = 0;
 
-       for (i = 0; i < BBPsize; i++) {
+       for (i = 0; i < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPdump"); i++) 
{
                BAT *b = BBP_cache(i);
                if (b == NULL)
                        continue;
@@ -1524,7 +1535,7 @@ BBP_find(const char *nme, int lock)
                /* for tmp_X and tmpr_X BATs, we already know X */
                const char *s;
 
-               if (ABS(i) >= BBPsize || (s = BBP_logical(i)) == NULL || 
strcmp(s, nme)) {
+               if (ABS(i) >= (bat) ATOMIC_GET(BBPsize, BBPsizeLock, 
"BBP_find") || (s = BBP_logical(i)) == NULL || strcmp(s, nme)) {
                        i = 0;
                }
        } else if (*nme != '.') {
@@ -1552,7 +1563,7 @@ BBPgetdesc(bat i)
 {
        if (i < 0)
                i = -i;
-       if (i != bat_nil && i < BBPsize && i && BBP_logical(i)) {
+       if (i != bat_nil && i < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, 
"BBPgetdesc") && i && BBP_logical(i)) {
                return BBP_desc(i);
        }
        return NULL;
@@ -1661,10 +1672,10 @@ BBPinsert(BATstore *bs)
                /* check again in case some other thread extended
                 * while we were waiting */
                if (BBP_free(idx) <= 0) {
-                       if (BBPsize++ >= BBPlimit) {
+                       if ((bat) ATOMIC_ADD(BBPsize, 1, BBPsizeLock, 
"BBPinsert") >= BBPlimit) {
                                BBPextend(idx, TRUE);
                        } else {
-                               BBP_free(idx) = BBPsize - 1;
+                               BBP_free(idx) = (bat) ATOMIC_GET(BBPsize, 
BBPsizeLock, "BBPinsert") - 1;
                        }
                }
                MT_lock_unset(&GDKnameLock, "BBPinsert");
@@ -2592,7 +2603,7 @@ BBPtrim_scan(bat bbppos, bat bbplim)
        bbptrimmax = BBPMAXTRIM;
        MEMDEBUG THRprintf(GDKstdout, "#TRIMSCAN: start=%d, limit=%d\n", (int) 
bbppos, (int) bbplim);
 
-       if (bbppos < BBPsize)
+       if (bbppos < (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPtrim_scan"))
                do {
                        if (BBPvalid(bbppos)) {
                                BAT *b = BBP_cache(bbppos);
@@ -2626,7 +2637,7 @@ BBPtrim_scan(bat bbppos, bat bbplim)
                                                break;
                                }
                        }
-                       if (++bbppos == BBPsize)
+                       if (++bbppos == (bat) ATOMIC_GET(BBPsize, BBPsizeLock, 
"BBPtrim_scan"))
                                bbppos = 1;     /* treat BBP as a circular 
buffer */
                } while (bbppos != bbplim);
 
@@ -2643,7 +2654,7 @@ BBPtrim_scan(bat bbppos, bat bbplim)
        } else {
                bbptrimfirst = BBPMAXTRIM;
        }
-       MEMDEBUG THRprintf(GDKstdout, "#TRIMSCAN: end at %d (size=%d)\n", 
bbppos, (int) BBPsize);
+       MEMDEBUG THRprintf(GDKstdout, "#TRIMSCAN: end at %d (size=%d)\n", 
bbppos, (int) (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPtrim_scan"));
 
        return bbppos;
 }
@@ -2819,7 +2830,7 @@ BBPtrim(size_t target)
        PERFDEBUG THRprintf(GDKstdout, "#BBPtrim(mem=%d)\n", target > 0);
 
        scan = (bbptrimfirst == BBPMAXTRIM);
-       if (bbpscanstart >= BBPsize)
+       if (bbpscanstart >= (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPtrim"))
                bbpscanstart = 1;       /* sometimes, the BBP shrinks! */
        limit = bbpscanstart;
 
@@ -3345,7 +3356,7 @@ BBPsync(int cnt, bat *subcommit)
                }
        }
 
-       PERFDEBUG THRprintf(GDKstdout, "#BBPsync (dir time %d) %d bats\n", (t1 
= GDKms()) - t0, BBPsize);
+       PERFDEBUG THRprintf(GDKstdout, "#BBPsync (dir time %d) %d bats\n", (t1 
= GDKms()) - t0, (bat) ATOMIC_GET(BBPsize, BBPsizeLock, "BBPsync"));
 
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to