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