aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/test/tprimes.c
blob: 56744984bddc4bc5f1ffd7822803e14cf5d8ceed (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
#include "u.h"
#include "libc.h"
#include "thread.h"

enum
{
	STACK = 8192
};

int max = 10000;
int (*mk)(void (*fn)(void*), void *arg, uint stack);
void printmsg(void*, char*);

void
countthread(void *v)
{
	uint i;
	Channel *c;

	c = v;
	for(i=2;; i++){
		sendul(c, i);
	}
}

void
filterthread(void *v)
{
	uint i, p;
	Channel *c, *nextc;

	c = v;
	p = recvul(c);
	print("%d\n", p);
	if(p > max)
		threadexitsall(0);
	nextc = chancreate(sizeof(ulong), 0);
	mk(filterthread, nextc, STACK);
	for(;;){
		i = recvul(c);
		if(i%p)
			sendul(nextc, i);
	}
}

void
usage(void)
{
	fprint(2, "usage: tprimes [-p] [max]\n");
	threadexitsall("usage");
}

void
threadmain(int argc, char **argv)
{
	Channel *c;
	int nbuf;

	notify(printmsg);
	nbuf = 0;
	mk = threadcreate;
	ARGBEGIN{
	default:
		usage();
	case 'b':
		nbuf = atoi(EARGF(usage()));
		break;
	case 'p':
		mk = proccreate;
		max = 1000;
		break;
	}ARGEND

	if(argc == 1)
		max = atoi(argv[0]);
	else if(argc)
		usage();

	c = chancreate(sizeof(ulong), nbuf);
	mk(countthread, c, STACK);
	mk(filterthread, c, STACK);
	recvp(chancreate(sizeof(void*), 0));
}

void
printmsg(void *v, char *msg)
{
	print("note: %s\n", msg);
}