make work with new thread library

This commit is contained in:
rsc 2004-12-26 21:37:31 +00:00
parent f99790979b
commit 60535a5ff6
3 changed files with 45 additions and 19 deletions

View file

@ -20,7 +20,7 @@ enum
int noecho = 0; int noecho = 0;
void servedevtext(void); void servedevtext(void);
void listenthread(void*); void listenproc(void*);
void textthread(void*); void textthread(void*);
typedef struct Text Text; typedef struct Text Text;
@ -249,7 +249,7 @@ threadmain(int argc, char *argv[])
initdraw(0, nil, "9term"); initdraw(0, nil, "9term");
notify(hangupnote); notify(hangupnote);
notifyatsig(SIGCHLD, 1); noteenable("sys: child");
servedevtext(); servedevtext();
mc = initmouse(nil, screen); mc = initmouse(nil, screen);
@ -322,7 +322,7 @@ hostproc(void *arg)
yield(); yield();
i = 1-i; /* toggle */ i = 1-i; /* toggle */
n = threadread(rcfd, rcbuf[i].data, sizeof rcbuf[i].data); n = read(rcfd, rcbuf[i].data, sizeof rcbuf[i].data);
if(n <= 0){ if(n <= 0){
if(n < 0) if(n < 0)
fprint(2, "9term: host read error: %r\n"); fprint(2, "9term: host read error: %r\n");
@ -338,7 +338,7 @@ void
hoststart(void) hoststart(void)
{ {
hostc = chancreate(sizeof(int), 0); hostc = chancreate(sizeof(int), 0);
threadcreate(hostproc, hostc, 32*1024); proccreate(hostproc, hostc, 32*1024);
} }
void void
@ -1868,25 +1868,25 @@ servedevtext(void)
} }
putenv("text9term", buf); putenv("text9term", buf);
threadcreate(listenthread, nil, STACK); proccreate(listenproc, nil, STACK);
strcpy(thesocket, buf+5); strcpy(thesocket, buf+5);
atexit(removethesocket); atexit(removethesocket);
} }
void void
listenthread(void *arg) listenproc(void *arg)
{ {
int fd; int fd;
char dir[100]; char dir[100];
USED(arg); USED(arg);
for(;;){ for(;;){
fd = threadlisten(adir, dir); fd = listen(adir, dir);
if(fd < 0){ if(fd < 0){
close(afd); close(afd);
return; return;
} }
threadcreate(textthread, (void*)fd, STACK); proccreate(textthread, (void*)fd, STACK);
} }
} }

View file

@ -4,19 +4,24 @@
#include "term.h" #include "term.h"
static void static void
sys(char *buf) sys(char *buf, int devnull)
{ {
char buf2[100]; char buf2[100];
char *f[20]; char *f[20];
int nf, pid; int nf, pid;
notedisable("sys: child");
strcpy(buf2, buf); strcpy(buf2, buf);
nf = tokenize(buf2, f, nelem(f)); nf = tokenize(buf2, f, nelem(f));
f[nf] = nil; f[nf] = nil;
switch(pid = fork()){ switch(pid = fork()){
case 0: case 0:
close(1);
open("/dev/null", OREAD);
close(2);
open("/dev/null", OREAD);
execvp(f[0], f); execvp(f[0], f);
_exits("oops"); _exit(2);
default: default:
waitpid(); waitpid();
} }
@ -43,18 +48,23 @@ rcstart(int argc, char **argv, int *pfd, int *tfd)
* fd0 is slave (tty), fd1 is master (pty) * fd0 is slave (tty), fd1 is master (pty)
*/ */
fd[0] = fd[1] = -1; fd[0] = fd[1] = -1;
if(getpts(fd, slave) < 0) if(getpts(fd, slave) < 0){
exit(3);
sysfatal("getpts: %r\n"); sysfatal("getpts: %r\n");
switch(pid = fork()) { }
notedisable("sys: window size change");
pid = fork();
switch(pid){
case 0: case 0:
putenv("TERM", "9term"); putenv("TERM", "9term");
sfd = childpty(fd, slave); sfd = childpty(fd, slave);
dup(sfd, 0); dup(sfd, 0);
dup(sfd, 1); dup(sfd, 1);
dup(sfd, 2); dup(sfd, 2);
sys("stty tabs -onlcr onocr icanon echo erase '^h' intr '^?'"); sys("stty tabs -onlcr icanon echo erase '^h' intr '^?'", 0);
sys("stty onocr", 1); /* not available on mac */
if(noecho) if(noecho)
sys("stty -echo"); sys("stty -echo", 0);
for(i=3; i<100; i++) for(i=3; i<100; i++)
close(i); close(i);
signal(SIGINT, SIG_DFL); signal(SIGINT, SIG_DFL);
@ -62,7 +72,7 @@ rcstart(int argc, char **argv, int *pfd, int *tfd)
signal(SIGTERM, SIG_DFL); signal(SIGTERM, SIG_DFL);
execvp(argv[0], argv); execvp(argv[0], argv);
fprint(2, "exec %s failed: %r\n", argv[0]); fprint(2, "exec %s failed: %r\n", argv[0]);
_exits("oops"); _exit(2);
break; break;
case -1: case -1:
sysfatal("proc failed: %r"); sysfatal("proc failed: %r");

View file

@ -109,6 +109,20 @@ waitthread(void *v)
threadexitsall(nil); threadexitsall(nil);
} }
void
hangupnote(void *a, char *msg)
{
if(strcmp(msg, "hangup") == 0 && pid != 0){
postnote(PNGROUP, pid, "hangup");
noted(NDFLT);
}
if(strstr(msg, "child")){
/* bug: do better */
threadexitsall(0);
}
noted(NDFLT);
}
void void
threadmain(int argc, char **argv) threadmain(int argc, char **argv)
{ {
@ -140,7 +154,10 @@ threadmain(int argc, char **argv)
} }
} }
threadnotify(nopipes, 1); notedisable("sys: write on closed pipe");
noteenable("sys: child");
notify(hangupnote);
if((fs = nsmount("acme", "")) == 0) if((fs = nsmount("acme", "")) == 0)
sysfatal("nsmount acme: %r"); sysfatal("nsmount acme: %r");
ctlfd = fsopen(fs, "new/ctl", ORDWR|OCEXEC); ctlfd = fsopen(fs, "new/ctl", ORDWR|OCEXEC);
@ -184,7 +201,7 @@ threadmain(int argc, char **argv)
fswrite(ctlfd, buf, strlen(buf)); fswrite(ctlfd, buf, strlen(buf));
updatewinsize(25, 80, 0, 0); updatewinsize(25, 80, 0, 0);
threadcreate(stdoutproc, nil, STACK); proccreate(stdoutproc, nil, STACK);
stdinproc(nil); stdinproc(nil);
} }
@ -420,13 +437,12 @@ stdoutproc(void *v)
char x[16], hold[UTFmax]; char x[16], hold[UTFmax];
USED(v); USED(v);
threadnotify(nopipes, 1);
buf = malloc(8192+UTFmax+1); buf = malloc(8192+UTFmax+1);
npart = 0; npart = 0;
for(;;){ for(;;){
/* Let typing have a go -- maybe there's a rubout waiting. */ /* Let typing have a go -- maybe there's a rubout waiting. */
yield(); yield();
n = threadread(fd1, buf+npart, 8192); n = read(fd1, buf+npart, 8192);
if(n < 0) if(n < 0)
error(nil); error(nil);
if(n == 0) if(n == 0)