#include <9pm/u.h>
#include <9pm/libc.h>
#include <9pm/thread.h>

int goal;

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

	c = arg;
	p = recvul(c);
	if(p > goal)
		threadexitsall(nil);
	threadprint(1, "%d\n", p);
	nc = chancreate(sizeof(ulong), 0);
	threadcreate(primethread, nc, 8192);
	for(;;){
		i = recvul(c);
		if(i%p)
			sendul(nc, i);
	}
}

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

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

	c = chancreate(sizeof(ulong), 0);
	threadcreate(primethread, c, 8192);
	for(i=2;; i++)
		sendul(c, i);
}
