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