code: 9ferno

Download patch

ref: e80a611c3978315ffbec1fabad9fd742696c7609
parent: 064b0e15d602a0f773099156ccf09a8aa7db5c3e
author: 9ferno <gophone2015@gmail.com>
date: Sun Jan 16 12:50:33 EST 2022

compiling devready

--- a/os/port/devready.c
+++ b/os/port/devready.c
@@ -9,17 +9,11 @@
 
 /*
 	Provides #r to notify when the pipes have data to be read
-	#r/watch	watch 0 3 4 # to watch for data on fd[0], fd[3] and fd[4]
-	#r/ready	reads the fd ready to be read
+	#r/canread	watch 0 3 4 # to watch for data on fd[0], fd[3] and fd[4]
 
-	TODO why not add this to devproc? Too much complexity in devproc?
+	why not add this to devproc? Too much complexity in devproc?
 		It belongs there..
 		instead of 2 files (watch and ready). Use 1 file (canread).
-
-	#r/canread
-
-up->shm = Ready*
-c->aux = 
  */
 enum
 {
@@ -26,54 +20,38 @@
 	NFD = 16,
 
 	Qcanread = 0,
-	Qready
 };
 
-/*
-struct Qid
-{
-	u64	path; Qctl or Qready
-	u32	vers; for version control
-	uchar	type; QTFILE | QTDIR;
-} Qid;
-also, keeping Chan.aux = for QTFILE, Svalue*. For QTDIR, Sgrp (up->shm).
- */
-
-Why do I even need locks? It does not work across processes anyway
-typedef struct Readyfd;
+typedef struct Readyfd Readyfd;
 struct Readyfd
 {
-	Qlock;
 	s32 fd;
 	Queue *q;
 	s32 ready;
-	u8 stop;
 	Proc *kproc;
+	Queue *commq;	/* same as Canread.commq */
+	u8 stop;
 };
 
-typedef struct Ready;
-struct Ready
+typedef struct Canread Canread;
+struct Canread
 {
-	Qlock;
-	Readyfd *rfd[[NFD];
+	Readyfd rfd[NFD];
 	s32 nrfd;
 	Queue *commq;	/* queue for the different watcher kproc's to communicate */
 };
 
 static int
-rgen(Chan *c, char *name, Dirtab*, int, int s, Dir *dp)
+crgen(Chan *c, char *name, Dirtab*, int, int s, Dir *dp)
 {
-	Sgrp *g;
-	Svalue *v;
 	Qid q;
-	s32 i;
 
 	if(s == DEVDOTDOT){
-		devdir(c, c->qid, "#s", 0, eve, 0775, dp);
+		devdir(c, c->qid, "#R", 0, eve, 0775, dp);
 		return 1;
 	}
 
-	if(c->qid == Qcanread){
+	if(s == 0){
 		mkqid(&q, Qcanread, 0, QTFILE);
 		devdir(c, q, "canread", 0, eve, 0664, dp);
 		return 1;
@@ -82,68 +60,49 @@
 }
 
 static Chan*
-rattach(char *spec)
+crattach(char *spec)
 {
 	Chan *c;
-	Sgrp *g;
 
-	c = devattach('r', spec);
+	if(up->canread == nil)
+		up->canread = malloc(sizeof(Canread));
+
+	if(up->canread == nil)
+		error(Enomem);
+
+	c = devattach('R', spec);
 	mkqid(&c->qid, 0, 0, QTDIR);
 	return c;
 }
 
 static Walkqid*
-rwalk(Chan *c, Chan *nc, char **name, int nname)
+crwalk(Chan *c, Chan *nc, char **name, int nname)
 {
-	Walkqid *wq;
-
-	qlock(up->readyfds);
-	wq = devwalk(c, nc, name, nname, 0, 0, shmgen);
-	if(wq != nil && wq->clone != nil && wq->clone != c){
-		wq->clone->aux = up->readyfds;
-		DBG("shmwalk wq->clone->path %s mode 0x%ux\n"
-			"	wq->clone->qid.path 0x%zux wq->clone->qid.vers %d\n"
-			"	wq->clone->qid.type %d 0x%ux\n"
-			"	wq->clone->aux 0x%zx\n",
-			chanpath(nc), wq->clone->mode,
-			wq->clone->qid.path, wq->clone->qid.vers,
-			wq->clone->qid.type, wq->clone->qid.type,
-			wq->clone->aux);
-	}
-	qunlock(up->readyfds);
-	return wq;
+	return devwalk(c, nc, name, nname, 0, 0, crgen);
 }
 
 static int
-rstat(Chan *c, uchar *db, int n)
+crstat(Chan *c, uchar *db, int n)
 {
-	qlock(up->readyfds);
-	s = devstat(c, db, n, 0, 0, shmgen);
-	qunlock(up->readyfds);
-	return s;
+	return devstat(c, db, n, 0, 0, crgen);
 }
 
 static Chan*
-ropen(Chan *c, u32 omode)
+cropen(Chan *c, u32 omode)
 {
-	Ready *r;
-	int trunc;
+	Canread *r;
+	int trunc, i;
 
-	if((r = up->readyfds) == nil)
+	if((r = up->canread) == nil)
 		error(Enonexist);
 
 	if(c->qid.type & QTDIR) {
 		if(omode != OREAD)
 			error(Eperm);
-	}else if(c->qid.path == Qready && omode != OREAD){
-			error(Eperm);
-	}else {
+	}else if(c->qid.path == Qcanread){
+		if(r->commq == nil)
+			r->commq = qopen(1024, 0, 0, 0);
 		trunc = omode & OTRUNC;
-		if(omode != OREAD)
-			error(Eperm);
-
-		qlock(r);
-		r->commq = qopen(1024, 0, 0, 0);
 		if(trunc) { /* shoud be Qwatch */
 			for(i = 0; i < r->nrfd; i++){
 				r->rfd[i].stop = 1;
@@ -151,7 +110,8 @@
 			}
 			r->nrfd = 0;
 		}
-		qunlock(r);
+	}else {
+		error(Enonexist);
 	}
 	c->mode = openmode(omode);
 	c->offset = 0;
@@ -160,28 +120,22 @@
 }
 
 static void
-rcreate(Chan *c, char *name, u32 omode, u32)
+crcreate(Chan *c, char *name, u32 omode, u32)
 {
-	Sgrp *g;
-	Svalue *v, *ev;
-	s32 i;
 
-	if((r = up->readyfds) == nil)
+	if(up->canread == nil)
 		error(Enonexist);
 
 	error(Eexist);
 }
 
-rread(Chan *c, void *a, s32 n, s64 off)
+crread(Chan *c, void *a, s32 n, s64)
 {
-	Sgrp *g;
-	Svalue *v;
-	u64 offset = off;
-
+	Canread *r;
 	if(c->qid.type & QTDIR)
-		return devdirread(c, a, n, 0, 0, shmgen);
+		return devdirread(c, a, n, 0, 0, crgen);
 
-	if((r = up->readyfds) == nil)
+	if((r = up->canread) == nil)
 		error(Enonexist);
 
 	return qread(r->commq, a, n);
@@ -191,14 +145,15 @@
 static void
 stopwatchers(void)
 {
-	Ready *r;
+	Canread *r;
+	int i;
 
-	if((r = up->readyfds) == nil)
+	if((r = up->canread) == nil)
 		error(Enonexist);
 
 	if(r->commq != nil)
 		qhangup(r->commq, nil);
-	for(i = 0; i < r->rnfd; i++){
+	for(i = 0; i < r->nrfd; i++){
 		r->rfd[i].stop = 1;
 		r->rfd[i].fd = -1;
 	}
@@ -210,10 +165,16 @@
 	TODO double check wv values
  */
 static void
-startwatcher(Readyfd *rfd)
+startwatcher(void *p)
 {
 	char s[16];
+	s32 wv, n;
+	Readyfd *rfd;
 
+	if(p == nil)
+		error(Enonexist);
+
+	rfd = p;
 	rfd->kproc = up;
 	while(rfd->stop == 0 && wv != 0 && wv != -1){
 		qhasdata(rfd->q);
@@ -224,30 +185,26 @@
 
 /* read the space separated fd's and add them to the Readyfd* list */
 static s32
-rwrite(Chan *c, void *a, s32 n, s64)
+crwrite(Chan *c, void *a, s32 n, s64 offset)
 {
-	u8 *s, *n, *end, name[KNAMELEN];
-	s32 nfd, fd;
+	s8 *s, *p, *end, name[KNAMELEN];
+	s32 nfd, fd, i;
+	Canread *r;
 
-	if(n <= 0)
-		return 0;
-	if(offset > Maxshmsize || n > (Maxshmsize - offset))
-		error(Etoobig);
-
-	if((r = up->readyfds) == nil)
+	if((r = up->canread) == nil)
 		error(Enonexist);
 
 	stopwatchers();
 	r->nrfd = 0;
-	end = (u8*)a+n;
-	s = (u8*)a;
+	end = (s8*)a+n;
+	s = (s8*)a;
 	nfd = 0;
 	while(s < end){
 		fd = -1;
-		fd = strtoul(s, &n, 0);
-		if(s == n || fd == -1)
+		fd = strtoul(s, &p, 0);
+		if(s == p || fd == -1)
 			break;
-		s = n;
+		s = p;
 		nfd++;
 		if(nfd > NFD)
 			error(Etoobig);
@@ -256,36 +213,35 @@
 		r->rfd[i].ready = 0;
 		r->rfd[i].commq = r->commq;
 		snprint(name, KNAMELEN, "watch %d for %d", fd, up->pid);
-		kproc(name, startwatcher, &r->rfd[i], 0);
+		kproc(name, startwatcher, (void*)&(r->rfd[i]), 0);
 		r->nrfd++;
 	}
-	qunlock(r);
 
 	return n;
 }
 
 static void
-shmclose(Chan *c)
+crclose(Chan *c)
 {
 	stopwatchers();
 }
 
-Dev shmdevtab = {
-	'r',
+Dev readydevtab = {
+	'R',
 	"ready",
 
 	devreset,
 	devinit,
 	devshutdown,
-	rattach,
-	rwalk,
-	rstat,
-	ropen,
-	rcreate,
-	rclose,
-	rread,
+	crattach,
+	crwalk,
+	crstat,
+	cropen,
+	crcreate,
+	crclose,
+	crread,
 	devbread,
-	rwrite,
+	crwrite,
 	devbwrite,
 	devremove,
 	devwstat