start linux pre-2.6 port

This commit is contained in:
rsc 2004-12-27 16:52:26 +00:00
parent fd46554030
commit 4dbefdd41c
5 changed files with 122 additions and 79 deletions

View file

@ -1,12 +1,14 @@
<$PLAN9/src/mkhdr <$PLAN9/src/mkhdr
SYSOFILES=`{sh ./sysofiles.sh}
LIB=libthread.a LIB=libthread.a
OFILES=\ OFILES=\
$SYSOFILES\
channel.$O\ channel.$O\
exec.$O\ exec.$O\
ioproc.$O\ ioproc.$O\
iorw.$O\ iorw.$O\
pthread.$O\
ref.$O\ ref.$O\
thread.$O\ thread.$O\

View file

@ -82,8 +82,8 @@ startprocfn(void *v)
fn = a[0]; fn = a[0];
p = a[1]; p = a[1];
free(a); free(a);
p->tid = pthread_self(); p->osprocid = pthread_self();
pthread_detach(p->tid); pthread_detach(p->osprocid);
(*fn)(p); (*fn)(p);
@ -101,7 +101,7 @@ _procstart(Proc *p, void (*fn)(Proc*))
a[0] = fn; a[0] = fn;
a[1] = p; a[1] = p;
if(pthread_create(&p->tid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){ if(pthread_create(&p->osprocid, nil, (void*(*)(void*))startprocfn, (void*)a) < 0){
fprint(2, "pthread_create: %r\n"); fprint(2, "pthread_create: %r\n");
abort(); abort();
} }

View file

@ -77,4 +77,5 @@ threadmain(int argc, char **argv)
c = chancreate(sizeof(ulong), nbuf); c = chancreate(sizeof(ulong), nbuf);
mk(countthread, c, STACK); mk(countthread, c, STACK);
mk(filterthread, c, STACK); mk(filterthread, c, STACK);
recvp(chancreate(sizeof(void*), 0));
} }

View file

@ -1,18 +1,18 @@
#include "u.h" #include "u.h"
#include <linux/unistd.h>
#include "libc.h" #include "libc.h"
#include "thread.h" #include "thread.h"
#include "threadimpl.h" #include "threadimpl.h"
_syscall0(pid_t,gettid)
int _threaddebuglevel; int _threaddebuglevel;
static uint threadnproc; static uint threadnproc;
static uint threadnsysproc; static uint threadnsysproc;
static Lock threadnproclock; static Lock threadnproclock;
static Ref threadidref; static Ref threadidref;
static Proc *threadmainproc;
static void addproc(Proc*);
static void delproc(Proc*);
static void addthread(_Threadlist*, _Thread*); static void addthread(_Threadlist*, _Thread*);
static void delthread(_Threadlist*, _Thread*); static void delthread(_Threadlist*, _Thread*);
static void addthreadinproc(Proc*, _Thread*); static void addthreadinproc(Proc*, _Thread*);
@ -36,6 +36,7 @@ procalloc(void)
if(p == nil) if(p == nil)
sysfatal("procalloc malloc: %r"); sysfatal("procalloc malloc: %r");
memset(p, 0, sizeof *p); memset(p, 0, sizeof *p);
addproc(p);
lock(&threadnproclock); lock(&threadnproclock);
threadnproc++; threadnproc++;
unlock(&threadnproclock); unlock(&threadnproclock);
@ -49,7 +50,7 @@ threadstart(void *v)
t = v; t = v;
t->startfn(t->startarg); t->startfn(t->startarg);
_threadexit(); threadexits(nil);
} }
static _Thread* static _Thread*
@ -115,9 +116,7 @@ proccreate(void (*fn)(void*), void *arg, uint stack)
Proc *p; Proc *p;
p = procalloc(); p = procalloc();
//print("pa %p\n", p);
t = _threadcreate(p, fn, arg, stack); t = _threadcreate(p, fn, arg, stack);
//print("ps %p\n", p);
_procstart(p, scheduler); _procstart(p, scheduler);
return t->id; return t->id;
} }
@ -150,31 +149,17 @@ threadyield(void)
_threadswitch(); _threadswitch();
} }
void
_threadexit(void)
{
proc()->thread->exiting = 1;
_threadswitch();
}
void void
threadexits(char *msg) threadexits(char *msg)
{ {
/*
Proc *p; Proc *p;
p = proc(); p = proc();
if(msg == nil)
msg = "";
utfecpy(p->msg, p->msg+sizeof p->msg, msg); utfecpy(p->msg, p->msg+sizeof p->msg, msg);
*/ proc()->thread->exiting = 1;
_threadexit(); _threadswitch();
}
void
threadexitsall(char *msg)
{
if(msg && msg[0])
exit(1);
exit(0);
} }
static void static void
@ -216,11 +201,12 @@ scheduler(Proc *p)
} }
Out: Out:
delproc(p);
lock(&threadnproclock); lock(&threadnproclock);
if(p->sysproc) if(p->sysproc)
--threadnsysproc; --threadnsysproc;
if(--threadnproc == threadnsysproc) if(--threadnproc == threadnsysproc)
exit(0); threadexitsall(p->msg);
unlock(&threadnproclock); unlock(&threadnproclock);
unlock(&p->lock); unlock(&p->lock);
free(p); free(p);
@ -237,6 +223,19 @@ _threadsetsysproc(void)
proc()->sysproc = 1; proc()->sysproc = 1;
} }
void**
procdata(void)
{
return &proc()->udata;
}
extern Jmp *(*_notejmpbuf)(void);
static Jmp*
threadnotejmp(void)
{
return &proc()->sigjmp;
}
/* /*
* debugging * debugging
*/ */
@ -421,6 +420,55 @@ threadrwakeup(Rendez *r, int all, ulong pc)
return i; return i;
} }
/*
* startup
*/
static int threadargc;
static char **threadargv;
int mainstacksize;
static void
threadmainstart(void *v)
{
USED(v);
threadmainproc = proc();
threadmain(threadargc, threadargv);
}
int
main(int argc, char **argv)
{
Proc *p;
threadargc = argc;
threadargv = argv;
/*
* Install locking routines into C library.
*/
_lock = _threadlock;
_unlock = _threadunlock;
_qlock = threadqlock;
_qunlock = threadqunlock;
_rlock = threadrlock;
_runlock = threadrunlock;
_wlock = threadwlock;
_wunlock = threadwunlock;
_rsleep = threadrsleep;
_rwakeup = threadrwakeup;
_notejmpbuf = threadnotejmp;
_pthreadinit();
p = procalloc();
_threadsetproc(p);
if(mainstacksize == 0)
mainstacksize = 65536;
_threadcreate(p, threadmainstart, nil, mainstacksize);
scheduler(p);
return 0; /* not reached */
}
/* /*
* hooray for linked lists * hooray for linked lists
*/ */
@ -484,58 +532,37 @@ delthreadinproc(Proc *p, _Thread *t)
l->tail = t->allprev; l->tail = t->allprev;
} }
void** Proc *_threadprocs;
procdata(void) Lock _threadprocslock;
{ static Proc *_threadprocstail;
return &proc()->udata;
}
static int threadargc;
static char **threadargv;
int mainstacksize;
static void static void
threadmainstart(void *v) addproc(Proc *p)
{ {
USED(v); lock(&_threadprocslock);
threadmain(threadargc, threadargv); if(_threadprocstail){
_threadprocstail->next = p;
p->prev = _threadprocstail;
}else{
_threadprocs = p;
p->prev = nil;
}
_threadprocstail = p;
p->next = nil;
unlock(&_threadprocslock);
} }
extern Jmp *(*_notejmpbuf)(void); static void
static Jmp* delproc(Proc *p)
threadnotejmp(void)
{ {
return &proc()->sigjmp; lock(&_threadprocslock);
} if(p->prev)
p->prev->next = p->next;
int else
main(int argc, char **argv) _threadprocs = p->next;
{ if(p->next)
Proc *p; p->next->prev = p->prev;
else
threadargc = argc; _threadprocstail = p->prev;
threadargv = argv; unlock(&_threadprocslock);
/*
* Install locking routines into C library.
*/
_lock = _threadlock;
_unlock = _threadunlock;
_qlock = threadqlock;
_qunlock = threadqunlock;
_rlock = threadrlock;
_runlock = threadrunlock;
_wlock = threadwlock;
_wunlock = threadwunlock;
_rsleep = threadrsleep;
_rwakeup = threadrwakeup;
_notejmpbuf = threadnotejmp;
_pthreadinit();
p = procalloc();
if(mainstacksize == 0)
mainstacksize = 65536;
_threadcreate(p, threadmainstart, nil, mainstacksize);
scheduler(p);
return 0; /* not reached */
} }

View file

@ -42,7 +42,11 @@ struct _Procrendez
{ {
Lock *l; Lock *l;
int asleep; int asleep;
#ifdef PLAN9PORT_USING_PTHREADS
pthread_cond_t cond; pthread_cond_t cond;
#else
int pid;
#endif
}; };
extern void _procsleep(_Procrendez*); extern void _procsleep(_Procrendez*);
@ -50,7 +54,14 @@ extern void _procwakeup(_Procrendez*);
struct Proc struct Proc
{ {
pthread_t tid; Proc *next;
Proc *prev;
char msg[128];
#ifdef PLAN9PORT_USING_PTHREADS
pthread_t osprocid;
#else
uint osprocid;
#endif
Lock lock; Lock lock;
_Thread *thread; _Thread *thread;
_Threadlist runqueue; _Threadlist runqueue;
@ -63,10 +74,12 @@ struct Proc
Jmp sigjmp; Jmp sigjmp;
}; };
extern Proc *xxx;
#define proc() _threadproc() #define proc() _threadproc()
#define setproc(p) _threadsetproc(p) #define setproc(p) _threadsetproc(p)
extern Proc *_threadprocs;
extern Lock _threadprocslock;
extern void _procstart(Proc*, void (*fn)(Proc*)); 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);