Blob


1 #ifndef _THREAD_H_
2 #define _THREAD_H_ 1
3 #if defined(__cplusplus)
4 extern "C" {
5 #endif
7 AUTOLIB(thread)
9 /*
10 * basic procs and threads
11 */
12 int proccreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
13 int threadcreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
14 void threadexits(char *);
15 void threadexitsall(char *);
16 void threadsetname(char*, ...);
17 void threadsetstate(char*, ...);
18 char *threadgetname(void);
19 int threadyield(void);
20 int threadidle(void);
21 void _threadready(_Thread*);
22 void _threadswitch(void);
23 void _threadsetsysproc(void);
24 void _threadsleep(Rendez*);
25 _Thread *_threadwakeup(Rendez*);
26 #define yield threadyield
27 int threadid(void);
28 void _threadpin(void);
29 void _threadunpin(void);
31 /*
32 * I am tired of making this mistake.
33 */
34 #define exits do_not_call_exits_in_threaded_programs
35 #define _exits do_not_call__exits_in_threaded_programs
39 /*
40 * signals
41 */
42 void threadnotify(int(*f)(void*,char*), int);
44 /*
45 * daemonize
46 *
47 void threaddaemonize(void);
48 */
50 /*
51 * per proc and thread data
52 */
53 void **procdata(void);
54 void **threaddata(void);
56 /*
57 * supplied by user instead of main.
58 * mainstacksize is size of stack allocated to run threadmain
59 */
60 void threadmain(int argc, char *argv[]);
61 extern int mainstacksize;
63 /*
64 * channel communication
65 */
66 typedef struct Alt Alt;
67 typedef struct _Altarray _Altarray;
68 typedef struct Channel Channel;
70 enum
71 {
72 CHANEND,
73 CHANSND,
74 CHANRCV,
75 CHANNOP,
76 CHANNOBLK
77 };
79 struct Alt
80 {
81 Channel *c;
82 void *v;
83 uint op;
84 _Thread *thread;
85 Alt *xalt;
86 };
88 struct _Altarray
89 {
90 Alt **a;
91 uint n;
92 uint m;
93 };
95 struct Channel
96 {
97 uint bufsize;
98 uint elemsize;
99 uchar *buf;
100 uint nbuf;
101 uint off;
102 _Altarray asend;
103 _Altarray arecv;
104 char *name;
105 };
107 /* [Edit .+1,./^$/ |cfn -h $PLAN9/src/libthread/channel.c] */
108 int chanalt(Alt *alts);
109 Channel* chancreate(int elemsize, int elemcnt);
110 void chanfree(Channel *c);
111 int chaninit(Channel *c, int elemsize, int elemcnt);
112 int channbrecv(Channel *c, void *v);
113 void* channbrecvp(Channel *c);
114 ulong channbrecvul(Channel *c);
115 int channbsend(Channel *c, void *v);
116 int channbsendp(Channel *c, void *v);
117 int channbsendul(Channel *c, ulong v);
118 int chanrecv(Channel *c, void *v);
119 void* chanrecvp(Channel *c);
120 ulong chanrecvul(Channel *c);
121 int chansend(Channel *c, void *v);
122 int chansendp(Channel *c, void *v);
123 int chansendul(Channel *c, ulong v);
124 void chansetname(Channel *c, char *fmt, ...);
126 #define alt chanalt
127 #define nbrecv channbrecv
128 #define nbrecvp channbrecvp
129 #define nbrecvul channbrecvul
130 #define nbsend channbsend
131 #define nbsendp channbsendp
132 #define nbsendul channbsendul
133 #define recv chanrecv
134 #define recvp chanrecvp
135 #define recvul chanrecvul
136 #define send chansend
137 #define sendp chansendp
138 #define sendul chansendul
140 /*
141 * reference counts
142 */
143 typedef struct Ref Ref;
145 struct Ref {
146 Lock lock;
147 long ref;
148 };
150 long decref(Ref *r);
151 long incref(Ref *r);
153 /*
154 * slave i/o processes
155 */
156 typedef struct Ioproc Ioproc;
158 /* [Edit .+1,/^$/ |cfn -h $PLAN9/src/libthread/io*.c] */
159 void closeioproc(Ioproc *io);
160 long iocall(Ioproc *io, long (*op)(va_list*), ...);
161 int ioclose(Ioproc *io, int fd);
162 int iodial(Ioproc *io, char *addr, char *local, char *dir, int *cdfp);
163 void iointerrupt(Ioproc *io);
164 int ioopen(Ioproc *io, char *path, int mode);
165 Ioproc* ioproc(void);
166 long ioread(Ioproc *io, int fd, void *a, long n);
167 int ioread9pmsg(Ioproc*, int, void*, int);
168 long ioreadn(Ioproc *io, int fd, void *a, long n);
169 int iorecvfd(Ioproc *, int);
170 int iosendfd(Ioproc*, int, int);
171 int iosleep(Ioproc *io, long n);
172 long iowrite(Ioproc *io, int fd, void *a, long n);
174 /*
175 * exec external programs
176 */
177 void threadexec(Channel*, int[3], char*, char *[]);
178 void threadexecl(Channel*, int[3], char*, ...);
179 int threadspawn(int[3], char*, char*[]);
180 int threadspawnl(int[3], char*, ...);
181 Channel* threadwaitchan(void);
183 /*
184 * alternate interface to threadwaitchan - don't use both!
185 */
186 Waitmsg* procwait(int pid);
188 #if defined(__cplusplus)
190 #endif
191 #endif /* _THREADH_ */