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;
c = malloc(sizeof *c+bufsize*elemsize);
if(c == nil)
sysfatal("chancreate malloc: %r");
memset(c, 0, sizeof *c);
c->elemsize = elemsize;
c->bufsize = bufsize;

View file

@ -71,16 +71,40 @@ _procwakeup(_Procrendez *r)
}
}
void
_procstart(Proc *p, void (*fn)(void*))
static void
startprocfn(void *v)
{
//print("pc\n");
if(pthread_create(&p->tid, nil, (void*(*)(void*))fn, p) < 0){
//print("pc1\n");
void **a;
void (*fn)(void*);
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");
abort();
}
//print("pc2\n");
}
static pthread_key_t prockey;

View file

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

View file

@ -67,7 +67,7 @@ extern Proc *xxx;
#define proc() _threadproc()
#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 void _threadexit(void);
extern Proc *_threadproc(void);