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);
29 /*
30 * I am tired of making this mistake.
31 */
32 #define exits do_not_call_exits_in_threaded_programs
33 #define _exits do_not_call__exits_in_threaded_programs
37 /*
38 * signals
39 */
40 void threadnotify(int(*f)(void*,char*), int);
42 /*
43 * daemonize
44 *
45 void threaddaemonize(void);
46 */
48 /*
49 * per proc and thread data
50 */
51 void **procdata(void);
52 void **threaddata(void);
54 /*
55 * supplied by user instead of main.
56 * mainstacksize is size of stack allocated to run threadmain
57 */
58 void threadmain(int argc, char *argv[]);
59 extern int mainstacksize;
61 /*
62 * channel communication
63 */
64 typedef struct Alt Alt;
65 typedef struct _Altarray _Altarray;
66 typedef struct Channel Channel;
68 enum
69 {
70 CHANEND,
71 CHANSND,
72 CHANRCV,
73 CHANNOP,
74 CHANNOBLK
75 };
77 struct Alt
78 {
79 Channel *c;
80 void *v;
81 uint op;
82 _Thread *thread;
83 Alt *xalt;
84 };
86 struct _Altarray
87 {
88 Alt **a;
89 uint n;
90 uint m;
91 };
93 struct Channel
94 {
95 uint bufsize;
96 uint elemsize;
97 uchar *buf;
98 uint nbuf;
99 uint off;
100 _Altarray asend;
101 _Altarray arecv;
102 char *name;
103 };
105 /* [Edit .+1,./^$/ |cfn -h $PLAN9/src/libthread/channel.c] */
106 int chanalt(Alt *alts);
107 Channel* chancreate(int elemsize, int elemcnt);
108 void chanfree(Channel *c);
109 int chaninit(Channel *c, int elemsize, int elemcnt);
110 int channbrecv(Channel *c, void *v);
111 void* channbrecvp(Channel *c);
112 ulong channbrecvul(Channel *c);
113 int channbsend(Channel *c, void *v);
114 int channbsendp(Channel *c, void *v);
115 int channbsendul(Channel *c, ulong v);
116 int chanrecv(Channel *c, void *v);
117 void* chanrecvp(Channel *c);
118 ulong chanrecvul(Channel *c);
119 int chansend(Channel *c, void *v);
120 int chansendp(Channel *c, void *v);
121 int chansendul(Channel *c, ulong v);
122 void chansetname(Channel *c, char *fmt, ...);
124 #define alt chanalt
125 #define nbrecv channbrecv
126 #define nbrecvp channbrecvp
127 #define nbrecvul channbrecvul
128 #define nbsend channbsend
129 #define nbsendp channbsendp
130 #define nbsendul channbsendul
131 #define recv chanrecv
132 #define recvp chanrecvp
133 #define recvul chanrecvul
134 #define send chansend
135 #define sendp chansendp
136 #define sendul chansendul
138 /*
139 * reference counts
140 */
141 typedef struct Ref Ref;
143 struct Ref {
144 Lock lock;
145 long ref;
146 };
148 long decref(Ref *r);
149 long incref(Ref *r);
151 /*
152 * slave i/o processes
153 */
154 typedef struct Ioproc Ioproc;
156 /* [Edit .+1,/^$/ |cfn -h $PLAN9/src/libthread/io*.c] */
157 void closeioproc(Ioproc *io);
158 long iocall(Ioproc *io, long (*op)(va_list*), ...);
159 int ioclose(Ioproc *io, int fd);
160 int iodial(Ioproc *io, char *addr, char *local, char *dir, int *cdfp);
161 void iointerrupt(Ioproc *io);
162 int ioopen(Ioproc *io, char *path, int mode);
163 Ioproc* ioproc(void);
164 long ioread(Ioproc *io, int fd, void *a, long n);
165 int ioread9pmsg(Ioproc*, int, void*, int);
166 long ioreadn(Ioproc *io, int fd, void *a, long n);
167 int iorecvfd(Ioproc *, int);
168 int iosendfd(Ioproc*, int, int);
169 int iosleep(Ioproc *io, long n);
170 long iowrite(Ioproc *io, int fd, void *a, long n);
172 /*
173 * exec external programs
174 */
175 void threadexec(Channel*, int[3], char*, char *[]);
176 void threadexecl(Channel*, int[3], char*, ...);
177 int threadspawn(int[3], char*, char*[]);
178 int threadspawnl(int[3], char*, ...);
179 Channel* threadwaitchan(void);
181 /*
182 * alternate interface to threadwaitchan - don't use both!
183 */
184 Waitmsg* procwait(int pid);
186 #if defined(__cplusplus)
188 #endif
189 #endif /* _THREADH_ */