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 long ref;
64 };
66 int alt(Alt alts[]);
67 Channel* chancreate(int elemsize, int bufsize);
68 int chaninit(Channel *c, int elemsize, int elemcnt);
69 void chanfree(Channel *c);
70 int chanprint(Channel *, char *, ...);
71 long decref(Ref *r); /* returns 0 iff value is now zero */
72 void incref(Ref *r);
73 int nbrecv(Channel *c, void *v);
74 void* nbrecvp(Channel *c);
75 unsigned long nbrecvul(Channel *c);
76 int nbsend(Channel *c, void *v);
77 int nbsendp(Channel *c, void *v);
78 int nbsendul(Channel *c, unsigned long v);
79 int proccreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
80 int procrfork(void (*f)(void *arg), void *arg, unsigned int stacksize, int flag);
81 void** procdata(void);
82 void procexec(Channel *, char *, char *[]);
83 void procexecl(Channel *, char *, ...);
84 int recv(Channel *c, void *v);
85 void* recvp(Channel *c);
86 unsigned long recvul(Channel *c);
87 int send(Channel *c, void *v);
88 int sendp(Channel *c, void *v);
89 int sendul(Channel *c, unsigned long v);
90 int threadcreate(void (*f)(void *arg), void *arg, unsigned int stacksize);
91 void** threaddata(void);
92 void threadexits(char *);
93 void threadexitsall(char *);
94 int threadgetgrp(void); /* return thread group of current thread */
95 char* threadgetname(void);
96 void threadint(int); /* interrupt thread */
97 void threadintgrp(int); /* interrupt threads in grp */
98 void threadkill(int); /* kill thread */
99 void threadkillgrp(int); /* kill threads in group */
100 void threadmain(int argc, char *argv[]);
101 void threadnonotes(void);
102 int threadnotify(int (*f)(void*, char*), int in);
103 int threadid(void);
104 int threadpid(int);
105 int threadsetgrp(int); /* set thread group, return old */
106 void threadsetname(char *name);
107 Channel* threadwaitchan(void);
108 int tprivalloc(void);
109 void tprivfree(int);
110 void **tprivaddr(int);
111 void yield(void);
113 long threadstack(void);
115 extern int mainstacksize;
117 /* slave I/O processes */
118 typedef struct Ioproc Ioproc;
120 Ioproc* ioproc(void);
121 void closeioproc(Ioproc*);
122 void iointerrupt(Ioproc*);
124 int ioclose(Ioproc*, int);
125 int iodial(Ioproc*, char*, char*, char*, int*);
126 int ioopen(Ioproc*, char*, int);
127 long ioread(Ioproc*, int, void*, long);
128 long ioreadn(Ioproc*, int, void*, long);
129 long iowrite(Ioproc*, int, void*, long);
130 int iosleep(Ioproc*, long);
132 long iocall(Ioproc*, long (*)(va_list*), ...);
133 void ioret(Ioproc*, int);
135 #if defined(__cplusplus)
137 #endif
138 #endif /* _THREADH_ */