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