Blob


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