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