aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/test
diff options
context:
space:
mode:
authorrsc <devnull@localhost>2004-12-25 21:56:33 +0000
committerrsc <devnull@localhost>2004-12-25 21:56:33 +0000
commit1544f90960275dc9211bde30329c3258e0e1bf38 (patch)
treef55e7a73c03aaa24daa7cc2ad02822b921c477f9 /src/libthread/test
parent7788fd54094693384ef5c92c475656dba8819feb (diff)
downloadplan9port-1544f90960275dc9211bde30329c3258e0e1bf38.tar.gz
plan9port-1544f90960275dc9211bde30329c3258e0e1bf38.tar.bz2
plan9port-1544f90960275dc9211bde30329c3258e0e1bf38.zip
New thread library
Diffstat (limited to 'src/libthread/test')
-rw-r--r--src/libthread/test/pthreadloop.c38
-rw-r--r--src/libthread/test/tprimes.c80
-rw-r--r--src/libthread/test/tspawn.c39
-rw-r--r--src/libthread/test/tspawnloop.c41
4 files changed, 198 insertions, 0 deletions
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 <pthread.h>
+#include <utf.h>
+#include <fmt.h>
+
+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");
+ }
+}