Blame


1 b2cfc4e2 2003-09-30 devnull .TH THREAD 2
2 b2cfc4e2 2003-09-30 devnull .SH NAME
3 b2cfc4e2 2003-09-30 devnull alt,
4 b2cfc4e2 2003-09-30 devnull chancreate,
5 b2cfc4e2 2003-09-30 devnull chanfree,
6 b2cfc4e2 2003-09-30 devnull chaninit,
7 b2cfc4e2 2003-09-30 devnull chanprint,
8 b2cfc4e2 2003-09-30 devnull mainstacksize,
9 b2cfc4e2 2003-09-30 devnull proccreate,
10 b2cfc4e2 2003-09-30 devnull procdata,
11 b2cfc4e2 2003-09-30 devnull procexec,
12 b2cfc4e2 2003-09-30 devnull procexecl,
13 b2cfc4e2 2003-09-30 devnull procrfork,
14 b2cfc4e2 2003-09-30 devnull recv,
15 b2cfc4e2 2003-09-30 devnull recvp,
16 b2cfc4e2 2003-09-30 devnull recvul,
17 b2cfc4e2 2003-09-30 devnull send,
18 b2cfc4e2 2003-09-30 devnull sendp,
19 b2cfc4e2 2003-09-30 devnull sendul,
20 b2cfc4e2 2003-09-30 devnull nbrecv,
21 b2cfc4e2 2003-09-30 devnull nbrecvp,
22 b2cfc4e2 2003-09-30 devnull nbrecvul,
23 b2cfc4e2 2003-09-30 devnull nbsend,
24 b2cfc4e2 2003-09-30 devnull nbsendp,
25 b2cfc4e2 2003-09-30 devnull nbsendul,
26 b2cfc4e2 2003-09-30 devnull threadcreate,
27 b2cfc4e2 2003-09-30 devnull threaddata,
28 b2cfc4e2 2003-09-30 devnull threadexits,
29 b2cfc4e2 2003-09-30 devnull threadexitsall,
30 b2cfc4e2 2003-09-30 devnull threadgetgrp,
31 b2cfc4e2 2003-09-30 devnull threadgetname,
32 b2cfc4e2 2003-09-30 devnull threadint,
33 b2cfc4e2 2003-09-30 devnull threadintgrp,
34 b2cfc4e2 2003-09-30 devnull threadkill,
35 b2cfc4e2 2003-09-30 devnull threadkillgrp,
36 b2cfc4e2 2003-09-30 devnull threadmain,
37 b2cfc4e2 2003-09-30 devnull threadnotify,
38 b2cfc4e2 2003-09-30 devnull threadid,
39 b2cfc4e2 2003-09-30 devnull threadpid,
40 b2cfc4e2 2003-09-30 devnull threadsetgrp,
41 b2cfc4e2 2003-09-30 devnull threadsetname,
42 b2cfc4e2 2003-09-30 devnull threadwaitchan,
43 b2cfc4e2 2003-09-30 devnull yield \- thread and proc management
44 b2cfc4e2 2003-09-30 devnull .SH SYNOPSIS
45 b2cfc4e2 2003-09-30 devnull .PP
46 b2cfc4e2 2003-09-30 devnull .EX
47 b2cfc4e2 2003-09-30 devnull .ta 4n +4n +4n +4n +4n +4n +4n
48 b2cfc4e2 2003-09-30 devnull #include <u.h>
49 b2cfc4e2 2003-09-30 devnull #include <libc.h>
50 b2cfc4e2 2003-09-30 devnull #include <thread.h>
51 b2cfc4e2 2003-09-30 devnull .sp
52 b2cfc4e2 2003-09-30 devnull #define CHANEND 0
53 b2cfc4e2 2003-09-30 devnull #define CHANSND 1
54 b2cfc4e2 2003-09-30 devnull #define CHANRCV 2
55 b2cfc4e2 2003-09-30 devnull #define CHANNOP 3
56 b2cfc4e2 2003-09-30 devnull #define CHANNOBLK 4
57 b2cfc4e2 2003-09-30 devnull .sp
58 b2cfc4e2 2003-09-30 devnull .ta \w' 'u +\w'Channel 'u
59 b2cfc4e2 2003-09-30 devnull typedef struct Alt Alt;
60 b2cfc4e2 2003-09-30 devnull struct Alt {
61 b2cfc4e2 2003-09-30 devnull Channel *c;
62 b2cfc4e2 2003-09-30 devnull void *v;
63 b2cfc4e2 2003-09-30 devnull int op;
64 b2cfc4e2 2003-09-30 devnull Channel **tag;
65 b2cfc4e2 2003-09-30 devnull int entryno;
66 b2cfc4e2 2003-09-30 devnull };
67 b2cfc4e2 2003-09-30 devnull .fi
68 b2cfc4e2 2003-09-30 devnull .de XX
69 b2cfc4e2 2003-09-30 devnull .ift .sp 0.5
70 b2cfc4e2 2003-09-30 devnull .ifn .sp
71 b2cfc4e2 2003-09-30 devnull ..
72 b2cfc4e2 2003-09-30 devnull .PP
73 b2cfc4e2 2003-09-30 devnull .nf
74 b2cfc4e2 2003-09-30 devnull .ft L
75 b2cfc4e2 2003-09-30 devnull .ta \w'\fLChannel* 'u +4n +4n +4n +4n
76 b2cfc4e2 2003-09-30 devnull void threadmain(int argc, char *argv[])
77 b2cfc4e2 2003-09-30 devnull int mainstacksize
78 b2cfc4e2 2003-09-30 devnull int proccreate(void (*fn)(void*), void *arg, uint stacksize)
79 b2cfc4e2 2003-09-30 devnull int procrfork(void (*fn)(void*), void *arg, uint stacksize,
80 b2cfc4e2 2003-09-30 devnull int rforkflag)
81 b2cfc4e2 2003-09-30 devnull int threadcreate(void (*fn)(void*), void *arg, uint stacksize)
82 b2cfc4e2 2003-09-30 devnull void threadexits(char *status)
83 b2cfc4e2 2003-09-30 devnull void threadexitsall(char *status)
84 b2cfc4e2 2003-09-30 devnull void yield(void)
85 b2cfc4e2 2003-09-30 devnull .XX
86 b2cfc4e2 2003-09-30 devnull int threadid(void)
87 b2cfc4e2 2003-09-30 devnull int threadgrp(void)
88 b2cfc4e2 2003-09-30 devnull int threadsetgrp(int group)
89 b2cfc4e2 2003-09-30 devnull int threadpid(int id)
90 b2cfc4e2 2003-09-30 devnull .XX
91 b2cfc4e2 2003-09-30 devnull int threadint(int id)
92 b2cfc4e2 2003-09-30 devnull int threadintgrp(int group)
93 b2cfc4e2 2003-09-30 devnull int threadkill(int id)
94 b2cfc4e2 2003-09-30 devnull int threadkillgrp(int group)
95 b2cfc4e2 2003-09-30 devnull .XX
96 b2cfc4e2 2003-09-30 devnull void threadsetname(char *name)
97 b2cfc4e2 2003-09-30 devnull char* threadgetname(void)
98 b2cfc4e2 2003-09-30 devnull .XX
99 b2cfc4e2 2003-09-30 devnull void** threaddata(void)
100 b2cfc4e2 2003-09-30 devnull void** procdata(void)
101 b2cfc4e2 2003-09-30 devnull .XX
102 b2cfc4e2 2003-09-30 devnull int chaninit(Channel *c, int elsize, int nel)
103 b2cfc4e2 2003-09-30 devnull Channel* chancreate(int elsize, int nel)
104 b2cfc4e2 2003-09-30 devnull void chanfree(Channel *c)
105 b2cfc4e2 2003-09-30 devnull .XX
106 b2cfc4e2 2003-09-30 devnull int alt(Alt *alts)
107 b2cfc4e2 2003-09-30 devnull int recv(Channel *c, void *v)
108 b2cfc4e2 2003-09-30 devnull void* recvp(Channel *c)
109 b2cfc4e2 2003-09-30 devnull ulong recvul(Channel *c)
110 b2cfc4e2 2003-09-30 devnull int nbrecv(Channel *c, void *v)
111 b2cfc4e2 2003-09-30 devnull void* nbrecvp(Channel *c)
112 b2cfc4e2 2003-09-30 devnull ulong nbrecvul(Channel *c)
113 b2cfc4e2 2003-09-30 devnull int send(Channel *c, void *v)
114 b2cfc4e2 2003-09-30 devnull int sendp(Channel *c, void *v)
115 b2cfc4e2 2003-09-30 devnull int sendul(Channel *c, ulong v)
116 b2cfc4e2 2003-09-30 devnull int nbsend(Channel *c, void *v)
117 b2cfc4e2 2003-09-30 devnull int nbsendp(Channel *c, void *v)
118 b2cfc4e2 2003-09-30 devnull int nbsendul(Channel *c, ulong v)
119 b2cfc4e2 2003-09-30 devnull int chanprint(Channel *c, char *fmt, ...)
120 b2cfc4e2 2003-09-30 devnull .XX
121 b2cfc4e2 2003-09-30 devnull int procexecl(Channel *cpid, char *file, ...)
122 b2cfc4e2 2003-09-30 devnull int procexec(Channel *cpid, char *file, char *args[])
123 b2cfc4e2 2003-09-30 devnull Channel* threadwaitchan(void)
124 b2cfc4e2 2003-09-30 devnull .XX
125 b2cfc4e2 2003-09-30 devnull int threadnotify(int (*f)(void*, char*), int in)
126 b2cfc4e2 2003-09-30 devnull .EE
127 b2cfc4e2 2003-09-30 devnull .SH DESCRIPTION
128 b2cfc4e2 2003-09-30 devnull .PP
129 b2cfc4e2 2003-09-30 devnull The thread library provides parallel programming support similar to that
130 b2cfc4e2 2003-09-30 devnull of the languages
131 b2cfc4e2 2003-09-30 devnull Alef and Newsqueak.
132 b2cfc4e2 2003-09-30 devnull Threads
133 b2cfc4e2 2003-09-30 devnull and
134 b2cfc4e2 2003-09-30 devnull procs
135 b2cfc4e2 2003-09-30 devnull occupy a shared address space,
136 b2cfc4e2 2003-09-30 devnull communicating and synchronizing through
137 b2cfc4e2 2003-09-30 devnull .I channels
138 b2cfc4e2 2003-09-30 devnull and shared variables.
139 b2cfc4e2 2003-09-30 devnull .PP
140 b2cfc4e2 2003-09-30 devnull A
141 b2cfc4e2 2003-09-30 devnull .I proc
142 b2cfc4e2 2003-09-30 devnull is a Plan 9 process that contains one or more cooperatively scheduled
143 b2cfc4e2 2003-09-30 devnull .IR threads .
144 b2cfc4e2 2003-09-30 devnull Programs using threads must replace
145 b2cfc4e2 2003-09-30 devnull .I main
146 b2cfc4e2 2003-09-30 devnull by
147 b2cfc4e2 2003-09-30 devnull .IR threadmain .
148 b2cfc4e2 2003-09-30 devnull The thread library provides a
149 b2cfc4e2 2003-09-30 devnull .I main
150 b2cfc4e2 2003-09-30 devnull function that sets up a proc with a single thread executing
151 b2cfc4e2 2003-09-30 devnull .I threadmain
152 b2cfc4e2 2003-09-30 devnull on a stack of size
153 b2cfc4e2 2003-09-30 devnull .I mainstacksize
154 b2cfc4e2 2003-09-30 devnull (default eight kilobytes).
155 b2cfc4e2 2003-09-30 devnull To set
156 b2cfc4e2 2003-09-30 devnull .IR mainstacksize ,
157 b2cfc4e2 2003-09-30 devnull declare a global variable
158 b2cfc4e2 2003-09-30 devnull initialized to the desired value
159 b2cfc4e2 2003-09-30 devnull .RI ( e.g. ,
160 b2cfc4e2 2003-09-30 devnull .B int
161 b2cfc4e2 2003-09-30 devnull .B mainstacksize
162 b2cfc4e2 2003-09-30 devnull .B =
163 b2cfc4e2 2003-09-30 devnull .BR 1024 ).
164 b2cfc4e2 2003-09-30 devnull .PP
165 b2cfc4e2 2003-09-30 devnull .I Threadcreate
166 b2cfc4e2 2003-09-30 devnull creates a new thread in the calling proc, returning a unique integer
167 b2cfc4e2 2003-09-30 devnull identifying the thread; the thread
168 b2cfc4e2 2003-09-30 devnull executes
169 b2cfc4e2 2003-09-30 devnull .I fn(arg)
170 b2cfc4e2 2003-09-30 devnull on a stack of size
171 b2cfc4e2 2003-09-30 devnull .IR stacksize .
172 b2cfc4e2 2003-09-30 devnull Thread stacks are allocated in shared memory, making it valid to pass
173 b2cfc4e2 2003-09-30 devnull pointers to stack variables between threads and procs.
174 b2cfc4e2 2003-09-30 devnull .I Procrfork
175 b2cfc4e2 2003-09-30 devnull creates a new proc, and inside that proc creates
176 b2cfc4e2 2003-09-30 devnull a single thread as
177 b2cfc4e2 2003-09-30 devnull .I threadcreate
178 b2cfc4e2 2003-09-30 devnull would,
179 b2cfc4e2 2003-09-30 devnull returning the id of the created thread.
180 b2cfc4e2 2003-09-30 devnull .I Procrfork
181 b2cfc4e2 2003-09-30 devnull creates the new proc by calling
182 b2cfc4e2 2003-09-30 devnull .B rfork
183 b2cfc4e2 2003-09-30 devnull (see
184 b2cfc4e2 2003-09-30 devnull .IR fork (2))
185 b2cfc4e2 2003-09-30 devnull with flags
186 b2cfc4e2 2003-09-30 devnull .BR RFPROC|RFMEM|RFNOWAIT| \fIrforkflag\fR.
187 b2cfc4e2 2003-09-30 devnull (The thread library depends on all its procs
188 b2cfc4e2 2003-09-30 devnull running in the same rendezvous group.
189 b2cfc4e2 2003-09-30 devnull Do not include
190 b2cfc4e2 2003-09-30 devnull .B RFREND
191 b2cfc4e2 2003-09-30 devnull in
192 b2cfc4e2 2003-09-30 devnull .IR rforkflag .)
193 b2cfc4e2 2003-09-30 devnull .I Proccreate
194 b2cfc4e2 2003-09-30 devnull is identical to
195 b2cfc4e2 2003-09-30 devnull .I procrfork
196 b2cfc4e2 2003-09-30 devnull with
197 b2cfc4e2 2003-09-30 devnull .I rforkflag
198 b2cfc4e2 2003-09-30 devnull set to zero.
199 b2cfc4e2 2003-09-30 devnull Be aware that the calling thread may continue
200 b2cfc4e2 2003-09-30 devnull execution before
201 b2cfc4e2 2003-09-30 devnull the newly created proc and thread
202 b2cfc4e2 2003-09-30 devnull are scheduled.
203 b2cfc4e2 2003-09-30 devnull Because of this,
204 b2cfc4e2 2003-09-30 devnull .I arg
205 b2cfc4e2 2003-09-30 devnull should not point to data on the stack of a function that could
206 b2cfc4e2 2003-09-30 devnull return before the new process is scheduled.
207 b2cfc4e2 2003-09-30 devnull .PP
208 b2cfc4e2 2003-09-30 devnull .I Threadexits
209 b2cfc4e2 2003-09-30 devnull terminates the calling thread.
210 b2cfc4e2 2003-09-30 devnull If the thread is the last in its proc,
211 b2cfc4e2 2003-09-30 devnull .I threadexits
212 b2cfc4e2 2003-09-30 devnull also terminates the proc, using
213 b2cfc4e2 2003-09-30 devnull .I status
214 b2cfc4e2 2003-09-30 devnull as the exit status.
215 b2cfc4e2 2003-09-30 devnull .I Threadexitsall
216 b2cfc4e2 2003-09-30 devnull terminates all procs in the program,
217 b2cfc4e2 2003-09-30 devnull using
218 b2cfc4e2 2003-09-30 devnull .I status
219 b2cfc4e2 2003-09-30 devnull as the exit status.
220 b2cfc4e2 2003-09-30 devnull .PP
221 b2cfc4e2 2003-09-30 devnull The threads in a proc are coroutines, scheduled nonpreemptively
222 b2cfc4e2 2003-09-30 devnull in a round-robin fashion.
223 b2cfc4e2 2003-09-30 devnull A thread must explicitly relinquish control of the processor
224 b2cfc4e2 2003-09-30 devnull before another thread in the same proc is run.
225 b2cfc4e2 2003-09-30 devnull Calls that do this are
226 b2cfc4e2 2003-09-30 devnull .IR yield ,
227 b2cfc4e2 2003-09-30 devnull .IR proccreate ,
228 b2cfc4e2 2003-09-30 devnull .IR procexec ,
229 b2cfc4e2 2003-09-30 devnull .IR procexecl ,
230 b2cfc4e2 2003-09-30 devnull .IR threadexits ,
231 b2cfc4e2 2003-09-30 devnull .IR alt ,
232 b2cfc4e2 2003-09-30 devnull .IR send ,
233 b2cfc4e2 2003-09-30 devnull and
234 b2cfc4e2 2003-09-30 devnull .I recv
235 b2cfc4e2 2003-09-30 devnull (and the calls related to
236 b2cfc4e2 2003-09-30 devnull .I send
237 b2cfc4e2 2003-09-30 devnull and
238 b2cfc4e2 2003-09-30 devnull .IR recv \(emsee
239 b2cfc4e2 2003-09-30 devnull their descriptions further on).
240 b2cfc4e2 2003-09-30 devnull Procs are scheduled by the operating system.
241 b2cfc4e2 2003-09-30 devnull Therefore, threads in different procs can preempt one another
242 b2cfc4e2 2003-09-30 devnull in arbitrary ways and should synchronize their
243 b2cfc4e2 2003-09-30 devnull actions using
244 b2cfc4e2 2003-09-30 devnull .B qlocks
245 b2cfc4e2 2003-09-30 devnull (see
246 b2cfc4e2 2003-09-30 devnull .IR lock (2))
247 b2cfc4e2 2003-09-30 devnull or channel communication.
248 b2cfc4e2 2003-09-30 devnull System calls such as
249 b2cfc4e2 2003-09-30 devnull .IR read (2)
250 b2cfc4e2 2003-09-30 devnull block the entire proc;
251 b2cfc4e2 2003-09-30 devnull all threads in a proc block until the system call finishes.
252 b2cfc4e2 2003-09-30 devnull .PP
253 b2cfc4e2 2003-09-30 devnull As mentioned above, each thread has a unique integer thread id.
254 b2cfc4e2 2003-09-30 devnull Thread ids are not reused; they are unique across the life of the program.
255 b2cfc4e2 2003-09-30 devnull .I Threadid
256 b2cfc4e2 2003-09-30 devnull returns the id for the current thread.
257 b2cfc4e2 2003-09-30 devnull Each thread also has a thread group id.
258 b2cfc4e2 2003-09-30 devnull The initial thread has a group id of zero.
259 b2cfc4e2 2003-09-30 devnull Each new thread inherits the group id of
260 b2cfc4e2 2003-09-30 devnull the thread that created it.
261 b2cfc4e2 2003-09-30 devnull .I Threadgrp
262 b2cfc4e2 2003-09-30 devnull returns the group id for the current thread;
263 b2cfc4e2 2003-09-30 devnull .I threadsetgrp
264 b2cfc4e2 2003-09-30 devnull sets it.
265 b2cfc4e2 2003-09-30 devnull .I Threadpid
266 b2cfc4e2 2003-09-30 devnull returns the pid of the Plan 9 process containing
267 b2cfc4e2 2003-09-30 devnull the thread identified by
268 b2cfc4e2 2003-09-30 devnull .IR id ,
269 b2cfc4e2 2003-09-30 devnull or \-1
270 b2cfc4e2 2003-09-30 devnull if no such thread is found.
271 b2cfc4e2 2003-09-30 devnull .PP
272 b2cfc4e2 2003-09-30 devnull .I Threadint
273 b2cfc4e2 2003-09-30 devnull interrupts a thread that is blocked in a channel operation
274 b2cfc4e2 2003-09-30 devnull or system call.
275 b2cfc4e2 2003-09-30 devnull .I Threadintgrp
276 b2cfc4e2 2003-09-30 devnull interrupts all threads with the given group id.
277 b2cfc4e2 2003-09-30 devnull .I Threadkill
278 b2cfc4e2 2003-09-30 devnull marks a thread to die when it next relinquishes the processor
279 b2cfc4e2 2003-09-30 devnull (via one of the calls listed above).
280 b2cfc4e2 2003-09-30 devnull If the thread is blocked in a channel operation or system call,
281 b2cfc4e2 2003-09-30 devnull it is also interrupted.
282 b2cfc4e2 2003-09-30 devnull .I Threadkillgrp
283 b2cfc4e2 2003-09-30 devnull kills all threads with the given group id.
284 b2cfc4e2 2003-09-30 devnull Note that
285 b2cfc4e2 2003-09-30 devnull .I threadkill
286 b2cfc4e2 2003-09-30 devnull and
287 b2cfc4e2 2003-09-30 devnull .I threadkillgrp
288 b2cfc4e2 2003-09-30 devnull will not terminate a thread that never relinquishes
289 b2cfc4e2 2003-09-30 devnull the processor.
290 b2cfc4e2 2003-09-30 devnull .PP
291 b2cfc4e2 2003-09-30 devnull Primarily for debugging,
292 b2cfc4e2 2003-09-30 devnull threads can have string names associated with them.
293 b2cfc4e2 2003-09-30 devnull .I Threadgetname
294 b2cfc4e2 2003-09-30 devnull returns the current thread's name;
295 b2cfc4e2 2003-09-30 devnull .I threadsetname
296 b2cfc4e2 2003-09-30 devnull sets it.
297 b2cfc4e2 2003-09-30 devnull The pointer returned by
298 b2cfc4e2 2003-09-30 devnull .I threadgetname
299 b2cfc4e2 2003-09-30 devnull is only valid until the next call to
300 b2cfc4e2 2003-09-30 devnull .IR threadsetname .
301 b2cfc4e2 2003-09-30 devnull .PP
302 b2cfc4e2 2003-09-30 devnull .I Threaddata
303 b2cfc4e2 2003-09-30 devnull returns a pointer to a per-thread pointer
304 b2cfc4e2 2003-09-30 devnull that may be modified by threaded programs for
305 b2cfc4e2 2003-09-30 devnull per-thread storage.
306 b2cfc4e2 2003-09-30 devnull Similarly,
307 b2cfc4e2 2003-09-30 devnull .I procdata
308 b2cfc4e2 2003-09-30 devnull returns a pointer to a per-proc pointer.
309 b2cfc4e2 2003-09-30 devnull .PP
310 b2cfc4e2 2003-09-30 devnull .I Procexecl
311 b2cfc4e2 2003-09-30 devnull and
312 b2cfc4e2 2003-09-30 devnull .I procexec
313 b2cfc4e2 2003-09-30 devnull are threaded analogues of
314 b2cfc4e2 2003-09-30 devnull .I exec
315 b2cfc4e2 2003-09-30 devnull and
316 b2cfc4e2 2003-09-30 devnull .I execl
317 b2cfc4e2 2003-09-30 devnull (see
318 b2cfc4e2 2003-09-30 devnull .IR exec (2));
319 b2cfc4e2 2003-09-30 devnull on success,
320 b2cfc4e2 2003-09-30 devnull they replace the calling thread (which must be the only thread in its proc)
321 b2cfc4e2 2003-09-30 devnull and invoke the external program, never returning.
322 b2cfc4e2 2003-09-30 devnull On error, they return \-1.
323 b2cfc4e2 2003-09-30 devnull If
324 b2cfc4e2 2003-09-30 devnull .I cpid
325 b2cfc4e2 2003-09-30 devnull is not null, the pid of the invoked program
326 b2cfc4e2 2003-09-30 devnull will be sent along
327 b2cfc4e2 2003-09-30 devnull .I cpid
328 b2cfc4e2 2003-09-30 devnull once the program has been started, or \-1 will be sent if an
329 b2cfc4e2 2003-09-30 devnull error occurs.
330 b2cfc4e2 2003-09-30 devnull .I Procexec
331 b2cfc4e2 2003-09-30 devnull and
332 b2cfc4e2 2003-09-30 devnull .I procexecl
333 b2cfc4e2 2003-09-30 devnull will not access their arguments after sending a result
334 b2cfc4e2 2003-09-30 devnull along
335 b2cfc4e2 2003-09-30 devnull .IR cpid .
336 b2cfc4e2 2003-09-30 devnull Thus, programs that malloc the
337 b2cfc4e2 2003-09-30 devnull .I argv
338 b2cfc4e2 2003-09-30 devnull passed to
339 b2cfc4e2 2003-09-30 devnull .I procexec
340 b2cfc4e2 2003-09-30 devnull can safely free it once they have
341 b2cfc4e2 2003-09-30 devnull received the
342 b2cfc4e2 2003-09-30 devnull .I cpid
343 b2cfc4e2 2003-09-30 devnull response.
344 b2cfc4e2 2003-09-30 devnull .I Threadwaitchan
345 b2cfc4e2 2003-09-30 devnull returns a channel of pointers to
346 b2cfc4e2 2003-09-30 devnull .B Waitmsg
347 b2cfc4e2 2003-09-30 devnull structures (see
348 b2cfc4e2 2003-09-30 devnull .IR wait (2)).
349 b2cfc4e2 2003-09-30 devnull When an exec'ed process exits, a pointer to a
350 b2cfc4e2 2003-09-30 devnull .B Waitmsg
351 b2cfc4e2 2003-09-30 devnull is sent to this channel.
352 b2cfc4e2 2003-09-30 devnull These
353 b2cfc4e2 2003-09-30 devnull .B Waitmsg
354 b2cfc4e2 2003-09-30 devnull structures have been allocated with
355 b2cfc4e2 2003-09-30 devnull .IR malloc (2)
356 b2cfc4e2 2003-09-30 devnull and should be freed after use.
357 b2cfc4e2 2003-09-30 devnull .PP
358 b2cfc4e2 2003-09-30 devnull A
359 b2cfc4e2 2003-09-30 devnull .B Channel
360 b2cfc4e2 2003-09-30 devnull is a buffered or unbuffered queue for fixed-size messages.
361 b2cfc4e2 2003-09-30 devnull Procs and threads
362 b2cfc4e2 2003-09-30 devnull .I send
363 b2cfc4e2 2003-09-30 devnull messages into the channel and
364 b2cfc4e2 2003-09-30 devnull .I recv
365 b2cfc4e2 2003-09-30 devnull messages from the channel. If the channel is unbuffered, a
366 b2cfc4e2 2003-09-30 devnull .I send
367 b2cfc4e2 2003-09-30 devnull operation blocks until the corresponding
368 b2cfc4e2 2003-09-30 devnull .I recv
369 b2cfc4e2 2003-09-30 devnull operation occurs and
370 b2cfc4e2 2003-09-30 devnull .IR "vice versa" .
371 b2cfc4e2 2003-09-30 devnull .I Chaninit
372 b2cfc4e2 2003-09-30 devnull initializes a
373 b2cfc4e2 2003-09-30 devnull .B Channel
374 b2cfc4e2 2003-09-30 devnull for messages of size
375 b2cfc4e2 2003-09-30 devnull .I elsize
376 b2cfc4e2 2003-09-30 devnull and with a buffer holding
377 b2cfc4e2 2003-09-30 devnull .I nel
378 b2cfc4e2 2003-09-30 devnull messages.
379 b2cfc4e2 2003-09-30 devnull If
380 b2cfc4e2 2003-09-30 devnull .I nel
381 b2cfc4e2 2003-09-30 devnull is zero, the channel is unbuffered.
382 b2cfc4e2 2003-09-30 devnull .IR Chancreate
383 b2cfc4e2 2003-09-30 devnull allocates a new channel and initializes it.
384 b2cfc4e2 2003-09-30 devnull .I Chanfree
385 b2cfc4e2 2003-09-30 devnull frees a channel that is no longer used.
386 b2cfc4e2 2003-09-30 devnull .I Chanfree
387 b2cfc4e2 2003-09-30 devnull can be called by either sender or receiver after the last item has been
388 b2cfc4e2 2003-09-30 devnull sent or received. Freeing the channel will be delayed if there is a thread
389 b2cfc4e2 2003-09-30 devnull blocked on it until that thread unblocks (but
390 b2cfc4e2 2003-09-30 devnull .I chanfree
391 b2cfc4e2 2003-09-30 devnull returns immediately).
392 b2cfc4e2 2003-09-30 devnull .PP
393 b2cfc4e2 2003-09-30 devnull .I Send
394 b2cfc4e2 2003-09-30 devnull sends the element pointed at by
395 b2cfc4e2 2003-09-30 devnull .I v
396 b2cfc4e2 2003-09-30 devnull to the channel
397 b2cfc4e2 2003-09-30 devnull .IR c .
398 b2cfc4e2 2003-09-30 devnull If
399 b2cfc4e2 2003-09-30 devnull .I v
400 b2cfc4e2 2003-09-30 devnull is null, zeros are sent.
401 b2cfc4e2 2003-09-30 devnull .I Recv
402 b2cfc4e2 2003-09-30 devnull receives an element from
403 b2cfc4e2 2003-09-30 devnull .I c
404 b2cfc4e2 2003-09-30 devnull and stores it in
405 b2cfc4e2 2003-09-30 devnull .IR v .
406 b2cfc4e2 2003-09-30 devnull If
407 b2cfc4e2 2003-09-30 devnull .I v
408 b2cfc4e2 2003-09-30 devnull is null,
409 b2cfc4e2 2003-09-30 devnull the received value is discarded.
410 b2cfc4e2 2003-09-30 devnull .I Send
411 b2cfc4e2 2003-09-30 devnull and
412 b2cfc4e2 2003-09-30 devnull .I recv
413 b2cfc4e2 2003-09-30 devnull return 1 on success, \-1 if interrupted.
414 b2cfc4e2 2003-09-30 devnull .I Nbsend
415 b2cfc4e2 2003-09-30 devnull and
416 b2cfc4e2 2003-09-30 devnull .I nbrecv
417 b2cfc4e2 2003-09-30 devnull behave similarly, but return 0 rather than blocking.
418 b2cfc4e2 2003-09-30 devnull .PP
419 b2cfc4e2 2003-09-30 devnull .IR Sendp ,
420 b2cfc4e2 2003-09-30 devnull .IR nbsendp ,
421 b2cfc4e2 2003-09-30 devnull .IR sendul ,
422 b2cfc4e2 2003-09-30 devnull and
423 b2cfc4e2 2003-09-30 devnull .I nbsendul
424 b2cfc4e2 2003-09-30 devnull send a pointer or an unsigned long; the channel must
425 b2cfc4e2 2003-09-30 devnull have been initialized with the appropriate
426 b2cfc4e2 2003-09-30 devnull .IR elsize .
427 b2cfc4e2 2003-09-30 devnull .IR Recvp ,
428 b2cfc4e2 2003-09-30 devnull .IR nbrecvp ,
429 b2cfc4e2 2003-09-30 devnull .IR recvul ,
430 b2cfc4e2 2003-09-30 devnull and
431 b2cfc4e2 2003-09-30 devnull .I nbrecvul
432 b2cfc4e2 2003-09-30 devnull receive a pointer or an unsigned long;
433 b2cfc4e2 2003-09-30 devnull they return zero when a zero is received,
434 b2cfc4e2 2003-09-30 devnull when interrupted, or
435 b2cfc4e2 2003-09-30 devnull (for
436 b2cfc4e2 2003-09-30 devnull .I nbrecvp
437 b2cfc4e2 2003-09-30 devnull and
438 b2cfc4e2 2003-09-30 devnull .IR nbrecvul )
439 b2cfc4e2 2003-09-30 devnull when the operation would have blocked.
440 b2cfc4e2 2003-09-30 devnull To distinguish between these three cases,
441 b2cfc4e2 2003-09-30 devnull use
442 b2cfc4e2 2003-09-30 devnull .I recv
443 b2cfc4e2 2003-09-30 devnull or
444 b2cfc4e2 2003-09-30 devnull .IR nbrecv .
445 b2cfc4e2 2003-09-30 devnull .PP
446 b2cfc4e2 2003-09-30 devnull .I Alt
447 b2cfc4e2 2003-09-30 devnull can be used to recv from or send to one of a number of channels,
448 b2cfc4e2 2003-09-30 devnull as directed by an array of
449 b2cfc4e2 2003-09-30 devnull .B Alt
450 b2cfc4e2 2003-09-30 devnull structures,
451 b2cfc4e2 2003-09-30 devnull each of which describes a potential send or receive operation.
452 b2cfc4e2 2003-09-30 devnull In an
453 b2cfc4e2 2003-09-30 devnull .B Alt
454 b2cfc4e2 2003-09-30 devnull structure,
455 b2cfc4e2 2003-09-30 devnull .B c
456 b2cfc4e2 2003-09-30 devnull is the channel;
457 b2cfc4e2 2003-09-30 devnull .B v
458 b2cfc4e2 2003-09-30 devnull the value pointer (which may be null); and
459 b2cfc4e2 2003-09-30 devnull .B op
460 b2cfc4e2 2003-09-30 devnull the operation:
461 b2cfc4e2 2003-09-30 devnull .B CHANSND
462 b2cfc4e2 2003-09-30 devnull for a send operation,
463 b2cfc4e2 2003-09-30 devnull .B CHANRECV
464 b2cfc4e2 2003-09-30 devnull for a recv operation;
465 b2cfc4e2 2003-09-30 devnull .B CHANNOP
466 b2cfc4e2 2003-09-30 devnull for no operation
467 b2cfc4e2 2003-09-30 devnull (useful
468 b2cfc4e2 2003-09-30 devnull when
469 b2cfc4e2 2003-09-30 devnull .I alt
470 b2cfc4e2 2003-09-30 devnull is called with a varying set of operations).
471 b2cfc4e2 2003-09-30 devnull The array of
472 b2cfc4e2 2003-09-30 devnull .B Alt
473 b2cfc4e2 2003-09-30 devnull structures is terminated by an entry with
474 b2cfc4e2 2003-09-30 devnull .I op
475 b2cfc4e2 2003-09-30 devnull .B CHANEND
476 b2cfc4e2 2003-09-30 devnull or
477 b2cfc4e2 2003-09-30 devnull .BR CHANNOBLK .
478 b2cfc4e2 2003-09-30 devnull If at least one
479 b2cfc4e2 2003-09-30 devnull .B Alt
480 b2cfc4e2 2003-09-30 devnull structure can proceed, one of them is
481 b2cfc4e2 2003-09-30 devnull chosen at random to be executed.
482 b2cfc4e2 2003-09-30 devnull .I Alt
483 b2cfc4e2 2003-09-30 devnull returns the index of the chosen structure.
484 b2cfc4e2 2003-09-30 devnull If no operations can proceed and the list is terminated with
485 b2cfc4e2 2003-09-30 devnull .BR CHANNOBLK ,
486 b2cfc4e2 2003-09-30 devnull .I alt
487 b2cfc4e2 2003-09-30 devnull returns the index of the terminating
488 b2cfc4e2 2003-09-30 devnull .B CHANNOBLK
489 b2cfc4e2 2003-09-30 devnull structure.
490 b2cfc4e2 2003-09-30 devnull Otherwise,
491 b2cfc4e2 2003-09-30 devnull .I alt
492 b2cfc4e2 2003-09-30 devnull blocks until one of the operations can proceed,
493 b2cfc4e2 2003-09-30 devnull eventually returning the index of the structure executes.
494 b2cfc4e2 2003-09-30 devnull .I Alt
495 b2cfc4e2 2003-09-30 devnull returns \-1 when interrupted.
496 b2cfc4e2 2003-09-30 devnull The
497 b2cfc4e2 2003-09-30 devnull .B tag
498 b2cfc4e2 2003-09-30 devnull and
499 b2cfc4e2 2003-09-30 devnull .B entryno
500 b2cfc4e2 2003-09-30 devnull fields in the
501 b2cfc4e2 2003-09-30 devnull .B Alt
502 b2cfc4e2 2003-09-30 devnull structure are used internally by
503 b2cfc4e2 2003-09-30 devnull .I alt
504 b2cfc4e2 2003-09-30 devnull and need not be initialized.
505 b2cfc4e2 2003-09-30 devnull They are not used between
506 b2cfc4e2 2003-09-30 devnull .I alt
507 b2cfc4e2 2003-09-30 devnull calls.
508 b2cfc4e2 2003-09-30 devnull .PP
509 b2cfc4e2 2003-09-30 devnull .I Chanprint
510 b2cfc4e2 2003-09-30 devnull formats its arguments in the manner of
511 b2cfc4e2 2003-09-30 devnull .IR print (2)
512 b2cfc4e2 2003-09-30 devnull and sends the result to the channel
513 b2cfc4e2 2003-09-30 devnull .IR c.
514 b2cfc4e2 2003-09-30 devnull The string delivered by
515 b2cfc4e2 2003-09-30 devnull .I chanprint
516 b2cfc4e2 2003-09-30 devnull is allocated with
517 b2cfc4e2 2003-09-30 devnull .IR malloc (2)
518 b2cfc4e2 2003-09-30 devnull and should be freed upon receipt.
519 b2cfc4e2 2003-09-30 devnull .PP
520 b2cfc4e2 2003-09-30 devnull Thread library functions do not return on failure;
521 b2cfc4e2 2003-09-30 devnull if errors occur, the entire program is aborted.
522 b2cfc4e2 2003-09-30 devnull .PP
523 b2cfc4e2 2003-09-30 devnull Threaded programs should use
524 b2cfc4e2 2003-09-30 devnull .I threadnotify
525 b2cfc4e2 2003-09-30 devnull in place of
526 b2cfc4e2 2003-09-30 devnull .I atnotify
527 b2cfc4e2 2003-09-30 devnull (see
528 b2cfc4e2 2003-09-30 devnull .IR notify (2)).
529 b2cfc4e2 2003-09-30 devnull .PP
530 b2cfc4e2 2003-09-30 devnull It is safe to use
531 b2cfc4e2 2003-09-30 devnull .B sysfatal
532 b2cfc4e2 2003-09-30 devnull (see
533 b2cfc4e2 2003-09-30 devnull .IR perror (2))
534 b2cfc4e2 2003-09-30 devnull in threaded programs.
535 b2cfc4e2 2003-09-30 devnull .I Sysfatal
536 b2cfc4e2 2003-09-30 devnull will print the error string and call
537 b2cfc4e2 2003-09-30 devnull .IR threadexitsall .
538 b2cfc4e2 2003-09-30 devnull .PP
539 b2cfc4e2 2003-09-30 devnull It is safe to use
540 b2cfc4e2 2003-09-30 devnull .IR rfork
541 b2cfc4e2 2003-09-30 devnull (see
542 b2cfc4e2 2003-09-30 devnull .IR fork (2))
543 b2cfc4e2 2003-09-30 devnull to manage the namespace, file descriptors, note group, and environment of a
544 b2cfc4e2 2003-09-30 devnull single process.
545 b2cfc4e2 2003-09-30 devnull That is, it is safe to call
546 b2cfc4e2 2003-09-30 devnull .I rfork
547 b2cfc4e2 2003-09-30 devnull with the flags
548 b2cfc4e2 2003-09-30 devnull .BR RFNAMEG ,
549 b2cfc4e2 2003-09-30 devnull .BR RFFDG ,
550 b2cfc4e2 2003-09-30 devnull .BR RFCFDG ,
551 b2cfc4e2 2003-09-30 devnull .BR RFNOTEG ,
552 b2cfc4e2 2003-09-30 devnull .BR RFENVG ,
553 b2cfc4e2 2003-09-30 devnull and
554 b2cfc4e2 2003-09-30 devnull .BR RFCENVG.
555 b2cfc4e2 2003-09-30 devnull (To create new processes, use
556 b2cfc4e2 2003-09-30 devnull .I proccreate
557 b2cfc4e2 2003-09-30 devnull and
558 b2cfc4e2 2003-09-30 devnull .IR procrfork .)
559 b2cfc4e2 2003-09-30 devnull As mentioned above,
560 b2cfc4e2 2003-09-30 devnull the thread library depends on all procs being in the
561 b2cfc4e2 2003-09-30 devnull same rendezvous group; do not change the rendezvous
562 b2cfc4e2 2003-09-30 devnull group with
563 b2cfc4e2 2003-09-30 devnull .IR rfork .
564 b2cfc4e2 2003-09-30 devnull .SH FILES
565 b2cfc4e2 2003-09-30 devnull .B /sys/lib/acid/thread
566 b2cfc4e2 2003-09-30 devnull contains useful
567 b2cfc4e2 2003-09-30 devnull .IR acid (1)
568 b2cfc4e2 2003-09-30 devnull functions for debugging threaded programs.
569 b2cfc4e2 2003-09-30 devnull .PP
570 b2cfc4e2 2003-09-30 devnull .B /sys/src/libthread/example.c
571 b2cfc4e2 2003-09-30 devnull contains a full example program.
572 b2cfc4e2 2003-09-30 devnull .SH SOURCE
573 b2cfc4e2 2003-09-30 devnull .B /sys/src/libthread
574 b2cfc4e2 2003-09-30 devnull .SH SEE ALSO
575 b2cfc4e2 2003-09-30 devnull .IR intro (2),
576 b2cfc4e2 2003-09-30 devnull .IR ioproc (2)