summaryrefslogtreecommitdiff
path: root/Channels/chans.b
blob: 700ea639e305dd907c4152a7d71e62a44ad4681d (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
implement Chans;

include "sys.m";
include "draw.m";

sys: Sys;
print: import sys;

Chans: module {
	init: fn(nil: ref Draw->Context, nil: list of string);
};

summer(done: chan of int, msg: chan of string, num: chan of (int, int)) {
	sum := big 0;
	run := 1;

	while(run)
		alt {
		(n, p) := <-num =>
			sum += big n**p;
			print("Sum: %bd\n", sum);
		<-done =>
			msg <-= string sum;
			run = 0;
		* =>
			sys->sleep(5);
		}
}

init(nil: ref Draw->Context, nil: list of string) {
	sys = load Sys Sys->PATH;

	n := 4;
	done := chan of int;
	msgChan := chan[0] of string;
	numChan := chan[n] of (int, int);

	spawn summer(done, msgChan, numChan);

	for(i := 0; i < 15; i++)
		numChan <-= (i, 2);

	done <-= 0;

	print("Final sum: %s\n", <-msgChan);

	buf := chan[20] of int;

	print("Len: %d\n", len buf);

	for(i = 0; i < 5; i++)
		buf <-= i;

	exit;
}