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 recv(Channel *c, void *v);
86 void* recvp(Channel *c);
87 unsigned long recvul(Channel *c);
88 int send(Channel *c, void *v);
89 int sendp(Channel *c, void *v);
90 int sendul(Channel *c, unsigned long v);
91 int threadcreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
92 int threadcreateidle(void (*f)(void*), void*, unsigned int);
93 void** threaddata(void);
94 void threadexits(char *);
95 void threadexitsall(char *);
96 void threadfdwait(int, int);
97 void threadfdwaitsetup(void);
98 int threadgetgrp(void); /* return thread group of current thread */
99 char* threadgetname(void);
100 void threadint(int); /* interrupt thread */
101 void threadintgrp(int); /* interrupt threads in grp */
102 void threadkill(int); /* kill thread */
103 void threadkillgrp(int); /* kill threads in group */
104 void threadmain(int argc, char *argv[]);
105 void threadfdnoblock(int);
106 void threadnonotes(void);
107 int threadnotify(int (*f)(void*, char*), int in);
108 int threadid(void);
109 int threadpid(int);
110 long threadread(int, void*, long);
111 long threadreadn(int, void*, long);
112 int threadread9pmsg(int, void*, uint);
113 int threadrecvfd(int);
114 long threadwrite(int, const void*, long);
115 int threadsendfd(int, int);
116 int threadsetgrp(int); /* set thread group, return old */
117 void threadsetname(char *name);
118 void threadsleep(int);
119 Channel* threadwaitchan(void);
120 int tprivalloc(void);
121 void tprivfree(int);
122 void **tprivaddr(int);
123 void yield(void);
125 long threadstack(void);
127 extern int mainstacksize;
129 /* slave I/O processes */
130 typedef struct Ioproc Ioproc;
132 Ioproc* ioproc(void);
133 void closeioproc(Ioproc*);
134 void iointerrupt(Ioproc*);
136 int ioclose(Ioproc*, int);
137 int iodial(Ioproc*, char*, char*, char*, int*);
138 int ioopen(Ioproc*, char*, int);
139 long ioread(Ioproc*, int, void*, long);
140 long ioreadn(Ioproc*, int, void*, long);
141 long iowrite(Ioproc*, int, void*, long);
142 int iosleep(Ioproc*, long);
144 long iocall(Ioproc*, long (*)(va_list*), ...);
145 void ioret(Ioproc*, int);
147 #if defined(__cplusplus)
149 #endif
150 #endif /* _THREADH_ */