Blob


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