plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

threadimpl.h (2562B)


      1 #include "u.h"
      2 #include <errno.h>
      3 #include <sys/time.h>
      4 #include <sys/types.h>
      5 #include <sys/wait.h>
      6 #include <sched.h>
      7 #include <signal.h>
      8 #if !defined(__OpenBSD__)
      9 #	if defined(__APPLE__)
     10 #		define _XOPEN_SOURCE    /* for Snow Leopard */
     11 #	endif
     12 #endif
     13 #include <sys/utsname.h>
     14 #include "libc.h"
     15 #include "thread.h"
     16 
     17 typedef struct Execjob Execjob;
     18 typedef struct Proc Proc;
     19 typedef struct _Procrendez _Procrendez;
     20 
     21 typedef struct Jmp Jmp;
     22 struct Jmp
     23 {
     24 	p9jmp_buf b;
     25 };
     26 
     27 enum
     28 {
     29 	STACK = 8192
     30 };
     31 
     32 struct Execjob
     33 {
     34 	int *fd;
     35 	char *cmd;
     36 	char **argv;
     37 	char *dir;
     38 	Channel *c;
     39 };
     40 
     41 struct _Procrendez
     42 {
     43 	Lock		*l;
     44 	int		asleep;
     45 	pthread_cond_t	cond;
     46 };
     47 
     48 struct _Thread
     49 {
     50 	_Thread	*next;
     51 	_Thread	*prev;
     52 	_Thread	*allnext;
     53 	_Thread	*allprev;
     54 	void	(*startfn)(void*);
     55 	void	*startarg;
     56 	uint	id;
     57 	pthread_t	osprocid;
     58 	uchar	*stk;
     59 	uint	stksize;
     60 	int		exiting;
     61 	int		mainthread;
     62 	Proc	*proc;
     63 	char	name[256];
     64 	char	state[256];
     65 	void *udata;
     66 	Alt	*alt;
     67 	_Procrendez schedrend;
     68 };
     69 
     70 extern	void	_procsleep(_Procrendez*);
     71 extern	void	_procwakeup(_Procrendez*);
     72 extern	void	_procwakeupandunlock(_Procrendez*);
     73 
     74 struct Proc
     75 {
     76 	Proc		*next;
     77 	Proc		*prev;
     78 	char		msg[128];
     79 	pthread_t	osprocid;
     80 	Lock		lock;
     81 	int			nswitch;
     82 	_Thread		*thread0;
     83 	_Thread		*thread;
     84 	_Thread		*pinthread;
     85 	_Threadlist	runqueue;
     86 	_Threadlist	idlequeue;
     87 	_Threadlist	allthreads;
     88 	uint		nthread;
     89 	uint		sysproc;
     90 	_Procrendez	runrend;
     91 	_Thread	*schedthread;
     92 	void		*udata;
     93 	Jmp		sigjmp;
     94 	int		mainproc;
     95 };
     96 
     97 #define proc() _threadproc()
     98 
     99 extern Proc *_threadprocs;
    100 extern Lock _threadprocslock;
    101 extern Proc *_threadexecproc;
    102 extern Channel *_threadexecchan;
    103 extern QLock _threadexeclock;
    104 extern Channel *_dowaitchan;
    105 
    106 extern void _procstart(Proc*, void (*fn)(Proc*));
    107 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
    108 extern void _procexit(void);
    109 extern Proc *_threadproc(void);
    110 extern void _threadsetproc(Proc*);
    111 extern int _threadlock(Lock*, int, ulong);
    112 extern void _threadunlock(Lock*, ulong);
    113 extern void _pthreadinit(void);
    114 extern int _threadspawn(int*, char*, char**, char*);
    115 extern int _runthreadspawn(int*, char*, char**, char*);
    116 extern void _threadsetupdaemonize(void);
    117 extern void _threaddodaemonize(char*);
    118 extern void _threadpexit(void);
    119 extern void _threaddaemonize(void);
    120 extern void *_threadstkalloc(int);
    121 extern void _threadstkfree(void*, int);
    122 extern void _threadpthreadmain(Proc*, _Thread*);
    123 extern void _threadpthreadstart(Proc*, _Thread*);
    124 
    125 #define USPALIGN(ucp, align) \
    126 	(void*)((((uintptr)(ucp)->uc_stack.ss_sp+(ucp)->uc_stack.ss_size)-(align))&~((align)-1))