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