#include <9pm/u.h>
#include <9pm/libc.h>
#include <9pm/ns.h>
#include <9pm/thread.h>
#include <9pm/threadimpl.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);
	proccreate(primethread, nc, 8192);
	for(;;){
		i = recvul(c);
		if(i%p)
			sendul(nc, i);
	}
}

void
threadmain(int argc, char **argv)
{
	int i;
	Channel *c;
//	_threaddebuglevel = DBGPROC;

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

	c = chancreate(sizeof(ulong), 0);
	proccreate(primethread, c, 8192);
	for(i=2;; i++)
		sendul(c, i);
	chanfree(c);
	threadexitsall(nil);
}
