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 void *udata;
74 };
76 struct _Procrendez
77 {
78 Lock *l;
79 int asleep;
80 #ifdef PLAN9PORT_USING_PTHREADS
81 pthread_cond_t cond;
82 #else
83 int pid;
84 #endif
85 };
87 extern void _procsleep(_Procrendez*);
88 extern void _procwakeup(_Procrendez*);
89 extern void _procwakeupandunlock(_Procrendez*);
91 struct Proc
92 {
93 Proc *next;
94 Proc *prev;
95 char msg[128];
96 #ifdef PLAN9PORT_USING_PTHREADS
97 pthread_t osprocid;
98 #else
99 int osprocid;
100 #endif
101 Lock lock;
102 int nswitch;
103 _Thread *thread;
104 _Threadlist runqueue;
105 _Threadlist allthreads;
106 uint nthread;
107 uint sysproc;
108 _Procrendez runrend;
109 Context schedcontext;
110 void *udata;
111 Jmp sigjmp;
112 int mainproc;
113 };
115 #define proc() _threadproc()
116 #define setproc(p) _threadsetproc(p)
118 extern Proc *_threadprocs;
119 extern Lock _threadprocslock;
120 extern Proc *_threadexecproc;
121 extern Channel *_threadexecchan;
122 extern QLock _threadexeclock;
123 extern Channel *_dowaitchan;
125 extern void _procstart(Proc*, void (*fn)(Proc*));
126 extern _Thread *_threadcreate(Proc*, void(*fn)(void*), void*, uint);
127 extern void _threadexit(void);
128 extern Proc *_threadproc(void);
129 extern void _threadsetproc(Proc*);
130 extern int _threadlock(Lock*, int, ulong);
131 extern void _threadunlock(Lock*, ulong);
132 extern void _pthreadinit(void);
133 extern int _threadspawn(int*, char*, char**);
134 extern int _runthreadspawn(int*, char*, char**);
135 extern void _threadsetupdaemonize(void);
136 extern void _threaddodaemonize(char*);
137 extern void _threadpexit(void);
138 extern void _threaddaemonize(void);