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