[prev in list] [next in list] [prev in thread] [next in thread] 

List:       monetdb-checkins
Subject:    MonetDB: default - Split batIdxLock into old batIdxLock and r/w ...
From:       Sjoerd_Mullender <commits+sjoerd=acm.org () monetdb ! org>
Date:       2021-03-31 14:25:35
Message-ID: hg.4753e191f5a7.1617200735.113030046782991390 () monetdb-vm0 ! spin-off ! cwi ! nl
[Download RAW message or body]

Changeset: 4753e191f5a7 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/4753e191f5a7
Modified Files:
	gdk/gdk.h
	gdk/gdk_align.c
	gdk/gdk_bat.c
	gdk/gdk_batop.c
	gdk/gdk_bbp.c
	gdk/gdk_group.c
	gdk/gdk_hash.c
	gdk/gdk_hash.h
	gdk/gdk_imprints.c
	gdk/gdk_join.c
	gdk/gdk_logger.c
	gdk/gdk_logger_old.c
	gdk/gdk_orderidx.c
	gdk/gdk_private.h
	gdk/gdk_select.c
	gdk/gdk_unique.c
	monetdb5/mal/mal_authorize.c
	monetdb5/modules/mal/tokenizer.c
Branch: default
Log Message:

Split batIdxLock into old batIdxLock and r/w thashlock, the latter for hashes.


diffs (truncated from 992 to 300 lines):

diff --git a/gdk/gdk.h b/gdk/gdk.h
--- a/gdk/gdk.h
+++ b/gdk/gdk.h
@@ -771,8 +771,8 @@ typedef struct BAT {
 	/* dynamic column properties */
 	COLrec T;		/* column info */
 	MT_Lock theaplock;	/* lock protecting heap reference changes */
-
-	MT_RWLock batIdxLock;	/* lock to manipulate indexes */
+	MT_RWLock thashlock;	/* lock specifically for hash management */
+	MT_Lock batIdxLock;	/* lock to manipulate other indexes/properties */
 } BAT;
 
 typedef struct BATiter {
diff --git a/gdk/gdk_align.c b/gdk/gdk_align.c
--- a/gdk/gdk_align.c
+++ b/gdk/gdk_align.c
@@ -142,7 +142,8 @@ VIEWcreate(oid seq, BAT *b)
 		}
 		HEAPdecref(bn->theap, false);
 		MT_lock_destroy(&bn->theaplock);
-		MT_rwlock_destroy(&bn->batIdxLock);
+		MT_lock_destroy(&bn->batIdxLock);
+		MT_rwlock_destroy(&bn->thashlock);
 		GDKfree(bn);
 		return NULL;
 	}
diff --git a/gdk/gdk_bat.c b/gdk/gdk_bat.c
--- a/gdk/gdk_bat.c
+++ b/gdk/gdk_bat.c
@@ -142,7 +142,9 @@ BATcreatedesc(oid hseq, int tt, bool hea
 	snprintf(name, sizeof(name), "heaplock%d", bn->batCacheid); /* fits */
 	MT_lock_init(&bn->theaplock, name);
 	snprintf(name, sizeof(name), "BATlock%d", bn->batCacheid); /* fits */
-	MT_rwlock_init(&bn->batIdxLock, name);
+	MT_lock_init(&bn->batIdxLock, name);
+	snprintf(name, sizeof(name), "hashlock%d", bn->batCacheid); /* fits */
+	MT_rwlock_init(&bn->thashlock, name);
 	bn->batDirtydesc = true;
 	return bn;
       bailout:
@@ -247,7 +249,8 @@ COLnew(oid hseq, int tt, BUN cap, role_t
 	if (bn->tvheap)
 		HEAPdecref(bn->tvheap, true);
 	MT_lock_destroy(&bn->theaplock);
-	MT_rwlock_destroy(&bn->batIdxLock);
+	MT_lock_destroy(&bn->batIdxLock);
+	MT_rwlock_destroy(&bn->thashlock);
 	GDKfree(bn);
 	return NULL;
 }
@@ -635,7 +638,8 @@ BATdestroy(BAT *b)
 	GDKfree(b->tvheap);
 	PROPdestroy(b);
 	MT_lock_destroy(&b->theaplock);
-	MT_rwlock_destroy(&b->batIdxLock);
+	MT_lock_destroy(&b->batIdxLock);
+	MT_rwlock_destroy(&b->thashlock);
 	GDKfree(b->theap);
 	GDKfree(b);
 }
diff --git a/gdk/gdk_batop.c b/gdk/gdk_batop.c
--- a/gdk/gdk_batop.c
+++ b/gdk/gdk_batop.c
@@ -2204,7 +2204,7 @@ BATsort(BAT **sorted, BAT **order, BAT *
 	mkorderidx = (g == NULL && !reverse && !nilslast && pb != NULL && (order || !pb->batTransient));
 	if (g == NULL && !reverse && !nilslast &&
 	    pb != NULL && !BATcheckorderidx(pb)) {
-		MT_rwlock_wrlock(&pb->batIdxLock);
+		MT_lock_set(&pb->batIdxLock);
 		if (pb->torderidx == NULL) {
 			/* no index created while waiting for lock */
 			if (mkorderidx) /* keep lock when going to create */
@@ -2214,7 +2214,7 @@ BATsort(BAT **sorted, BAT **order, BAT *
 			mkorderidx = false;
 		}
 		if (!orderidxlock)
-			MT_rwlock_wrunlock(&pb->batIdxLock);
+			MT_lock_unset(&pb->batIdxLock);
 	} else {
 		mkorderidx = false;
 	}
@@ -2441,7 +2441,7 @@ BATsort(BAT **sorted, BAT **order, BAT *
 				GDKfree(m);
 			}
 			if (orderidxlock)
-				MT_rwlock_wrunlock(&pb->batIdxLock);
+				MT_lock_unset(&pb->batIdxLock);
 			goto error;
 		}
 		bn->tsorted = !reverse && !nilslast;
@@ -2464,7 +2464,7 @@ BATsort(BAT **sorted, BAT **order, BAT *
 		}
 	}
 	if (orderidxlock)
-		MT_rwlock_wrunlock(&pb->batIdxLock);
+		MT_lock_unset(&pb->batIdxLock);
 	bn->theap->dirty = true;
 	bn->tnosorted = 0;
 	bn->tnorevsorted = 0;
@@ -2719,11 +2719,22 @@ BATsetprop_nolock(BAT *b, enum prop_t id
 }
 
 PROPrec *
+BATgetprop_try(BAT *b, enum prop_t idx)
+{
+	PROPrec *p = NULL;
+	if (MT_lock_try(&b->batIdxLock)) {
+		p = BATgetprop_nolock(b, idx);
+		MT_lock_unset(&b->batIdxLock);
+	}
+	return p;
+}
+
+PROPrec *
 BATgetprop(BAT *b, enum prop_t idx)
 {
 	PROPrec *p;
 
-	MT_rwlock_wrlock(&b->batIdxLock);
+	MT_lock_set(&b->batIdxLock);
 	p = BATgetprop_nolock(b, idx);
 	if (p == NULL) {
 		/* if looking for the min/max value, we may be able to
@@ -2747,7 +2758,7 @@ BATgetprop(BAT *b, enum prop_t idx)
 			break;
 		}
 	}
-	MT_rwlock_wrunlock(&b->batIdxLock);
+	MT_lock_unset(&b->batIdxLock);
 	return p;
 }
 
@@ -2755,18 +2766,18 @@ PROPrec *
 BATsetprop(BAT *b, enum prop_t idx, int type, const void *v)
 {
 	PROPrec *p;
-	MT_rwlock_wrlock(&b->batIdxLock);
+	MT_lock_set(&b->batIdxLock);
 	p = BATsetprop_nolock(b, idx, type, v);
-	MT_rwlock_wrunlock(&b->batIdxLock);
+	MT_lock_unset(&b->batIdxLock);
 	return p;
 }
 
 void
 BATrmprop(BAT *b, enum prop_t idx)
 {
-	MT_rwlock_wrlock(&b->batIdxLock);
+	MT_lock_set(&b->batIdxLock);
 	BATrmprop_nolock(b, idx);
-	MT_rwlock_wrunlock(&b->batIdxLock);
+	MT_lock_unset(&b->batIdxLock);
 }
 
 
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -647,7 +647,9 @@ BBPreadEntries(FILE *fp, unsigned bbpver
 		snprintf(name, sizeof(name), "heaplock%d", bn->batCacheid); /* fits */
 		MT_lock_init(&bn->theaplock, name);
 		snprintf(name, sizeof(name), "BATlock%d", bn->batCacheid); /* fits */
-		MT_rwlock_init(&bn->batIdxLock, name);
+		MT_lock_init(&bn->batIdxLock, name);
+		snprintf(name, sizeof(name), "hashlock%d", bn->batCacheid); /* fits */
+		MT_rwlock_init(&bn->thashlock, name);
 		ATOMIC_INIT(&bn->theap->refs, 1);
 
 		if (base > (uint64_t) GDK_oid_max) {
diff --git a/gdk/gdk_group.c b/gdk/gdk_group.c
--- a/gdk/gdk_group.c
+++ b/gdk/gdk_group.c
@@ -768,14 +768,14 @@ BATgroup_internal(BAT **groups, BAT **ex
 	if (gn == NULL)
 		goto error;
 	ngrps = (oid *) Tloc(gn, 0);
-	MT_rwlock_rdlock(&b->batIdxLock);
+	MT_rwlock_rdlock(&b->thashlock);
 	if (b->thash && b->thash != (Hash *) 1)
 		maxgrps = b->thash->nunique;
 	else if ((prop = BATgetprop_nolock(b, GDK_NUNIQUE)) != NULL)
 		maxgrps = prop->v.val.oval;
 	else
 		maxgrps = cnt / 10;
-	MT_rwlock_rdunlock(&b->batIdxLock);
+	MT_rwlock_rdunlock(&b->thashlock);
 	if (!is_oid_nil(maxgrp) && maxgrps < maxgrp)
 		maxgrps += maxgrp;
 	if (e && maxgrps < BATcount(e))
diff --git a/gdk/gdk_hash.c b/gdk/gdk_hash.c
--- a/gdk/gdk_hash.c
+++ b/gdk/gdk_hash.c
@@ -427,7 +427,7 @@ BATcheckhash(BAT *b)
 	if (b->thash == (Hash *) 1) {
 		/* but when we want to change it, we need the lock */
 		TRC_DEBUG_IF(ACCELERATOR) t = GDKusec();
-		MT_rwlock_wrlock(&b->batIdxLock);
+		MT_rwlock_wrlock(&b->thashlock);
 		TRC_DEBUG_IF(ACCELERATOR) t = GDKusec() - t;
 		/* if still 1 now that we have the lock, we can update */
 		if (b->thash == (Hash *) 1) {
@@ -524,7 +524,7 @@ BATcheckhash(BAT *b)
 								b->thash = h;
 								TRC_DEBUG(ACCELERATOR,
 									  ALGOBATFMT ": reusing persisted hash\n", ALGOBATPAR(b));
-								MT_rwlock_wrunlock(&b->batIdxLock);
+								MT_rwlock_wrunlock(&b->thashlock);
 								return true;
 							}
 							/* if h->nil==h->nbucket
@@ -556,7 +556,7 @@ BATcheckhash(BAT *b)
 			GDKfree(h);
 			GDKclrerr();	/* we're not currently interested in errors */
 		}
-		MT_rwlock_wrunlock(&b->batIdxLock);
+		MT_rwlock_wrunlock(&b->thashlock);
 	}
 	ret = b->thash != NULL;
 	if (ret)
@@ -643,9 +643,9 @@ BAThashsync(void *arg)
 	/* we could check whether b->thash == NULL before getting the
 	 * lock, and only lock if it isn't; however, it's very
 	 * unlikely that that is the case, so we don't */
-	MT_rwlock_rdlock(&b->batIdxLock);
+	MT_rwlock_rdlock(&b->thashlock);
 	BAThashsave(b, true);
-	MT_rwlock_rdunlock(&b->batIdxLock);
+	MT_rwlock_rdunlock(&b->thashlock);
 	BBPunfix(b->batCacheid);
 }
 #endif
@@ -797,10 +797,10 @@ BAThash_impl(BAT *restrict b, struct can
 		/* if key, or if small, don't bother dynamically
 		 * adjusting the hash mask */
 		mask = HASHmask(ci->ncand);
- 	} else if (!hascand && (prop = BATgetprop_nolock(b, GDK_NUNIQUE)) != NULL) {
+ 	} else if (!hascand && (prop = BATgetprop_try(b, GDK_NUNIQUE)) != NULL) {
 		assert(prop->v.vtype == TYPE_oid);
 		mask = prop->v.val.oval * 8 / 7;
- 	} else if (!hascand && (prop = BATgetprop_nolock(b, GDK_HASH_BUCKETS)) != NULL) {
+ 	} else if (!hascand && (prop = BATgetprop_try(b, GDK_HASH_BUCKETS)) != NULL) {
 		assert(prop->v.vtype == TYPE_oid);
 		mask = prop->v.val.oval;
 		maxmask = HASHmask(ci->ncand);
@@ -990,12 +990,12 @@ BAThash(BAT *b)
 	if (BATcheckhash(b)) {
 		return GDK_SUCCEED;
 	}
-	MT_rwlock_wrlock(&b->batIdxLock);
+	MT_rwlock_wrlock(&b->thashlock);
 	if (b->thash == NULL) {
 		struct canditer ci;
 		canditer_init(&ci, b, NULL);
 		if ((b->thash = BAThash_impl(b, &ci, "thash")) == NULL) {
-			MT_rwlock_wrunlock(&b->batIdxLock);
+			MT_rwlock_wrunlock(&b->thashlock);
 			return GDK_FAIL;
 		}
 #ifdef PERSISTENTHASH
@@ -1004,7 +1004,7 @@ BAThash(BAT *b)
 			BBPfix(b->batCacheid);
 			char name[MT_NAME_LEN];
 			snprintf(name, sizeof(name), "hashsync%d", b->batCacheid);
-			MT_rwlock_wrunlock(&b->batIdxLock);
+			MT_rwlock_wrunlock(&b->thashlock);
 			if (MT_create_thread(&tid, BAThashsync, b,
 					     MT_THR_DETACHED,
 					     name) < 0) {
@@ -1017,7 +1017,7 @@ BAThash(BAT *b)
 					"NOT persisting hash %d\n", b->batCacheid);
 #endif
 	}
-	MT_rwlock_wrunlock(&b->batIdxLock);
+	MT_rwlock_wrunlock(&b->thashlock);
 	return GDK_SUCCEED;
 }
 
@@ -1103,9 +1103,9 @@ HASHappend_locked(BAT *b, BUN i, const v
 void
 HASHappend(BAT *b, BUN i, const void *v)
 {
-	MT_rwlock_wrlock(&b->batIdxLock);
+	MT_rwlock_wrlock(&b->thashlock);
 	HASHappend_locked(b, i, v);
-	MT_rwlock_wrunlock(&b->batIdxLock);
+	MT_rwlock_wrunlock(&b->thashlock);
 }
 
 /* insert value v at position p into the hash table of b */
@@ -1174,9 +1174,9 @@ HASHinsert_locked(BAT *b, BUN p, const v
 void
 HASHinsert(BAT *b, BUN p, const void *v)
 {
-	MT_rwlock_wrlock(&b->batIdxLock);
+	MT_rwlock_wrlock(&b->thashlock);
 	HASHinsert_locked(b, p, v);
-	MT_rwlock_wrunlock(&b->batIdxLock);
+	MT_rwlock_wrunlock(&b->thashlock);
 }
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic