New thread library
This commit is contained in:
parent
7788fd5409
commit
1544f90960
70 changed files with 1041 additions and 4684 deletions
38
src/libthread/test/pthreadloop.c
Normal file
38
src/libthread/test/pthreadloop.c
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
80
src/libthread/test/tprimes.c
Normal file
80
src/libthread/test/tprimes.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
39
src/libthread/test/tspawn.c
Normal file
39
src/libthread/test/tspawn.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
41
src/libthread/test/tspawnloop.c
Normal file
41
src/libthread/test/tspawnloop.c
Normal file
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue