提交 beed8e04 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

pthread-like thing

上级 c132eade
......@@ -4,12 +4,13 @@
#include "mtrace.h"
#include "amd64.h"
#include "uspinlock.h"
#include "pthread.h"
enum { readaccess = 0 };
enum { verbose = 0 };
enum { npg = 1 };
void
void*
thr(void *arg)
{
u64 tid = (u64)arg;
......@@ -34,6 +35,7 @@ thr(void *arg)
exit();
}
}
return 0;
}
int
......@@ -50,11 +52,9 @@ main(int ac, char **av)
// fprintf(1, "mapbench[%d]: start esp %x, nthread=%d\n", getpid(), rrsp(), nthread);
for(int i = 0; i < nthread; i++) {
sbrk(8192);
void *tstack = sbrk(0);
// fprintf(1, "tstack %lx\n", tstack);
int tid = forkt(tstack, (void*) thr, (void *)(u64)i);
for(u64 i = 0; i < nthread; i++) {
pthread_t tid;
pthread_create(&tid, 0, thr, (void*) i);
if (0) fprintf(1, "mapbench[%d]: child %d\n", getpid(), tid);
}
......
......@@ -4,6 +4,7 @@
#include "mtrace.h"
#include "amd64.h"
#include "uspinlock.h"
#include "pthread.h"
static volatile char *p;
static struct uspinlock l;
......@@ -17,8 +18,8 @@ spin(void)
;
}
void
thr(void)
void*
thr(void*)
{
for (;;) {
acquire(&l);
......@@ -54,8 +55,8 @@ main(void)
exit();
}
sbrk(4096);
forkt(sbrk(0), (void*) thr, 0);
pthread_t tid;
pthread_create(&tid, 0, thr, 0);
acquire(&l);
state = 1;
......
......@@ -4,12 +4,13 @@
#include "mtrace.h"
#include "amd64.h"
#include "uspinlock.h"
#include "pthread.h"
static struct uspinlock l;
static volatile int tcount;
enum { nthread = 8 };
void
void*
thr(void *arg)
{
acquire(&l);
......@@ -26,9 +27,8 @@ main(void)
fprintf(1, "thrtest[%d]: start esp %x\n", getpid(), rrsp());
for(int i = 0; i < nthread; i++) {
sbrk(8192);
void *tstack = sbrk(0);
int tid = forkt(tstack, (void*) thr, (void*)(u64)(0xc0ffee00|i));
pthread_t tid;
pthread_create(&tid, 0, &thr, (void*) (0xc0ffee00ULL | i));
fprintf(1, "thrtest[%d]: child %d\n", getpid(), tid);
}
......
/*
* Our minimal version of pthreads
*/
typedef int pthread_t;
typedef int pthread_attr_t;
typedef int pthread_key_t;
int pthread_create(pthread_t* tid, const pthread_attr_t* attr, void* (*start)(void*), void* arg);
pthread_t pthread_self(void);
int pthread_key_create(pthread_key_t* key, void (*destructor)(void*));
void* pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, void* value);
......@@ -50,6 +50,7 @@ int atoi(const char*);
// uthread.S
int forkt(void *sp, void *pc, void *arg);
void forkt_setup(void);
// printf.c
void printf(const char*, ...);
......
ULIB = ulib.o usys.o printf.o umalloc.o uthread.o fmt.o stream.o ipc.o
ULIB = ulib.o usys.o printf.o umalloc.o uthread.o fmt.o stream.o ipc.o \
threads.o
ULIB := $(addprefix $(O)/lib/, $(ULIB))
.PRECIOUS: $(O)/lib/%.o
......
#include "types.h"
#include "pthread.h"
#include "user.h"
#include "atomic.hh"
enum { stack_size = 8192 };
static std::atomic<int> nextkey;
struct tlsdata {
void* buf[128];
};
void
forkt_setup()
{
printf("forkt_setup: pid %d\n", getpid());
tlsdata* t = (tlsdata*) sbrk(sizeof(*t));
setfs((u64) t);
}
int
pthread_create(pthread_t* tid, const pthread_attr_t* attr,
void* (*start)(void*), void* arg)
{
char* base = (char*) sbrk(stack_size);
int t = forkt(base + stack_size, (void*) start, arg);
if (t < 0)
return t;
*tid = t;
return 0;
}
pthread_t
pthread_self()
{
return getpid();
}
int
pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
{
// Ignore the destructor for now.
*key = nextkey++;
return 0;
}
void*
pthread_getspecific(pthread_key_t key)
{
u64 v;
__asm volatile("movq %%fs:(%1), %0" : "=r" (v) : "r" (key * 8));
return (void*) v;
}
int
pthread_setspecific(pthread_key_t key, void* value)
{
__asm volatile("movq %0, %%fs:(%1)" : : "r" (value), "r" (key * 8));
return 0;
}
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论