Blob


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 #include <ucontext.h>
9 #include <sys/utsname.h>
10 #include "libc.h"
11 #include "thread.h"
13 #if defined(__FreeBSD__) && __FreeBSD_version < 500000
14 extern int getcontext(ucontext_t*);
15 extern void setcontext(ucontext_t*);
16 extern int swapcontext(ucontext_t*, ucontext_t*);
17 extern void makecontext(ucontext_t*, void(*)(), int, ...);
18 #endif
20 #if defined(__APPLE__)
21 # define mcontext libthread_mcontext
22 # define mcontext_t libthread_mcontext_t
23 # define ucontext libthread_ucontext
24 # define ucontext_t libthread_ucontext_t
25 # include "Darwin-ucontext.h"
26 #endif
28 typedef struct Context Context;
29 typedef struct Execjob Execjob;
30 typedef struct Proc Proc;
31 typedef struct _Procrendez _Procrendez;
33 typedef struct Jmp Jmp;
34 struct Jmp
35 {
36 p9jmp_buf b;
37 };
39 enum
40 {
41 STACK = 8192
42 };
44 struct Context
45 {
46 ucontext_t uc;
47 };
49 struct Execjob
50 {
51 int *fd;
52 char *cmd;
53 char **argv;
54 Channel *c;
55 };
57 struct _Thread
58 {
59 _Thread *next;
60 _Thread *prev;
61 _Thread *allnext;
62 _Thread *allprev;
63 Context context;
64 uint id;
65 uchar *stk;
66 uint stksize;
67 int exiting;
68 void (*startfn)(void*);
69 void *startarg;
70 Proc *proc;
71 char name[256];
72 char state[256];
73 };
75 struct _Procrendez
76 {
77 Lock *l;
78 int asleep;
79 #ifdef PLAN9PORT_USING_PTHREADS
80 pthread_cond_t cond;
81 #else
82 int pid;
83 #endif
84 };
86 extern void _procsleep(_Procrendez*);
87 extern void _procwakeup(_Procrendez*);
88 extern void _procwakeupandunlock(_Procrendez*);
90 struct Proc
91 {
92 Proc *next;
93 Proc *prev;
94 char msg[128];
95 #ifdef PLAN9PORT_USING_PTHREADS
96 pthread_t osprocid;
97 #else
98 int osprocid;
99 #endif
100 Lock lock;
101 int nswitch;
102 _Thread *thread;
103 _Threadlist runqueue;
104 _Threadlist allthreads;
105 uint nthread;
106 uint sysproc;
107 _Procrendez runrend;
108 Context schedcontext;
109 void *udata;
110 Jmp sigjmp;
111 int mainproc;
112 };
114 #define proc() _threadproc()
115 #define setproc(p) _threadsetproc(p)
117 extern Proc *_threadprocs;
118 extern Lock _threadprocslock;
119 extern Proc *_threadexecproc;
120 extern Channel *_threadexecchan;
121 extern QLock _threadexeclock;
122 extern Channel *_dowaitchan;
124 extern void _procstart(Proc*, void (*fn)(Proc*));
125 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
126 extern void _threadexit(void);
127 extern Proc *_threadproc(void);
128 extern void _threadsetproc(Proc*);
129 extern int _threadlock(Lock*, int, ulong);
130 extern void _threadunlock(Lock*, ulong);
131 extern void _pthreadinit(void);
132 extern int _threadspawn(int*, char*, char**);
133 extern int _runthreadspawn(int*, char*, char**);
134 extern void _threadsetupdaemonize(void);
135 extern void _threaddodaemonize(char*);
136 extern void _threadpexit(void);
137 extern void _threaddaemonize(void);