confine pthreads to pthread.c

This commit is contained in:
rsc 2004-12-27 03:49:03 +00:00
parent e317e37406
commit 73722a8bbf
4 changed files with 39 additions and 14 deletions

View file

@ -22,6 +22,8 @@ chancreate(int elemsize, int bufsize)
Channel *c; Channel *c;
c = malloc(sizeof *c+bufsize*elemsize); c = malloc(sizeof *c+bufsize*elemsize);
if(c == nil)
sysfatal("chancreate malloc: %r");
memset(c, 0, sizeof *c); memset(c, 0, sizeof *c);
c->elemsize = elemsize; c->elemsize = elemsize;
c->bufsize = bufsize; c->bufsize = bufsize;

View file

@ -71,16 +71,40 @@ _procwakeup(_Procrendez *r)
} }
} }
void static void
_procstart(Proc *p, void (*fn)(void*)) startprocfn(void *v)
{ {
//print("pc\n"); void **a;
if(pthread_create(&p->tid, nil, (void*(*)(void*))fn, p) < 0){ void (*fn)(void*);
//print("pc1\n"); Proc *p;
a = (void**)v;
fn = a[0];
p = a[1];
free(a);
p->tid = pthread_self();
pthread_detach(p->tid);
(*fn)(p);
pthread_exit(0);
}
void
_procstart(Proc *p, void (*fn)(Proc*))
{
void **a;
a = malloc(2*sizeof a[0]);
if(a == nil)
sysfatal("_procstart malloc: %r");
a[0] = fn;
a[1] = p;
if(pthread_create(&p->tid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
fprint(2, "pthread_create: %r\n"); fprint(2, "pthread_create: %r\n");
abort(); abort();
} }
//print("pc2\n");
} }
static pthread_key_t prockey; static pthread_key_t prockey;

View file

@ -18,7 +18,7 @@ static void delthread(_Threadlist*, _Thread*);
static void addthreadinproc(Proc*, _Thread*); static void addthreadinproc(Proc*, _Thread*);
static void delthreadinproc(Proc*, _Thread*); static void delthreadinproc(Proc*, _Thread*);
static void contextswitch(Context *from, Context *to); static void contextswitch(Context *from, Context *to);
static void scheduler(void*); static void scheduler(Proc*);
static _Thread* static _Thread*
getthreadnow(void) getthreadnow(void)
@ -33,6 +33,8 @@ procalloc(void)
Proc *p; Proc *p;
p = malloc(sizeof *p); p = malloc(sizeof *p);
if(p == nil)
sysfatal("procalloc malloc: %r");
memset(p, 0, sizeof *p); memset(p, 0, sizeof *p);
lock(&threadnproclock); lock(&threadnproclock);
threadnproc++; threadnproc++;
@ -58,6 +60,8 @@ threadalloc(void (*fn)(void*), void *arg, uint stack)
/* allocate the task and stack together */ /* allocate the task and stack together */
t = malloc(sizeof *t+stack); t = malloc(sizeof *t+stack);
if(t == nil)
sysfatal("threadalloc malloc: %r");
memset(t, 0, sizeof *t); memset(t, 0, sizeof *t);
t->stk = (uchar*)(t+1); t->stk = (uchar*)(t+1);
t->stksize = stack; t->stksize = stack;
@ -183,16 +187,12 @@ contextswitch(Context *from, Context *to)
} }
static void static void
scheduler(void *v) scheduler(Proc *p)
{ {
_Thread *t; _Thread *t;
Proc *p;
p = v;
setproc(p); setproc(p);
// print("s %p %d\n", p, gettid()); // print("s %p %d\n", p, gettid());
p->tid = pthread_self();
pthread_detach(p->tid);
lock(&p->lock); lock(&p->lock);
for(;;){ for(;;){
while((t = p->runqueue.head) == nil){ while((t = p->runqueue.head) == nil){
@ -225,7 +225,6 @@ Out:
unlock(&p->lock); unlock(&p->lock);
free(p); free(p);
setproc(0); setproc(0);
pthread_exit(nil);
} }
void void

View file

@ -67,7 +67,7 @@ extern Proc *xxx;
#define proc() _threadproc() #define proc() _threadproc()
#define setproc(p) _threadsetproc(p) #define setproc(p) _threadsetproc(p)
extern void _procstart(Proc*, void (*fn)(void*)); extern void _procstart(Proc*, void (*fn)(Proc*));
extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint); extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
extern void _threadexit(void); extern void _threadexit(void);
extern Proc *_threadproc(void); extern Proc *_threadproc(void);