ref: 482dce5fd337a05deac98fa492af4ce9193b4040
parent: cc84e45d9e3aab0b442e5e7a09a642bb49898de5
author: Michael Misch <michaelmisch1985@gmail.com>
date: Sun Jun 23 17:31:28 EDT 2024
Initial work for blocking feeds
--- a/alt.h
+++ b/alt.h
@@ -3,6 +3,7 @@
struct Buffer
{+ QLock;
char *name;
char title[1024];
char status[1024];
@@ -10,8 +11,8 @@
int fd;
Channel *cmds;
Notify *notify;
-
Buffer *next;
+ Rendez rz;
};
struct Notify
--- a/buffer.c
+++ b/buffer.c
@@ -49,6 +49,7 @@
b = emalloc(sizeof(*b));
b->name = estrdup(name);
b->notify = nil;
+ b->rz.l = b;
memset(b->title, 0, sizeof(b->title));
memset(b->status, 0, sizeof(b->status));
memset(b->aside, 0, sizeof(b->aside));
@@ -67,10 +68,14 @@
bufferSearch(Buffer *base, char *name)
{Buffer *sp;
-
+ qlock(base);
for(sp = base; sp; sp = sp->next)
- if(strcmp(sp->name, name) == 0)
+ if(strcmp(sp->name, name) == 0){+ qunlock(base);
+ qlock(sp);
return sp;
+ }
+ qunlock(base);
return nil;
}
@@ -87,7 +92,7 @@
b->cmds = cmds;
b->notify = nil;
b->next = nil;
+ b->rz.l = b;
return b;
}
-
--- a/client.c
+++ b/client.c
@@ -73,6 +73,8 @@
cl->current = bufferSearch(root, aname);
cl->showmarkdown = 0;
+ if(cl->current)
+ qunlock(cl->current);
return cl;
}
@@ -208,6 +210,7 @@
clread(Req *r)
{Clfid *f;
+ Buffer *b;
Notify *np;
char buf[1024];
int n;
@@ -219,14 +222,27 @@
respond(r, nil);
return;
case Qfeed:
- // TODO: We could read a channel here and not respond until we have new data
- // This currently does not block for input, but we want it to
+Read:
+ // Catch EOF
+ if(!f->cl->fd || !f->cl->current){+ r->ofcall.data[0] = 0;
+ respond(r, nil);
+ return;
+ }
+ b = f->cl->current;
+ qlock(b);
n = pread(f->cl->fd, buf, sizeof(buf), r->ifcall.offset);
if(n){- r->ofcall.count = n - 1;
- memmove(r->ofcall.data, buf, r->ofcall.count);
+ // cut off the EOF
+ r->ofcall.count = strlen(buf) - 1;
+ memmove(r->ofcall.data, buf, n);
+ } else if(f->cl->current) {+ rsleep(&b->rz);
+ qunlock(b);
+ goto Read;
}
respond(r, nil);
+ qunlock(b);
return;
case Qtitle:
if(f->cl->current && f->cl->current->title){@@ -289,6 +305,18 @@
memset(path, sizeof(path), 0);
snprint(path, sizeof(path), "%s/%s/%s", logdir, root->name, s);
f->cl->fd = open(path, OREAD);
+ r->fid->aux = f;
+ respond(r, nil);
+ qunlock(f->cl->current);
+ } else if(strcmp(t, "markdown")==0){+ if(f->cl->showmarkdown)
+ f->cl->showmarkdown = 0;
+ else
+ f->cl->showmarkdown = 1;
+ r->fid->aux = f;
+ respond(r, nil);
+ } else if(strcmp(t, "hidemarkdown")==0){+ f->cl->showmarkdown = 0;
r->fid->aux = f;
respond(r, nil);
} else {--- a/service.c
+++ b/service.c
@@ -302,10 +302,8 @@
if(!f->svc->isInitialized) {snprint(buf, sizeof(buf), "%d\n", SERVICEID(f->svc));
readstr(r, buf);
- } else {+ } else
recv(f->svc->cmds, buf);
- print("%s\n", buf);- }
respond(r, nil);
return;
@@ -326,6 +324,11 @@
if(b = bufferSearch(svc->base, targ)) {seek(b->fd, 0, 2);
write(b->fd, data, strlen(data));
+
+ if(rwakeupall(&b->rz)){+ // We have readers, we can update tabs
+ }
+ qunlock(b);
return nil;
}
return "buffer not found";
@@ -332,6 +335,7 @@
} else if(strcmp(cmd, "status")==0){ if(b = bufferSearch(svc->base, targ)) {strcpy(b->status, data);
+ qunlock(b);
return nil;
}
return "buffer not found";
@@ -338,6 +342,7 @@
} else if(strcmp(cmd, "title")==0){ if(b = bufferSearch(svc->base, targ)) {strcpy(b->title, data);
+ qunlock(b);
return nil;
}
return "buffer not found";
@@ -344,6 +349,7 @@
} else if(strcmp(cmd, "status")==0){ if(b = bufferSearch(svc->base, targ)) {strcpy(b->status, data);
+ qunlock(b);
return nil;
}
return "buffer not found";
@@ -350,6 +356,7 @@
} else if(strcmp(cmd, "aside")==0){ if(b = bufferSearch(svc->base, targ)) {strcpy(b->aside, data);
+ qunlock(b);
return nil;
}
return "buffer not found";
--
⑨