rscmore tests4223fce21 years agoHistory
89 lines · 1.1 KB · C
RawDownload
#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*); voidcountthread(void *v){	uint i;	Channel *c; 	c = v;	for(i=2;; i++){		sendul(c, i);	}} voidfilterthread(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);	}} voidusage(void){	fprint(2, "usage: tprimes [-p] [max]\n");	threadexitsall("usage");} voidthreadmain(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));} voidprintmsg(void *v, char *msg){	print("note: %s\n", msg);}