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>
|
2005-07-19 11:43:12 +00:00
|
|
|
#if !defined(__OpenBSD__)
|
2009-09-11 13:51:50 -04:00
|
|
|
# if defined(__APPLE__)
|
|
|
|
|
# define _XOPEN_SOURCE /* for Snow Leopard */
|
|
|
|
|
# endif
|
2005-05-01 18:38:12 +00:00
|
|
|
# include <ucontext.h>
|
|
|
|
|
#endif
|
2005-01-17 21:30:05 +00:00
|
|
|
#include <sys/utsname.h>
|
2004-12-28 04:20:39 +00:00
|
|
|
#include "libc.h"
|
|
|
|
|
#include "thread.h"
|
|
|
|
|
|
2005-11-01 18:35:25 +00:00
|
|
|
#if defined(__FreeBSD__) && __FreeBSD__ < 5
|
2005-11-04 16:34:19 +00:00
|
|
|
extern int getmcontext(mcontext_t*);
|
|
|
|
|
extern void setmcontext(mcontext_t*);
|
|
|
|
|
#define setcontext(u) setmcontext(&(u)->uc_mcontext)
|
|
|
|
|
#define getcontext(u) getmcontext(&(u)->uc_mcontext)
|
2004-12-28 04:20:39 +00:00
|
|
|
extern int swapcontext(ucontext_t*, ucontext_t*);
|
|
|
|
|
extern void makecontext(ucontext_t*, void(*)(), int, ...);
|
|
|
|
|
#endif
|
2004-12-25 21:57:50 +00:00
|
|
|
|
2011-10-13 23:57:54 -04:00
|
|
|
#if defined(__APPLE__)
|
2007-11-05 13:14:52 -05:00
|
|
|
/*
|
|
|
|
|
* OS X before 10.5 (Leopard) does not provide
|
|
|
|
|
* swapcontext nor makecontext, so we have to use our own.
|
|
|
|
|
* In theory, Leopard does provide them, but when we use
|
|
|
|
|
* them, they seg fault. Maybe we're using them wrong.
|
|
|
|
|
* So just use our own versions, even on Leopard.
|
|
|
|
|
*/
|
2005-01-11 17:43:53 +00:00
|
|
|
# define mcontext libthread_mcontext
|
|
|
|
|
# define mcontext_t libthread_mcontext_t
|
|
|
|
|
# define ucontext libthread_ucontext
|
|
|
|
|
# define ucontext_t libthread_ucontext_t
|
2007-11-05 13:14:52 -05:00
|
|
|
# define swapcontext libthread_swapcontext
|
|
|
|
|
# define makecontext libthread_makecontext
|
2006-04-21 03:34:44 +00:00
|
|
|
# if defined(__i386__)
|
|
|
|
|
# include "386-ucontext.h"
|
2011-10-13 23:57:54 -04:00
|
|
|
# elif defined(__x86_64__)
|
|
|
|
|
# include "x86_64-ucontext.h"
|
2011-10-23 17:55:23 -04:00
|
|
|
# elif defined(__ppc__) || defined(__power__)
|
2006-04-21 03:34:44 +00:00
|
|
|
# include "power-ucontext.h"
|
2011-08-02 14:28:04 -04:00
|
|
|
# else
|
|
|
|
|
# error "unknown architecture"
|
2006-04-21 03:34:44 +00:00
|
|
|
# endif
|
2005-05-01 18:38:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(__OpenBSD__)
|
2005-07-20 23:29:56 +00:00
|
|
|
# define mcontext libthread_mcontext
|
|
|
|
|
# define mcontext_t libthread_mcontext_t
|
|
|
|
|
# define ucontext libthread_ucontext
|
|
|
|
|
# define ucontext_t libthread_ucontext_t
|
2005-07-19 11:43:12 +00:00
|
|
|
# if defined __i386__
|
|
|
|
|
# include "386-ucontext.h"
|
|
|
|
|
# else
|
|
|
|
|
# include "power-ucontext.h"
|
|
|
|
|
# endif
|
2005-05-01 18:38:12 +00:00
|
|
|
extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
|
2005-01-07 21:47:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
2007-05-01 10:51:23 +00:00
|
|
|
/* THIS DOES NOT WORK! Don't do this!
|
|
|
|
|
(At least, not on Solaris. Maybe this is right for Linux,
|
|
|
|
|
in which case it should say if defined(__linux__) && defined(__sun__),
|
|
|
|
|
but surely the latter would be defined(__sparc__).
|
|
|
|
|
|
2006-06-04 16:06:49 +00:00
|
|
|
#if defined(__sun__)
|
|
|
|
|
# define mcontext libthread_mcontext
|
|
|
|
|
# define mcontext_t libthread_mcontext_t
|
|
|
|
|
# define ucontext libthread_ucontext
|
|
|
|
|
# define ucontext_t libthread_ucontext_t
|
|
|
|
|
# include "sparc-ucontext.h"
|
|
|
|
|
#endif
|
2007-05-01 10:51:23 +00:00
|
|
|
*/
|
2006-06-04 16:06:49 +00:00
|
|
|
|
2005-11-04 18:09:35 +00:00
|
|
|
#if defined(__arm__)
|
2011-05-18 13:20:56 -04:00
|
|
|
int mygetmcontext(ulong*);
|
|
|
|
|
void mysetmcontext(const ulong*);
|
|
|
|
|
#define setcontext(u) mysetmcontext(&(u)->uc_mcontext.arm_r0)
|
|
|
|
|
#define getcontext(u) mygetmcontext(&(u)->uc_mcontext.arm_r0)
|
2005-11-04 18:09:35 +00:00
|
|
|
#endif
|
|
|
|
|
|
2006-06-04 16:06:49 +00:00
|
|
|
|
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;
|
2011-08-02 14:28:04 -04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
/*
|
|
|
|
|
* On Snow Leopard, etc., the context routines exist,
|
|
|
|
|
* so we use them, but apparently they write past the
|
|
|
|
|
* end of the ucontext_t. Sigh. We put some extra
|
|
|
|
|
* scratch space here for them.
|
|
|
|
|
*/
|
2011-10-13 23:57:54 -04:00
|
|
|
uchar buf[1024];
|
2011-08-02 14:28:04 -04:00
|
|
|
#endif
|
2004-12-25 21:57:50 +00:00
|
|
|
};
|
|
|
|
|
|
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;
|
2011-08-02 14:28:04 -04:00
|
|
|
void (*startfn)(void*);
|
|
|
|
|
void *startarg;
|
2004-12-25 21:57:50 +00:00
|
|
|
uint id;
|
|
|
|
|
uchar *stk;
|
|
|
|
|
uint stksize;
|
|
|
|
|
int exiting;
|
|
|
|
|
Proc *proc;
|
|
|
|
|
char name[256];
|
|
|
|
|
char state[256];
|
2005-03-18 18:56:17 +00:00
|
|
|
void *udata;
|
2007-08-22 11:14:30 -04:00
|
|
|
Alt *alt;
|
2004-12-25 21:57:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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*);
|
2005-01-06 23:43:42 +00:00
|
|
|
extern void _procwakeupandunlock(_Procrendez*);
|
2004-12-25 21:57:50 +00:00
|
|
|
|
|
|
|
|
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
|
2005-01-30 16:35:05 +00:00
|
|
|
int osprocid;
|
2004-12-27 16:52:26 +00:00
|
|
|
#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;
|
2006-06-26 05:49:10 +00:00
|
|
|
_Thread *pinthread;
|
2004-12-25 21:57:50 +00:00
|
|
|
_Threadlist runqueue;
|
2006-02-07 17:02:05 +00:00
|
|
|
_Threadlist idlequeue;
|
2004-12-25 21:57:50 +00:00
|
|
|
_Threadlist allthreads;
|
|
|
|
|
uint nthread;
|
|
|
|
|
uint sysproc;
|
|
|
|
|
_Procrendez runrend;
|
|
|
|
|
Context schedcontext;
|
|
|
|
|
void *udata;
|
2004-12-27 00:13:48 +00:00
|
|
|
Jmp sigjmp;
|
2005-02-14 18:58:56 +00:00
|
|
|
int mainproc;
|
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*);
|
2004-12-28 22:36:24 +00:00
|
|
|
extern void _threadpexit(void);
|
2005-01-04 22:23:24 +00:00
|
|
|
extern void _threaddaemonize(void);
|