Blame


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