summaryrefslogtreecommitdiff
path: root/appl/cmd/mash/serve.b
blob: e293a8f47427e003f666ec109cd6583da2dc7534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#
#	This should be called by spawned (persistent) threads.
#	It arranges for them to be killed at the end of the day.
#
reap()
{
	if (pidchan == nil) {
		pidchan = chan of int;
		spawn zombie();
	}
	pidchan <-= sys->pctl(0, nil);
}

#
#	This thread records spawned threads and kills them.
#
zombie()
{
	pids := array[10] of int;
	pidx := 0;
	for (;;) {
		pid := <- pidchan;
		if (pid == PIDEXIT) {
			for (i := 0; i < pidx; i++)
				kill(pids[i]);
			exit;
		}
		if (pidx == len pids) {
			n := pidx * 3 / 2;
			a := array[n] of int;
			a[:] = pids;
			pids = a;
		}
		pids[pidx++] = pid;
	}
}

#
#	Kill a thread.
#
kill(pid: int)
{
	fd := sys->open("#p/" + string pid + "/ctl", sys->OWRITE);
	if (fd != nil)
		sys->fprint(fd, "kill");
}

#
#	Exit top level, killing spawned threads.
#
exitmash()
{
	if (pidchan != nil)
		pidchan <-= PIDEXIT;
	exit;
}

#
#	Slice a buffer if needed.
#
restrict(buff: array of byte, count: int): array of byte
{
	if (count < len buff)
		return buff[:count];
	else
		return buff;
}

#
#	Serve mash console reads.  Favours other programs
#	ahead of the input loop.
#
serve_read(c: ref Sys->FileIO, sync: chan of int)
{
	s: string;
	in := sys->fildes(0);
	sys->pctl(Sys->NEWFD, in.fd :: nil);
	sync <-= 0;
	reap();
	buff := array[Sys->ATOMICIO] of byte;
outer:	for (;;) {
		n := sys->read(in, buff, len buff);
		if (n < 0) {
			n = 0;
			s = errstr();
		} else
			s = nil;
		b := buff[:n];
		alt {
		(off, count, fid, rc) := <-c.read =>
			if (rc == nil)
				break;
			rc <-= (restrict(b, count), s);
			continue outer;
		* =>
			;
		}
	inner:	for (;;) {
			alt {
			(off, count, fid, rc) := <-c.read =>
				if (rc == nil)
					continue inner;
				rc <-= (restrict(b, count), s);
			inchan <-= b =>
				;
			}
			break;
		}
	}
}

#
#	Serve mash console writes.
#
serve_write(c: ref Sys->FileIO, sync: chan of int)
{
	out := sys->fildes(1);
	sys->pctl(Sys->NEWFD, out.fd :: nil);
	sync <-= 0;
	reap();
	for (;;) {
		(off, data, fid, wc) := <-c.write;
		if (wc == nil)
			continue;
		if (sys->write(out, data, len data) < 0)
			wc <-= (0, errstr());
		else
			wc <-= (len data, nil);
	}
}

#
#	Begin serving the mash console.
#
Env.serve(e: self ref Env)
{
	if (servechan != nil)
		return;
	(s, c) := e.servefile(nil);
	inchan = chan of array of byte;
	servechan = chan of array of byte;
	sync := chan of int;
	spawn serve_read(c, sync);
	spawn serve_write(c, sync);
	<-sync;
	<-sync;
	if (sys->bind(s, CONSOLE, Sys->MREPL) < 0)
		e.couldnot("bind", CONSOLE);
	sys->pctl(Sys->NEWFD, nil);
	e.in = sys->open(CONSOLE, sys->OREAD | sys->ORCLOSE);
	e.out = sys->open(CONSOLE, sys->OWRITE);
	e.stderr = sys->open(CONSOLE, sys->OWRITE);
	e.wait = nil;
}