shithub: riscv

Download patch

ref: b24ebff76a1d0ef99ecef551a78288642e6ea35b
parent: f616c63b399fed4f78121f387be9599e8e0c2b4c
author: cinap_lenrek <[email protected]>
date: Sun Mar 12 17:23:21 EDT 2017

upas/fs: remove useless syncsem, make sure mbox is locked when fetching / syncing

--- a/sys/src/cmd/upas/fs/dat.h
+++ b/sys/src/cmd/upas/fs/dat.h
@@ -149,7 +149,6 @@
 struct Mailbox {
 	QLock;
 	long	idxsem;		/* abort on concurrent index access */
-	long	syncsem;		/* abort on concurrent syncs */
 	int	refs;
 	Mailbox	*next;
 	int	id;
--- a/sys/src/cmd/upas/fs/fs.c
+++ b/sys/src/cmd/upas/fs/fs.c
@@ -1244,6 +1244,7 @@
 		return nil;
 	}
 
+	qlock(f->mb);
 	switch(t){
 	case Qctl:
 		rhdr.count = 0;
@@ -1251,7 +1252,6 @@
 	case Qmboxctl:
 		i = mboxctlread(f->mb, &p);
 		goto output;
-		break;
 	case Qheader:
 		cacheheaders(f->mb, f->m);
 		rhdr.count = readheader(f->m, (char*)mbuf, off, cnt);
@@ -1287,6 +1287,7 @@
 		}
 		break;
 	}
+	qunlock(f->mb);
 	return nil;
 }
 
@@ -1410,7 +1411,10 @@
 			argc = tokenize(thdr.data, argv, nelem(argvbuf));
 			if(argc == 0)
 				return Ebadctl;
-			return f->mb->ctl(f->mb, argc, argv);
+			qlock(f->mb);
+			err = f->mb->ctl(f->mb, argc, argv);
+			qunlock(f->mb);
+			return err;
 		}
 		break;
 	case Qflags:
@@ -1419,11 +1423,10 @@
 		 */
 		if(!f->mb || !f->m)
 			break;
+		qlock(f->mb);
 		m = gettopmsg(f->mb, f->m);
 		err = modflags(f->mb, m, thdr.data);
-//		premature optimization?  flags not written immediately.
-//		if(err == nil && f->m->cstate&Cidxstale)
-//			wridxfile(f->mb);		/* syncmbox(f->mb, 1); */
+		qunlock(f->mb);
 		return err;
 	}
 	return Eperm;
@@ -1607,28 +1610,24 @@
 		t = time(0);
 		qlock(&mbllock);
 		for(mb = mbl; mb != nil; mb = mb->next){
-			assert(mb->refs > 0);
+			if(!canqlock(mb))
+				continue;
 			if(mb->waketime != 0 && t >= mb->waketime){
-				qlock(mb);
 				mb->waketime = 0;
 				break;
 			}
-
-			if(mb->d == nil || mb->d->name == nil)
-				continue;
-			d = dirstat(mb->path);
-			if(d == nil)
-				continue;
-
-			qlock(mb);
-			if(mb->d)
-			if(d->qid.path != mb->d->qid.path
-			   || d->qid.vers != mb->d->qid.vers){
-				free(d);
-				break;
+			if(mb->d != nil && mb->d->name != nil){
+				d = dirstat(mb->path);
+				if(d != nil){
+					if(d->qid.path != mb->d->qid.path
+					|| d->qid.vers != mb->d->qid.vers){
+						free(d);
+						break;
+					}
+					free(d);
+				}
 			}
 			qunlock(mb);
-			free(d);
 		}
 		qunlock(&mbllock);
 		if(mb != nil){
--- a/sys/src/cmd/upas/fs/mbox.c
+++ b/sys/src/cmd/upas/fs/mbox.c
@@ -66,15 +66,12 @@
 	int n, d, y, a;
 	Message *m, *next;
 
-	if(semacquire(&mb->syncsem, 0) <= 0)
-		return nil;
+	assert(canqlock(mb) == 0);
 	a = mb->root->subname;
 	if(rdidxfile(mb, doplumb) == -2)
 		wridxfile(mb);
-	if(s = mb->sync(mb, doplumb, &n)){
-		semrelease(&mb->syncsem, 1);
+	if(s = mb->sync(mb, doplumb, &n))
 		return s;
-	}
 	d = 0;
 	y = 0;
 	for(m = mb->root->part; m; m = next){
@@ -102,7 +99,6 @@
 		henter(PATH(0, Qtop), mb->name,
 			(Qid){PATH(mb->id, Qmbox), mb->vers, QTDIR}, nil, mb);
 	}
-	semrelease(&mb->syncsem, 1);
 	return nil;
 }
 
@@ -182,7 +178,6 @@
 	initheaders();
 	mb = emalloc(sizeof *mb);
 	mb->idxsem = 1;
-	mb->syncsem = 1;
 	mb->flags = flags;
 	strncpy(mb->path, path, sizeof mb->path - 1);
 	p = name;
@@ -1184,8 +1179,8 @@
 void
 mboxdecref(Mailbox *mb)
 {
-	assert(mb->refs > 0);
 	qlock(mb);
+	assert(mb->refs > 0);
 	mb->refs--;
 	if(mb->refs == 0){
 		syncmbox(mb, 1);