From 1544f90960275dc9211bde30329c3258e0e1bf38 Mon Sep 17 00:00:00 2001 From: rsc Date: Sat, 25 Dec 2004 21:56:33 +0000 Subject: New thread library --- src/libthread/test/tprimes.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/libthread/test/tprimes.c (limited to 'src/libthread/test/tprimes.c') diff --git a/src/libthread/test/tprimes.c b/src/libthread/test/tprimes.c new file mode 100644 index 00000000..91af5a73 --- /dev/null +++ b/src/libthread/test/tprimes.c @@ -0,0 +1,80 @@ +#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 +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; + + 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); +} -- cgit v1.2.3