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 # include "power-ucontext.h"
32 extern pid_t rfork_thread(int, void*, int(*)(void*), void*);
33 #endif
35 typedef struct Context Context;
36 typedef struct Execjob Execjob;
37 typedef struct Proc Proc;
38 typedef struct _Procrendez _Procrendez;
40 typedef struct Jmp Jmp;
41 struct Jmp
42 {
43 p9jmp_buf b;
44 };
46 enum
47 {
48 STACK = 8192
49 };
51 struct Context
52 {
53 ucontext_t uc;
54 };
56 struct Execjob
57 {
58 int *fd;
59 char *cmd;
60 char **argv;
61 Channel *c;
62 };
64 struct _Thread
65 {
66 _Thread *next;
67 _Thread *prev;
68 _Thread *allnext;
69 _Thread *allprev;
70 Context context;
71 uint id;
72 uchar *stk;
73 uint stksize;
74 int exiting;
75 void (*startfn)(void*);
76 void *startarg;
77 Proc *proc;
78 char name[256];
79 char state[256];
80 void *udata;
81 };
83 struct _Procrendez
84 {
85 Lock *l;
86 int asleep;
87 #ifdef PLAN9PORT_USING_PTHREADS
88 pthread_cond_t cond;
89 #else
90 int pid;
91 #endif
92 };
94 extern void _procsleep(_Procrendez*);
95 extern void _procwakeup(_Procrendez*);
96 extern void _procwakeupandunlock(_Procrendez*);
98 struct Proc
99 {
100 Proc *next;
101 Proc *prev;
102 char msg[128];
103 #ifdef PLAN9PORT_USING_PTHREADS
104 pthread_t osprocid;
105 #else
106 int osprocid;
107 #endif
108 Lock lock;
109 int nswitch;
110 _Thread *thread;
111 _Threadlist runqueue;
112 _Threadlist allthreads;
113 uint nthread;
114 uint sysproc;
115 _Procrendez runrend;
116 Context schedcontext;
117 void *udata;
118 Jmp sigjmp;
119 int mainproc;
120 };
122 #define proc() _threadproc()
123 #define setproc(p) _threadsetproc(p)
125 extern Proc *_threadprocs;
126 extern Lock _threadprocslock;
127 extern Proc *_threadexecproc;
128 extern Channel *_threadexecchan;
129 extern QLock _threadexeclock;
130 extern Channel *_dowaitchan;
132 extern void _procstart(Proc*, void (*fn)(Proc*));
133 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
134 extern void _threadexit(void);
135 extern Proc *_threadproc(void);
136 extern void _threadsetproc(Proc*);
137 extern int _threadlock(Lock*, int, ulong);
138 extern void _threadunlock(Lock*, ulong);
139 extern void _pthreadinit(void);
140 extern int _threadspawn(int*, char*, char**);
141 extern int _runthreadspawn(int*, char*, char**);
142 extern void _threadsetupdaemonize(void);
143 extern void _threaddodaemonize(char*);
144 extern void _threadpexit(void);
145 extern void _threaddaemonize(void);