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

enum
{
	STACK = 8192
};

int quiet;
int goal;
int buffer;
int (*fn)(void(*)(void*), void*, uint) = threadcreate;

void
primethread(void *arg)
{
	Channel *c, *nc;
	int p, i;

	c = arg;
	p = recvul(c);
	if(p > goal)
		threadexitsall(nil);
	if(!quiet)
		print("%d\n", p);
	nc = chancreate(sizeof(ulong), buffer);
	(*fn)(primethread, nc, STACK);
	for(;;){
		i = recvul(c);
		if(i%p)
			sendul(nc, i);
	}
}

extern int _threaddebuglevel;

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

	ARGBEGIN{
	case 'D':
		_threaddebuglevel = atoi(ARGF());
		break;
	case 'q':
		quiet = 1;
		break;
	case 'b':
		buffer = atoi(ARGF());
		break;
	case 'p':
		fn=proccreate;
		break;
	}ARGEND

	if(argc>0)
		goal = atoi(argv[0]);
	else
		goal = 100;

	c = chancreate(sizeof(ulong), buffer);
	(*fn)(primethread, c, STACK);
	for(i=2;; i++)
		sendul(c, i);
}