Blob


1 #ifndef _THREAD_H_
2 #define _THREAD_H_ 1
3 #if defined(__cplusplus)
4 extern "C" {
5 #endif
7 /* avoid conflicts with socket library */
8 #undef send
9 #define send _threadsend
10 #undef recv
11 #define recv _threadrecv
13 typedef struct Alt Alt;
14 typedef struct Channel Channel;
15 typedef struct Ref Ref;
17 /* Channel structure. S is the size of the buffer. For unbuffered channels
18 * s is zero. v is an array of s values. If s is zero, v is unused.
19 * f and n represent the state of the queue pointed to by v.
20 */
22 enum {
23 Nqwds = 2,
24 Nqshift = 5, // 2log #of bits in long
25 Nqmask = - 1,
26 Nqbits = (1 << Nqshift) * 2,
27 };
29 struct Channel {
30 int s; // Size of the channel (may be zero)
31 unsigned int f; // Extraction point (insertion pt: (f + n) % s)
32 unsigned int n; // Number of values in the channel
33 int e; // Element size
34 int freed; // Set when channel is being deleted
35 volatile Alt **qentry; // Receivers/senders waiting (malloc)
36 volatile int nentry; // # of entries malloc-ed
37 unsigned char v[1]; // Array of s values in the channel
38 };
41 /* Channel operations for alt: */
42 typedef enum {
43 CHANEND,
44 CHANSND,
45 CHANRCV,
46 CHANNOP,
47 CHANNOBLK,
48 } ChanOp;
50 struct Alt {
51 Channel *c; /* channel */
52 void *v; /* pointer to value */
53 ChanOp op; /* operation */
55 /* the next variables are used internally to alt
56 * they need not be initialized
57 */
58 Channel **tag; /* pointer to rendez-vous tag */
59 int entryno; /* entry number */
60 };
62 struct Ref {
63 Lock lk;
64 long ref;
65 };
67 int alt(Alt alts[]);
68 Channel* chancreate(int elemsize, int bufsize);
69 int chaninit(Channel *c, int elemsize, int elemcnt);
70 void chanfree(Channel *c);
71 int chanprint(Channel *, char *, ...);
72 long decref(Ref *r); /* returns 0 iff value is now zero */
73 void incref(Ref *r);
74 int nbrecv(Channel *c, void *v);
75 void* nbrecvp(Channel *c);
76 unsigned long nbrecvul(Channel *c);
77 int nbsend(Channel *c, void *v);
78 int nbsendp(Channel *c, void *v);
79 int nbsendul(Channel *c, unsigned long v);
80 int proccreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
81 int procrfork(void (*f)(void *arg), void *arg, unsigned int stacksize, int flag);
82 void** procdata(void);
83 void threadexec(Channel *, int[3], char *, char *[]);
84 void threadexecl(Channel *, int[3], char *, ...);
85 int threadspawn(int[3], char*, char*[]);
86 int recv(Channel *c, void *v);
87 void* recvp(Channel *c);
88 unsigned long recvul(Channel *c);
89 int send(Channel *c, void *v);
90 int sendp(Channel *c, void *v);
91 int sendul(Channel *c, unsigned long v);
92 int threadcreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
93 int threadcreateidle(void (*f)(void*), void*, unsigned int);
94 void** threaddata(void);
95 void threadexits(char *);
96 void threadexitsall(char *);
97 void threadfdwait(int, int);
98 void threadfdwaitsetup(void);
99 int threadgetgrp(void); /* return thread group of current thread */
100 char* threadgetname(void);
101 void threadint(int); /* interrupt thread */
102 void threadintgrp(int); /* interrupt threads in grp */
103 void threadkill(int); /* kill thread */
104 void threadkillgrp(int); /* kill threads in group */
105 void threadmain(int argc, char *argv[]);
106 void threadfdnoblock(int);
107 void threadnonotes(void);
108 int threadnotify(int (*f)(void*, char*), int in);
109 int threadid(void);
110 int threadpid(int);
111 long threadread(int, void*, long);
112 long threadreadn(int, void*, long);
113 int threadread9pmsg(int, void*, uint);
114 int threadrecvfd(int);
115 long threadwrite(int, const void*, long);
116 int threadsendfd(int, int);
117 int threadsetgrp(int); /* set thread group, return old */
118 void threadsetname(char *fmt, ...);
119 void threadsleep(int);
120 Channel* threadwaitchan(void);
121 int tprivalloc(void);
122 void tprivfree(int);
123 void **tprivaddr(int);
124 int yield(void);
126 long threadstack(void);
128 extern int mainstacksize;
130 /* slave I/O processes */
131 typedef struct Ioproc Ioproc;
133 Ioproc* ioproc(void);
134 void closeioproc(Ioproc*);
135 void iointerrupt(Ioproc*);
137 int ioclose(Ioproc*, int);
138 int iodial(Ioproc*, char*, char*, char*, int*);
139 int ioopen(Ioproc*, char*, int);
140 long ioread(Ioproc*, int, void*, long);
141 long ioreadn(Ioproc*, int, void*, long);
142 long iowrite(Ioproc*, int, void*, long);
143 int iosleep(Ioproc*, long);
145 long iocall(Ioproc*, long (*)(va_list*), ...);
146 void ioret(Ioproc*, int);
148 #if defined(__cplusplus)
150 #endif
151 #endif /* _THREADH_ */