2004-12-28 04:20:39 +00:00
|
|
|
#include "u.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <signal.h>
|
2004-12-25 21:57:50 +00:00
|
|
|
#include <ucontext.h>
|
2004-12-28 04:20:39 +00:00
|
|
|
#include "libc.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
#if defined(__FreeBSD__) && !defined(__FreeBSD5__)
|
|
|
|
extern int getcontext(ucontext_t*);
|
|
|
|
extern void setcontext(ucontext_t*);
|
|
|
|
extern int swapcontext(ucontext_t*, ucontext_t*);
|
|
|
|
extern void makecontext(ucontext_t*, void(*)(), int, ...);
|
|
|
|
#endif
|
2004-12-25 21:57:50 +00:00
|
|
|
|
|
|
|
typedef struct Context Context;
|
2004-12-27 17:19:44 +00:00
|
|
|
typedef struct Execjob Execjob;
|
2004-12-25 21:57:50 +00:00
|
|
|
typedef struct Proc Proc;
|
|
|
|
typedef struct _Procrendez _Procrendez;
|
|
|
|
|
2004-12-27 00:13:48 +00:00
|
|
|
typedef struct Jmp Jmp;
|
|
|
|
struct Jmp
|
|
|
|
{
|
|
|
|
p9jmp_buf b;
|
|
|
|
};
|
|
|
|
|
2004-12-25 21:57:50 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
STACK = 8192
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
ucontext_t uc;
|
|
|
|
};
|
|
|
|
|
2004-12-27 17:19:44 +00:00
|
|
|
struct Execjob
|
|
|
|
{
|
|
|
|
int *fd;
|
|
|
|
char *cmd;
|
|
|
|
char **argv;
|
|
|
|
Channel *c;
|
|
|
|
};
|
|
|
|
|
2004-12-25 21:57:50 +00:00
|
|
|
struct _Thread
|
|
|
|
{
|
|
|
|
_Thread *next;
|
|
|
|
_Thread *prev;
|
|
|
|
_Thread *allnext;
|
|
|
|
_Thread *allprev;
|
|
|
|
Context context;
|
|
|
|
uint id;
|
|
|
|
uchar *stk;
|
|
|
|
uint stksize;
|
|
|
|
int exiting;
|
|
|
|
void (*startfn)(void*);
|
|
|
|
void *startarg;
|
|
|
|
Proc *proc;
|
|
|
|
char name[256];
|
|
|
|
char state[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _Procrendez
|
|
|
|
{
|
|
|
|
Lock *l;
|
|
|
|
int asleep;
|
2004-12-27 16:52:26 +00:00
|
|
|
#ifdef PLAN9PORT_USING_PTHREADS
|
2004-12-25 21:57:50 +00:00
|
|
|
pthread_cond_t cond;
|
2004-12-27 16:52:26 +00:00
|
|
|
#else
|
|
|
|
int pid;
|
|
|
|
#endif
|
2004-12-25 21:57:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern void _procsleep(_Procrendez*);
|
|
|
|
extern void _procwakeup(_Procrendez*);
|
|
|
|
|
|
|
|
struct Proc
|
|
|
|
{
|
2004-12-27 16:52:26 +00:00
|
|
|
Proc *next;
|
|
|
|
Proc *prev;
|
|
|
|
char msg[128];
|
|
|
|
#ifdef PLAN9PORT_USING_PTHREADS
|
|
|
|
pthread_t osprocid;
|
|
|
|
#else
|
|
|
|
uint osprocid;
|
|
|
|
#endif
|
2004-12-25 21:57:50 +00:00
|
|
|
Lock lock;
|
2004-12-27 19:11:33 +00:00
|
|
|
int nswitch;
|
2004-12-25 21:57:50 +00:00
|
|
|
_Thread *thread;
|
|
|
|
_Threadlist runqueue;
|
|
|
|
_Threadlist allthreads;
|
|
|
|
uint nthread;
|
|
|
|
uint sysproc;
|
|
|
|
_Procrendez runrend;
|
|
|
|
Context schedcontext;
|
|
|
|
void *udata;
|
2004-12-27 00:13:48 +00:00
|
|
|
Jmp sigjmp;
|
2004-12-25 21:57:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define proc() _threadproc()
|
|
|
|
#define setproc(p) _threadsetproc(p)
|
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
extern Proc *_threadprocs;
|
|
|
|
extern Lock _threadprocslock;
|
2004-12-27 18:21:58 +00:00
|
|
|
extern Proc *_threadexecproc;
|
|
|
|
extern Channel *_threadexecchan;
|
|
|
|
extern QLock _threadexeclock;
|
|
|
|
extern Channel *_dowaitchan;
|
2004-12-27 16:52:26 +00:00
|
|
|
|
2004-12-27 03:49:03 +00:00
|
|
|
extern void _procstart(Proc*, void (*fn)(Proc*));
|
2004-12-25 21:57:50 +00:00
|
|
|
extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
|
|
|
|
extern void _threadexit(void);
|
|
|
|
extern Proc *_threadproc(void);
|
|
|
|
extern void _threadsetproc(Proc*);
|
|
|
|
extern int _threadlock(Lock*, int, ulong);
|
|
|
|
extern void _threadunlock(Lock*, ulong);
|
2004-12-27 00:13:48 +00:00
|
|
|
extern void _pthreadinit(void);
|
2004-12-27 17:19:44 +00:00
|
|
|
extern int _threadspawn(int*, char*, char**);
|
|
|
|
extern int _runthreadspawn(int*, char*, char**);
|
2004-12-28 01:35:38 +00:00
|
|
|
extern void _threadsetupdaemonize(void);
|
|
|
|
extern void _threaddodaemonize(char*);
|