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);
51 /*
52 * supplied by user instead of main.
53 * mainstacksize is size of stack allocated to run threadmain
54 */
55 void threadmain(int argc, char *argv[]);
56 extern int mainstacksize;
58 /*
59 * channel communication
60 */
61 typedef struct Alt Alt;
62 typedef struct _Altarray _Altarray;
63 typedef struct Channel Channel;
65 enum
66 {
67 CHANEND,
68 CHANSND,
69 CHANRCV,
70 CHANNOP,
71 CHANNOBLK,
72 };
74 struct Alt
75 {
76 void *v;
77 Channel *c;
78 uint op;
79 _Thread *thread;
80 Alt *xalt;
81 };
83 struct _Altarray
84 {
85 Alt **a;
86 uint n;
87 uint m;
88 };
90 struct Channel
91 {
92 uint bufsize;
93 uint elemsize;
94 uchar *buf;
95 uint nbuf;
96 uint off;
97 _Altarray asend;
98 _Altarray arecv;
99 char *name;
100 };
102 /* [Edit .+1,./^$/ |cfn -h $PLAN9/src/libthread/channel.c] */
103 int chanalt(Alt *alts);
104 Channel* chancreate(int elemsize, int elemcnt);
105 void chanfree(Channel *c);
106 int chaninit(Channel *c, int elemsize, int elemcnt);
107 int channbrecv(Channel *c, void *v);
108 void* channbrecvp(Channel *c);
109 ulong channbrecvul(Channel *c);
110 int channbsend(Channel *c, void *v);
111 int channbsendp(Channel *c, void *v);
112 int channbsendul(Channel *c, ulong v);
113 int chanrecv(Channel *c, void *v);
114 void* chanrecvp(Channel *c);
115 ulong chanrecvul(Channel *c);
116 int chansend(Channel *c, void *v);
117 int chansendp(Channel *c, void *v);
118 int chansendul(Channel *c, ulong v);
119 void chansetname(Channel *c, char *fmt, ...);
121 #define alt chanalt
122 #define nbrecv channbrecv
123 #define nbrecvp channbrecvp
124 #define nbrecvul channbrecvul
125 #define nbsend channbsend
126 #define nbsendp channbsendp
127 #define nbsendul channbsendul
128 #define recv chanrecv
129 #define recvp chanrecvp
130 #define recvul chanrecvul
131 #define send chansend
132 #define sendp chansendp
133 #define sendul chansendul
135 /*
136 * reference counts
137 */
138 typedef struct Ref Ref;
140 struct Ref {
141 Lock lock;
142 long ref;
143 };
145 long decref(Ref *r);
146 long incref(Ref *r);
148 /*
149 * slave i/o processes
150 */
151 typedef struct Ioproc Ioproc;
153 /* [Edit .+1,/^$/ |cfn -h $PLAN9/src/libthread/io*.c] */
154 void closeioproc(Ioproc *io);
155 long iocall(Ioproc *io, long (*op)(va_list*), ...);
156 int ioclose(Ioproc *io, int fd);
157 int iodial(Ioproc *io, char *addr, char *local, char *dir, int *cdfp);
158 void iointerrupt(Ioproc *io);
159 int ioopen(Ioproc *io, char *path, int mode);
160 Ioproc* ioproc(void);
161 long ioread(Ioproc *io, int fd, void *a, long n);
162 int ioread9pmsg(Ioproc*, int, void*, int);
163 long ioreadn(Ioproc *io, int fd, void *a, long n);
164 int iorecvfd(Ioproc *, int);
165 int iosendfd(Ioproc*, int, int);
166 int iosleep(Ioproc *io, long n);
167 long iowrite(Ioproc *io, int fd, void *a, long n);
169 /*
170 * exec external programs
171 */
172 void threadexec(Channel*, int[3], char*, char *[]);
173 void threadexecl(Channel*, int[3], char*, ...);
174 int threadspawn(int[3], char*, char*[]);
175 int threadspawnl(int[3], char*, ...);
176 Channel* threadwaitchan(void);
178 #if defined(__cplusplus)
180 #endif
181 #endif /* _THREADH_ */