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/pthreadloop.c | 38 +++++++++++++++++++ src/libthread/test/tprimes.c | 80 ++++++++++++++++++++++++++++++++++++++++ src/libthread/test/tspawn.c | 39 ++++++++++++++++++++ src/libthread/test/tspawnloop.c | 41 ++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 src/libthread/test/pthreadloop.c create mode 100644 src/libthread/test/tprimes.c create mode 100644 src/libthread/test/tspawn.c create mode 100644 src/libthread/test/tspawnloop.c (limited to 'src/libthread/test') diff --git a/src/libthread/test/pthreadloop.c b/src/libthread/test/pthreadloop.c new file mode 100644 index 00000000..73b600e7 --- /dev/null +++ b/src/libthread/test/pthreadloop.c @@ -0,0 +1,38 @@ +#include +#include +#include + +pthread_key_t key; + +void +pexit(void *v) +{ + int s; + + pthread_setspecific(key, (void*)1); + switch(fork()){ + case -1: + fprint(2, "fork: %r\n"); + case 0: + _exit(0); + default: + wait(&s); + } + pthread_exit(0); +} + +int +main(int argc, char *argv[]) +{ + int i; + pthread_t pid; + + pthread_key_create(&key, 0); + for(i=0;; i++){ + print("%d\n", i); + if(pthread_create(&pid, 0, pexit, 0) < 0){ + fprint(2, "pthread_create: %r\n"); + abort(); + } + } +} 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); +} diff --git a/src/libthread/test/tspawn.c b/src/libthread/test/tspawn.c new file mode 100644 index 00000000..ed4df3c4 --- /dev/null +++ b/src/libthread/test/tspawn.c @@ -0,0 +1,39 @@ +#include "u.h" +#include "libc.h" +#include "thread.h" + +void +threadmain(int argc, char **argv) +{ + int fd[3]; + Channel *c; + Waitmsg *w; + + ARGBEGIN{ + case 'D': + break; + }ARGEND + + c = threadwaitchan(); + fd[0] = dup(0, -1); + fd[1] = dup(1, -1); + fd[2] = dup(2, -1); + if(threadspawn(fd, argv[0], argv) < 0) + sysfatal("threadspawn: %r"); + fd[0] = dup(0, -1); + fd[1] = dup(1, -1); + fd[2] = dup(2, -1); + if(threadspawn(fd, argv[0], argv) < 0) + sysfatal("threadspawn: %r"); + w = recvp(c); + if(w == nil) + print("exec/recvp failed: %r\n"); + else + print("%d %lud %lud %lud %s\n", w->pid, w->time[0], w->time[1], w->time[2], w->msg); + w = recvp(c); + if(w == nil) + print("exec/recvp failed: %r\n"); + else + print("%d %lud %lud %lud %s\n", w->pid, w->time[0], w->time[1], w->time[2], w->msg); + threadexits(nil); +} diff --git a/src/libthread/test/tspawnloop.c b/src/libthread/test/tspawnloop.c new file mode 100644 index 00000000..8835267f --- /dev/null +++ b/src/libthread/test/tspawnloop.c @@ -0,0 +1,41 @@ +#include "u.h" +#include "libc.h" +#include "thread.h" + +void +execproc(void *v) +{ + int i, fd[3]; + char buf[100], *args[3]; + + i = (int)v; + sprint(buf, "%d", i); + fd[0] = dup(0, -1); + fd[1] = dup(1, -1); + fd[2] = dup(2, -1); + args[0] = "echo"; + args[1] = buf; + args[2] = nil; + threadexec(nil, fd, args[0], args); +} + +void +threadmain(int argc, char **argv) +{ + int i; + Channel *c; + Waitmsg *w; + + ARGBEGIN{ + case 'D': + break; + }ARGEND + + c = threadwaitchan(); + for(i=0;; i++){ + proccreate(execproc, (void*)i, 16384); + w = recvp(c); + if(w == nil) + sysfatal("exec/recvp failed: %r"); + } +} -- cgit v1.2.3