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 058b0118 2005-01-03 devnull chansetname,
9 b2cfc4e2 2003-09-30 devnull mainstacksize,
10 b2cfc4e2 2003-09-30 devnull proccreate,
11 b2cfc4e2 2003-09-30 devnull procdata,
12 b2cfc4e2 2003-09-30 devnull recv,
13 b2cfc4e2 2003-09-30 devnull recvp,
14 b2cfc4e2 2003-09-30 devnull recvul,
15 b2cfc4e2 2003-09-30 devnull send,
16 b2cfc4e2 2003-09-30 devnull sendp,
17 b2cfc4e2 2003-09-30 devnull sendul,
18 b2cfc4e2 2003-09-30 devnull nbrecv,
19 b2cfc4e2 2003-09-30 devnull nbrecvp,
20 b2cfc4e2 2003-09-30 devnull nbrecvul,
21 b2cfc4e2 2003-09-30 devnull nbsend,
22 b2cfc4e2 2003-09-30 devnull nbsendp,
23 b2cfc4e2 2003-09-30 devnull nbsendul,
24 b2cfc4e2 2003-09-30 devnull threadcreate,
25 b2cfc4e2 2003-09-30 devnull threaddata,
26 058b0118 2005-01-03 devnull threadexec,
27 058b0118 2005-01-03 devnull threadexecl,
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 df970459 2006-06-26 devnull threadpin,
41 df970459 2006-06-26 devnull threadunpin,
42 b2cfc4e2 2003-09-30 devnull threadsetgrp,
43 b2cfc4e2 2003-09-30 devnull threadsetname,
44 058b0118 2005-01-03 devnull threadsetstate,
45 058b0118 2005-01-03 devnull threadspawn,
46 e4d62adf 2005-01-18 devnull threadspawnl,
47 b2cfc4e2 2003-09-30 devnull threadwaitchan,
48 b2cfc4e2 2003-09-30 devnull yield \- thread and proc management
49 b2cfc4e2 2003-09-30 devnull .SH SYNOPSIS
50 b2cfc4e2 2003-09-30 devnull .PP
51 b2cfc4e2 2003-09-30 devnull .EX
52 b2cfc4e2 2003-09-30 devnull .ta 4n +4n +4n +4n +4n +4n +4n
53 b2cfc4e2 2003-09-30 devnull #include <u.h>
54 b2cfc4e2 2003-09-30 devnull #include <libc.h>
55 b2cfc4e2 2003-09-30 devnull #include <thread.h>
56 b2cfc4e2 2003-09-30 devnull .sp
57 b2cfc4e2 2003-09-30 devnull #define CHANEND 0
58 b2cfc4e2 2003-09-30 devnull #define CHANSND 1
59 b2cfc4e2 2003-09-30 devnull #define CHANRCV 2
60 b2cfc4e2 2003-09-30 devnull #define CHANNOP 3
61 b2cfc4e2 2003-09-30 devnull #define CHANNOBLK 4
62 b2cfc4e2 2003-09-30 devnull .sp
63 b2cfc4e2 2003-09-30 devnull .ta \w' 'u +\w'Channel 'u
64 b2cfc4e2 2003-09-30 devnull typedef struct Alt Alt;
65 b2cfc4e2 2003-09-30 devnull struct Alt {
66 b2cfc4e2 2003-09-30 devnull Channel *c;
67 b2cfc4e2 2003-09-30 devnull void *v;
68 b2cfc4e2 2003-09-30 devnull int op;
69 b2cfc4e2 2003-09-30 devnull Channel **tag;
70 b2cfc4e2 2003-09-30 devnull int entryno;
71 058b0118 2005-01-03 devnull char *name;
72 b2cfc4e2 2003-09-30 devnull };
73 b2cfc4e2 2003-09-30 devnull .fi
74 b2cfc4e2 2003-09-30 devnull .de XX
75 b5fdffee 2004-04-19 devnull .if t .sp 0.5
76 b5fdffee 2004-04-19 devnull .if n .sp
77 b2cfc4e2 2003-09-30 devnull ..
78 b2cfc4e2 2003-09-30 devnull .PP
79 b2cfc4e2 2003-09-30 devnull .nf
80 b2cfc4e2 2003-09-30 devnull .ft L
81 b2cfc4e2 2003-09-30 devnull .ta \w'\fLChannel* 'u +4n +4n +4n +4n
82 b2cfc4e2 2003-09-30 devnull void threadmain(int argc, char *argv[])
83 b2cfc4e2 2003-09-30 devnull int mainstacksize
84 b2cfc4e2 2003-09-30 devnull int proccreate(void (*fn)(void*), void *arg, uint stacksize)
85 b2cfc4e2 2003-09-30 devnull int threadcreate(void (*fn)(void*), void *arg, uint stacksize)
86 b2cfc4e2 2003-09-30 devnull void threadexits(char *status)
87 b2cfc4e2 2003-09-30 devnull void threadexitsall(char *status)
88 b2cfc4e2 2003-09-30 devnull void yield(void)
89 df970459 2006-06-26 devnull int threadpin(void)
90 df970459 2006-06-26 devnull int threadunpin(void)
91 b2cfc4e2 2003-09-30 devnull .XX
92 b2cfc4e2 2003-09-30 devnull int threadid(void)
93 b2cfc4e2 2003-09-30 devnull int threadgrp(void)
94 b2cfc4e2 2003-09-30 devnull int threadsetgrp(int group)
95 b2cfc4e2 2003-09-30 devnull int threadpid(int id)
96 b2cfc4e2 2003-09-30 devnull .XX
97 b2cfc4e2 2003-09-30 devnull int threadint(int id)
98 b2cfc4e2 2003-09-30 devnull int threadintgrp(int group)
99 b2cfc4e2 2003-09-30 devnull int threadkill(int id)
100 b2cfc4e2 2003-09-30 devnull int threadkillgrp(int group)
101 b2cfc4e2 2003-09-30 devnull .XX
102 b2cfc4e2 2003-09-30 devnull void threadsetname(char *name)
103 b2cfc4e2 2003-09-30 devnull char* threadgetname(void)
104 b2cfc4e2 2003-09-30 devnull .XX
105 b2cfc4e2 2003-09-30 devnull void** threaddata(void)
106 b2cfc4e2 2003-09-30 devnull void** procdata(void)
107 b2cfc4e2 2003-09-30 devnull .XX
108 b2cfc4e2 2003-09-30 devnull int chaninit(Channel *c, int elsize, int nel)
109 b2cfc4e2 2003-09-30 devnull Channel* chancreate(int elsize, int nel)
110 b2cfc4e2 2003-09-30 devnull void chanfree(Channel *c)
111 b2cfc4e2 2003-09-30 devnull .XX
112 b2cfc4e2 2003-09-30 devnull int alt(Alt *alts)
113 b2cfc4e2 2003-09-30 devnull int recv(Channel *c, void *v)
114 b2cfc4e2 2003-09-30 devnull void* recvp(Channel *c)
115 b2cfc4e2 2003-09-30 devnull ulong recvul(Channel *c)
116 b2cfc4e2 2003-09-30 devnull int nbrecv(Channel *c, void *v)
117 b2cfc4e2 2003-09-30 devnull void* nbrecvp(Channel *c)
118 b2cfc4e2 2003-09-30 devnull ulong nbrecvul(Channel *c)
119 b2cfc4e2 2003-09-30 devnull int send(Channel *c, void *v)
120 b2cfc4e2 2003-09-30 devnull int sendp(Channel *c, void *v)
121 b2cfc4e2 2003-09-30 devnull int sendul(Channel *c, ulong v)
122 b2cfc4e2 2003-09-30 devnull int nbsend(Channel *c, void *v)
123 b2cfc4e2 2003-09-30 devnull int nbsendp(Channel *c, void *v)
124 b2cfc4e2 2003-09-30 devnull int nbsendul(Channel *c, ulong v)
125 b2cfc4e2 2003-09-30 devnull int chanprint(Channel *c, char *fmt, ...)
126 b2cfc4e2 2003-09-30 devnull .XX
127 e4d62adf 2005-01-18 devnull int threadspawnl(int fd[3], char *file, ...)
128 058b0118 2005-01-03 devnull int threadspawn(int fd[3], char *file, char *args[])
129 058b0118 2005-01-03 devnull int threadexecl(Channel *cpid, int fd[3], char *file, ...)
130 058b0118 2005-01-03 devnull int threadexec(Channel *cpid, int fd[3], 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 058b0118 2005-01-03 devnull .I Proccreate
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 058b0118 2005-01-03 devnull .\" .I Procrfork
189 058b0118 2005-01-03 devnull .\" creates the new proc by calling
190 058b0118 2005-01-03 devnull .\" .B rfork
191 058b0118 2005-01-03 devnull .\" (see
192 058b0118 2005-01-03 devnull .\" .IR fork (3))
193 058b0118 2005-01-03 devnull .\" with flags
194 058b0118 2005-01-03 devnull .\" .BR RFPROC|RFMEM|RFNOWAIT| \fIrforkflag\fR.
195 058b0118 2005-01-03 devnull .\" (The thread library depends on all its procs
196 058b0118 2005-01-03 devnull .\" running in the same rendezvous group.
197 058b0118 2005-01-03 devnull .\" Do not include
198 058b0118 2005-01-03 devnull .\" .B RFREND
199 058b0118 2005-01-03 devnull .\" in
200 058b0118 2005-01-03 devnull .\" .IR rforkflag .)
201 058b0118 2005-01-03 devnull .\" .I Proccreate
202 058b0118 2005-01-03 devnull .\" is identical to
203 058b0118 2005-01-03 devnull .\" .I procrfork
204 058b0118 2005-01-03 devnull .\" with
205 058b0118 2005-01-03 devnull .\" .I rforkflag
206 058b0118 2005-01-03 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 058b0118 2005-01-03 devnull When the last thread in
230 058b0118 2005-01-03 devnull .IR threadmain 's
231 058b0118 2005-01-03 devnull proc exits, the program will appear to its parent to have exited.
232 058b0118 2005-01-03 devnull The remaining procs will still run together, but as a background program.
233 058b0118 2005-01-03 devnull .PP
234 b2cfc4e2 2003-09-30 devnull The threads in a proc are coroutines, scheduled nonpreemptively
235 b2cfc4e2 2003-09-30 devnull in a round-robin fashion.
236 b2cfc4e2 2003-09-30 devnull A thread must explicitly relinquish control of the processor
237 b2cfc4e2 2003-09-30 devnull before another thread in the same proc is run.
238 b2cfc4e2 2003-09-30 devnull Calls that do this are
239 b2cfc4e2 2003-09-30 devnull .IR yield ,
240 b2cfc4e2 2003-09-30 devnull .IR proccreate ,
241 058b0118 2005-01-03 devnull .IR threadexec ,
242 058b0118 2005-01-03 devnull .IR threadexecl ,
243 b2cfc4e2 2003-09-30 devnull .IR threadexits ,
244 058b0118 2005-01-03 devnull .IR threadspawn ,
245 b2cfc4e2 2003-09-30 devnull .IR alt ,
246 b2cfc4e2 2003-09-30 devnull .IR send ,
247 b2cfc4e2 2003-09-30 devnull and
248 b2cfc4e2 2003-09-30 devnull .I recv
249 b2cfc4e2 2003-09-30 devnull (and the calls related to
250 b2cfc4e2 2003-09-30 devnull .I send
251 b2cfc4e2 2003-09-30 devnull and
252 b2cfc4e2 2003-09-30 devnull .IR recv \(emsee
253 b2cfc4e2 2003-09-30 devnull their descriptions further on).
254 b2cfc4e2 2003-09-30 devnull Procs are scheduled by the operating system.
255 b2cfc4e2 2003-09-30 devnull Therefore, threads in different procs can preempt one another
256 b2cfc4e2 2003-09-30 devnull in arbitrary ways and should synchronize their
257 b2cfc4e2 2003-09-30 devnull actions using
258 b2cfc4e2 2003-09-30 devnull .B qlocks
259 b2cfc4e2 2003-09-30 devnull (see
260 bf8a59fa 2004-04-11 devnull .IR lock (3))
261 b2cfc4e2 2003-09-30 devnull or channel communication.
262 b2cfc4e2 2003-09-30 devnull System calls such as
263 bf8a59fa 2004-04-11 devnull .IR read (3)
264 b2cfc4e2 2003-09-30 devnull block the entire proc;
265 b2cfc4e2 2003-09-30 devnull all threads in a proc block until the system call finishes.
266 df970459 2006-06-26 devnull .PP
267 df970459 2006-06-26 devnull .I Threadpin
268 df970459 2006-06-26 devnull disables scheduling inside a proc, `pinning' the current
269 df970459 2006-06-26 devnull thread as the only runnable one in the current proc.
270 df970459 2006-06-26 devnull .I Threadunpin
271 df970459 2006-06-26 devnull reenables scheduling, allowing other procs to run once the current
272 df970459 2006-06-26 devnull thread relinquishes the processor.
273 df970459 2006-06-26 devnull .I Threadpin
274 df970459 2006-06-26 devnull and
275 df970459 2006-06-26 devnull .I threadunpin
276 df970459 2006-06-26 devnull can lead to deadlock.
277 df970459 2006-06-26 devnull Used carefully, they can make library routines that use
278 df970459 2006-06-26 devnull .B qlocks
279 df970459 2006-06-26 devnull appear atomic relative to the current proc, like a system call.
280 b2cfc4e2 2003-09-30 devnull .PP
281 b2cfc4e2 2003-09-30 devnull As mentioned above, each thread has a unique integer thread id.
282 b2cfc4e2 2003-09-30 devnull Thread ids are not reused; they are unique across the life of the program.
283 b2cfc4e2 2003-09-30 devnull .I Threadid
284 b2cfc4e2 2003-09-30 devnull returns the id for the current thread.
285 b2cfc4e2 2003-09-30 devnull Each thread also has a thread group id.
286 b2cfc4e2 2003-09-30 devnull The initial thread has a group id of zero.
287 b2cfc4e2 2003-09-30 devnull Each new thread inherits the group id of
288 b2cfc4e2 2003-09-30 devnull the thread that created it.
289 b2cfc4e2 2003-09-30 devnull .I Threadgrp
290 b2cfc4e2 2003-09-30 devnull returns the group id for the current thread;
291 b2cfc4e2 2003-09-30 devnull .I threadsetgrp
292 b2cfc4e2 2003-09-30 devnull sets it.
293 b2cfc4e2 2003-09-30 devnull .I Threadpid
294 b2cfc4e2 2003-09-30 devnull returns the pid of the Plan 9 process containing
295 b2cfc4e2 2003-09-30 devnull the thread identified by
296 b2cfc4e2 2003-09-30 devnull .IR id ,
297 b2cfc4e2 2003-09-30 devnull or \-1
298 b2cfc4e2 2003-09-30 devnull if no such thread is found.
299 b2cfc4e2 2003-09-30 devnull .PP
300 b2cfc4e2 2003-09-30 devnull .I Threadint
301 b2cfc4e2 2003-09-30 devnull interrupts a thread that is blocked in a channel operation
302 b2cfc4e2 2003-09-30 devnull or system call.
303 b2cfc4e2 2003-09-30 devnull .I Threadintgrp
304 b2cfc4e2 2003-09-30 devnull interrupts all threads with the given group id.
305 b2cfc4e2 2003-09-30 devnull .I Threadkill
306 b2cfc4e2 2003-09-30 devnull marks a thread to die when it next relinquishes the processor
307 b2cfc4e2 2003-09-30 devnull (via one of the calls listed above).
308 b2cfc4e2 2003-09-30 devnull If the thread is blocked in a channel operation or system call,
309 b2cfc4e2 2003-09-30 devnull it is also interrupted.
310 b2cfc4e2 2003-09-30 devnull .I Threadkillgrp
311 b2cfc4e2 2003-09-30 devnull kills all threads with the given group id.
312 b2cfc4e2 2003-09-30 devnull Note that
313 b2cfc4e2 2003-09-30 devnull .I threadkill
314 b2cfc4e2 2003-09-30 devnull and
315 b2cfc4e2 2003-09-30 devnull .I threadkillgrp
316 b2cfc4e2 2003-09-30 devnull will not terminate a thread that never relinquishes
317 b2cfc4e2 2003-09-30 devnull the processor.
318 b2cfc4e2 2003-09-30 devnull .PP
319 b2cfc4e2 2003-09-30 devnull Primarily for debugging,
320 b2cfc4e2 2003-09-30 devnull threads can have string names associated with them.
321 b2cfc4e2 2003-09-30 devnull .I Threadgetname
322 b2cfc4e2 2003-09-30 devnull returns the current thread's name;
323 b2cfc4e2 2003-09-30 devnull .I threadsetname
324 b2cfc4e2 2003-09-30 devnull sets it.
325 b2cfc4e2 2003-09-30 devnull The pointer returned by
326 b2cfc4e2 2003-09-30 devnull .I threadgetname
327 b2cfc4e2 2003-09-30 devnull is only valid until the next call to
328 b2cfc4e2 2003-09-30 devnull .IR threadsetname .
329 b2cfc4e2 2003-09-30 devnull .PP
330 058b0118 2005-01-03 devnull Also for debugging,
331 058b0118 2005-01-03 devnull threads have a string state associated with them.
332 058b0118 2005-01-03 devnull .I Threadsetstate
333 058b0118 2005-01-03 devnull sets the state string.
334 058b0118 2005-01-03 devnull There is no
335 058b0118 2005-01-03 devnull .IR threadgetstate ;
336 058b0118 2005-01-03 devnull since the thread scheduler resets the state to
337 058b0118 2005-01-03 devnull .B Running
338 058b0118 2005-01-03 devnull every time it runs the thread,
339 058b0118 2005-01-03 devnull it is only useful for debuggers to inspect the state.
340 058b0118 2005-01-03 devnull .PP
341 b2cfc4e2 2003-09-30 devnull .I Threaddata
342 b2cfc4e2 2003-09-30 devnull returns a pointer to a per-thread pointer
343 b2cfc4e2 2003-09-30 devnull that may be modified by threaded programs for
344 b2cfc4e2 2003-09-30 devnull per-thread storage.
345 b2cfc4e2 2003-09-30 devnull Similarly,
346 b2cfc4e2 2003-09-30 devnull .I procdata
347 b2cfc4e2 2003-09-30 devnull returns a pointer to a per-proc pointer.
348 b2cfc4e2 2003-09-30 devnull .PP
349 058b0118 2005-01-03 devnull .I Threadexecl
350 b2cfc4e2 2003-09-30 devnull and
351 058b0118 2005-01-03 devnull .I threadexec
352 b2cfc4e2 2003-09-30 devnull are threaded analogues of
353 b2cfc4e2 2003-09-30 devnull .I exec
354 b2cfc4e2 2003-09-30 devnull and
355 b2cfc4e2 2003-09-30 devnull .I execl
356 b2cfc4e2 2003-09-30 devnull (see
357 bf8a59fa 2004-04-11 devnull .IR exec (3));
358 b2cfc4e2 2003-09-30 devnull on success,
359 058b0118 2005-01-03 devnull they replace the calling thread
360 b2cfc4e2 2003-09-30 devnull and invoke the external program, never returning.
361 058b0118 2005-01-03 devnull (Unlike on Plan 9, the calling thread need not be the only thread in its proc\(emthe other
362 058b0118 2005-01-03 devnull threads will continue executing.)
363 b2cfc4e2 2003-09-30 devnull On error, they return \-1.
364 b2cfc4e2 2003-09-30 devnull If
365 b2cfc4e2 2003-09-30 devnull .I cpid
366 b2cfc4e2 2003-09-30 devnull is not null, the pid of the invoked program
367 b2cfc4e2 2003-09-30 devnull will be sent along
368 b2cfc4e2 2003-09-30 devnull .I cpid
369 058b0118 2005-01-03 devnull (using
370 058b0118 2005-01-03 devnull .IR sendul )
371 b2cfc4e2 2003-09-30 devnull once the program has been started, or \-1 will be sent if an
372 b2cfc4e2 2003-09-30 devnull error occurs.
373 058b0118 2005-01-03 devnull .I Threadexec
374 b2cfc4e2 2003-09-30 devnull and
375 058b0118 2005-01-03 devnull .I threadexecl
376 b2cfc4e2 2003-09-30 devnull will not access their arguments after sending a result
377 b2cfc4e2 2003-09-30 devnull along
378 b2cfc4e2 2003-09-30 devnull .IR cpid .
379 b2cfc4e2 2003-09-30 devnull Thus, programs that malloc the
380 b2cfc4e2 2003-09-30 devnull .I argv
381 b2cfc4e2 2003-09-30 devnull passed to
382 058b0118 2005-01-03 devnull .I threadexec
383 b2cfc4e2 2003-09-30 devnull can safely free it once they have
384 b2cfc4e2 2003-09-30 devnull received the
385 b2cfc4e2 2003-09-30 devnull .I cpid
386 b2cfc4e2 2003-09-30 devnull response.
387 058b0118 2005-01-03 devnull .PP
388 058b0118 2005-01-03 devnull .I Threadexecl
389 058b0118 2005-01-03 devnull and
390 058b0118 2005-01-03 devnull .I threadexec
391 058b0118 2005-01-03 devnull will duplicate
392 058b0118 2005-01-03 devnull (see
393 058b0118 2005-01-03 devnull .IR dup (3))
394 058b0118 2005-01-03 devnull the three file descriptors in
395 058b0118 2005-01-03 devnull .I fd
396 058b0118 2005-01-03 devnull onto standard input, output, and error for the external program
397 058b0118 2005-01-03 devnull and then close them in the calling thread.
398 058b0118 2005-01-03 devnull Beware of code that sets
399 058b0118 2005-01-03 devnull .IP
400 058b0118 2005-01-03 devnull .EX
401 058b0118 2005-01-03 devnull fd[0] = 0;
402 058b0118 2005-01-03 devnull fd[1] = 1;
403 058b0118 2005-01-03 devnull fd[2] = 2;
404 058b0118 2005-01-03 devnull .EE
405 c8b6342d 2005-01-13 devnull .LP
406 058b0118 2005-01-03 devnull to use the current standard files. The correct code is
407 058b0118 2005-01-03 devnull .IP
408 058b0118 2005-01-03 devnull .EX
409 058b0118 2005-01-03 devnull fd[0] = dup(0, -1);
410 058b0118 2005-01-03 devnull fd[1] = dup(1, -1);
411 058b0118 2005-01-03 devnull fd[2] = dup(2, -1);
412 058b0118 2005-01-03 devnull .EE
413 058b0118 2005-01-03 devnull .PP
414 e4d62adf 2005-01-18 devnull .I Threadspawnl
415 e4d62adf 2005-01-18 devnull and
416 e4d62adf 2005-01-18 devnull .I threadspawn
417 e4d62adf 2005-01-18 devnull are like
418 e4d62adf 2005-01-18 devnull .I threadexecl
419 e4d62adf 2005-01-18 devnull and
420 058b0118 2005-01-03 devnull .I threadexec
421 e4d62adf 2005-01-18 devnull but do not replace the current thread.
422 e4d62adf 2005-01-18 devnull They return the pid of the invoked program on success, or
423 058b0118 2005-01-03 devnull \-1 on error.
424 058b0118 2005-01-03 devnull .PP
425 b2cfc4e2 2003-09-30 devnull .I Threadwaitchan
426 b2cfc4e2 2003-09-30 devnull returns a channel of pointers to
427 b2cfc4e2 2003-09-30 devnull .B Waitmsg
428 b2cfc4e2 2003-09-30 devnull structures (see
429 bf8a59fa 2004-04-11 devnull .IR wait (3)).
430 b2cfc4e2 2003-09-30 devnull When an exec'ed process exits, a pointer to a
431 b2cfc4e2 2003-09-30 devnull .B Waitmsg
432 b2cfc4e2 2003-09-30 devnull is sent to this channel.
433 b2cfc4e2 2003-09-30 devnull These
434 b2cfc4e2 2003-09-30 devnull .B Waitmsg
435 b2cfc4e2 2003-09-30 devnull structures have been allocated with
436 bf8a59fa 2004-04-11 devnull .IR malloc (3)
437 b2cfc4e2 2003-09-30 devnull and should be freed after use.
438 b2cfc4e2 2003-09-30 devnull .PP
439 b2cfc4e2 2003-09-30 devnull A
440 b2cfc4e2 2003-09-30 devnull .B Channel
441 b2cfc4e2 2003-09-30 devnull is a buffered or unbuffered queue for fixed-size messages.
442 b2cfc4e2 2003-09-30 devnull Procs and threads
443 b2cfc4e2 2003-09-30 devnull .I send
444 b2cfc4e2 2003-09-30 devnull messages into the channel and
445 b2cfc4e2 2003-09-30 devnull .I recv
446 b2cfc4e2 2003-09-30 devnull messages from the channel. If the channel is unbuffered, a
447 b2cfc4e2 2003-09-30 devnull .I send
448 b2cfc4e2 2003-09-30 devnull operation blocks until the corresponding
449 b2cfc4e2 2003-09-30 devnull .I recv
450 b2cfc4e2 2003-09-30 devnull operation occurs and
451 b2cfc4e2 2003-09-30 devnull .IR "vice versa" .
452 b2cfc4e2 2003-09-30 devnull .I Chaninit
453 b2cfc4e2 2003-09-30 devnull initializes a
454 b2cfc4e2 2003-09-30 devnull .B Channel
455 b2cfc4e2 2003-09-30 devnull for messages of size
456 b2cfc4e2 2003-09-30 devnull .I elsize
457 b2cfc4e2 2003-09-30 devnull and with a buffer holding
458 b2cfc4e2 2003-09-30 devnull .I nel
459 b2cfc4e2 2003-09-30 devnull messages.
460 b2cfc4e2 2003-09-30 devnull If
461 b2cfc4e2 2003-09-30 devnull .I nel
462 b2cfc4e2 2003-09-30 devnull is zero, the channel is unbuffered.
463 b2cfc4e2 2003-09-30 devnull .IR Chancreate
464 b2cfc4e2 2003-09-30 devnull allocates a new channel and initializes it.
465 b2cfc4e2 2003-09-30 devnull .I Chanfree
466 b2cfc4e2 2003-09-30 devnull frees a channel that is no longer used.
467 b2cfc4e2 2003-09-30 devnull .I Chanfree
468 b2cfc4e2 2003-09-30 devnull can be called by either sender or receiver after the last item has been
469 b2cfc4e2 2003-09-30 devnull sent or received. Freeing the channel will be delayed if there is a thread
470 b2cfc4e2 2003-09-30 devnull blocked on it until that thread unblocks (but
471 b2cfc4e2 2003-09-30 devnull .I chanfree
472 b2cfc4e2 2003-09-30 devnull returns immediately).
473 b2cfc4e2 2003-09-30 devnull .PP
474 058b0118 2005-01-03 devnull The
475 058b0118 2005-01-03 devnull .B name
476 058b0118 2005-01-03 devnull element in the
477 058b0118 2005-01-03 devnull .B Channel
478 058b0118 2005-01-03 devnull structure is a description intended for use in debugging.
479 058b0118 2005-01-03 devnull .I Chansetname
480 058b0118 2005-01-03 devnull sets the name.
481 058b0118 2005-01-03 devnull .PP
482 b2cfc4e2 2003-09-30 devnull .I Send
483 b2cfc4e2 2003-09-30 devnull sends the element pointed at by
484 b2cfc4e2 2003-09-30 devnull .I v
485 b2cfc4e2 2003-09-30 devnull to the channel
486 b2cfc4e2 2003-09-30 devnull .IR c .
487 b2cfc4e2 2003-09-30 devnull If
488 b2cfc4e2 2003-09-30 devnull .I v
489 b2cfc4e2 2003-09-30 devnull is null, zeros are sent.
490 b2cfc4e2 2003-09-30 devnull .I Recv
491 b2cfc4e2 2003-09-30 devnull receives an element from
492 b2cfc4e2 2003-09-30 devnull .I c
493 b2cfc4e2 2003-09-30 devnull and stores it in
494 b2cfc4e2 2003-09-30 devnull .IR v .
495 b2cfc4e2 2003-09-30 devnull If
496 b2cfc4e2 2003-09-30 devnull .I v
497 b2cfc4e2 2003-09-30 devnull is null,
498 b2cfc4e2 2003-09-30 devnull the received value is discarded.
499 b2cfc4e2 2003-09-30 devnull .I Send
500 b2cfc4e2 2003-09-30 devnull and
501 b2cfc4e2 2003-09-30 devnull .I recv
502 b2cfc4e2 2003-09-30 devnull return 1 on success, \-1 if interrupted.
503 b2cfc4e2 2003-09-30 devnull .I Nbsend
504 b2cfc4e2 2003-09-30 devnull and
505 b2cfc4e2 2003-09-30 devnull .I nbrecv
506 b2cfc4e2 2003-09-30 devnull behave similarly, but return 0 rather than blocking.
507 b2cfc4e2 2003-09-30 devnull .PP
508 b2cfc4e2 2003-09-30 devnull .IR Sendp ,
509 b2cfc4e2 2003-09-30 devnull .IR nbsendp ,
510 b2cfc4e2 2003-09-30 devnull .IR sendul ,
511 b2cfc4e2 2003-09-30 devnull and
512 b2cfc4e2 2003-09-30 devnull .I nbsendul
513 b2cfc4e2 2003-09-30 devnull send a pointer or an unsigned long; the channel must
514 b2cfc4e2 2003-09-30 devnull have been initialized with the appropriate
515 b2cfc4e2 2003-09-30 devnull .IR elsize .
516 b2cfc4e2 2003-09-30 devnull .IR Recvp ,
517 b2cfc4e2 2003-09-30 devnull .IR nbrecvp ,
518 b2cfc4e2 2003-09-30 devnull .IR recvul ,
519 b2cfc4e2 2003-09-30 devnull and
520 b2cfc4e2 2003-09-30 devnull .I nbrecvul
521 b2cfc4e2 2003-09-30 devnull receive a pointer or an unsigned long;
522 b2cfc4e2 2003-09-30 devnull they return zero when a zero is received,
523 b2cfc4e2 2003-09-30 devnull when interrupted, or
524 b2cfc4e2 2003-09-30 devnull (for
525 b2cfc4e2 2003-09-30 devnull .I nbrecvp
526 b2cfc4e2 2003-09-30 devnull and
527 b2cfc4e2 2003-09-30 devnull .IR nbrecvul )
528 b2cfc4e2 2003-09-30 devnull when the operation would have blocked.
529 b2cfc4e2 2003-09-30 devnull To distinguish between these three cases,
530 b2cfc4e2 2003-09-30 devnull use
531 b2cfc4e2 2003-09-30 devnull .I recv
532 b2cfc4e2 2003-09-30 devnull or
533 b2cfc4e2 2003-09-30 devnull .IR nbrecv .
534 b2cfc4e2 2003-09-30 devnull .PP
535 b2cfc4e2 2003-09-30 devnull .I Alt
536 b2cfc4e2 2003-09-30 devnull can be used to recv from or send to one of a number of channels,
537 b2cfc4e2 2003-09-30 devnull as directed by an array of
538 b2cfc4e2 2003-09-30 devnull .B Alt
539 b2cfc4e2 2003-09-30 devnull structures,
540 b2cfc4e2 2003-09-30 devnull each of which describes a potential send or receive operation.
541 b2cfc4e2 2003-09-30 devnull In an
542 b2cfc4e2 2003-09-30 devnull .B Alt
543 b2cfc4e2 2003-09-30 devnull structure,
544 b2cfc4e2 2003-09-30 devnull .B c
545 b2cfc4e2 2003-09-30 devnull is the channel;
546 b2cfc4e2 2003-09-30 devnull .B v
547 b2cfc4e2 2003-09-30 devnull the value pointer (which may be null); and
548 b2cfc4e2 2003-09-30 devnull .B op
549 b2cfc4e2 2003-09-30 devnull the operation:
550 b2cfc4e2 2003-09-30 devnull .B CHANSND
551 b2cfc4e2 2003-09-30 devnull for a send operation,
552 b2cfc4e2 2003-09-30 devnull .B CHANRECV
553 b2cfc4e2 2003-09-30 devnull for a recv operation;
554 b2cfc4e2 2003-09-30 devnull .B CHANNOP
555 b2cfc4e2 2003-09-30 devnull for no operation
556 b2cfc4e2 2003-09-30 devnull (useful
557 b2cfc4e2 2003-09-30 devnull when
558 b2cfc4e2 2003-09-30 devnull .I alt
559 b2cfc4e2 2003-09-30 devnull is called with a varying set of operations).
560 b2cfc4e2 2003-09-30 devnull The array of
561 b2cfc4e2 2003-09-30 devnull .B Alt
562 b2cfc4e2 2003-09-30 devnull structures is terminated by an entry with
563 b2cfc4e2 2003-09-30 devnull .I op
564 b2cfc4e2 2003-09-30 devnull .B CHANEND
565 b2cfc4e2 2003-09-30 devnull or
566 b2cfc4e2 2003-09-30 devnull .BR CHANNOBLK .
567 b2cfc4e2 2003-09-30 devnull If at least one
568 b2cfc4e2 2003-09-30 devnull .B Alt
569 b2cfc4e2 2003-09-30 devnull structure can proceed, one of them is
570 b2cfc4e2 2003-09-30 devnull chosen at random to be executed.
571 b2cfc4e2 2003-09-30 devnull .I Alt
572 b2cfc4e2 2003-09-30 devnull returns the index of the chosen structure.
573 b2cfc4e2 2003-09-30 devnull If no operations can proceed and the list is terminated with
574 b2cfc4e2 2003-09-30 devnull .BR CHANNOBLK ,
575 b2cfc4e2 2003-09-30 devnull .I alt
576 b2cfc4e2 2003-09-30 devnull returns the index of the terminating
577 b2cfc4e2 2003-09-30 devnull .B CHANNOBLK
578 b2cfc4e2 2003-09-30 devnull structure.
579 b2cfc4e2 2003-09-30 devnull Otherwise,
580 b2cfc4e2 2003-09-30 devnull .I alt
581 b2cfc4e2 2003-09-30 devnull blocks until one of the operations can proceed,
582 b2cfc4e2 2003-09-30 devnull eventually returning the index of the structure executes.
583 b2cfc4e2 2003-09-30 devnull .I Alt
584 b2cfc4e2 2003-09-30 devnull returns \-1 when interrupted.
585 b2cfc4e2 2003-09-30 devnull The
586 b2cfc4e2 2003-09-30 devnull .B tag
587 b2cfc4e2 2003-09-30 devnull and
588 b2cfc4e2 2003-09-30 devnull .B entryno
589 b2cfc4e2 2003-09-30 devnull fields in the
590 b2cfc4e2 2003-09-30 devnull .B Alt
591 b2cfc4e2 2003-09-30 devnull structure are used internally by
592 b2cfc4e2 2003-09-30 devnull .I alt
593 b2cfc4e2 2003-09-30 devnull and need not be initialized.
594 b2cfc4e2 2003-09-30 devnull They are not used between
595 b2cfc4e2 2003-09-30 devnull .I alt
596 b2cfc4e2 2003-09-30 devnull calls.
597 b2cfc4e2 2003-09-30 devnull .PP
598 b2cfc4e2 2003-09-30 devnull .I Chanprint
599 b2cfc4e2 2003-09-30 devnull formats its arguments in the manner of
600 bf8a59fa 2004-04-11 devnull .IR print (3)
601 b2cfc4e2 2003-09-30 devnull and sends the result to the channel
602 b2cfc4e2 2003-09-30 devnull .IR c.
603 b2cfc4e2 2003-09-30 devnull The string delivered by
604 b2cfc4e2 2003-09-30 devnull .I chanprint
605 b2cfc4e2 2003-09-30 devnull is allocated with
606 bf8a59fa 2004-04-11 devnull .IR malloc (3)
607 b2cfc4e2 2003-09-30 devnull and should be freed upon receipt.
608 b2cfc4e2 2003-09-30 devnull .PP
609 b2cfc4e2 2003-09-30 devnull Thread library functions do not return on failure;
610 b2cfc4e2 2003-09-30 devnull if errors occur, the entire program is aborted.
611 b2cfc4e2 2003-09-30 devnull .PP
612 b2cfc4e2 2003-09-30 devnull Threaded programs should use
613 b2cfc4e2 2003-09-30 devnull .I threadnotify
614 b2cfc4e2 2003-09-30 devnull in place of
615 b2cfc4e2 2003-09-30 devnull .I atnotify
616 b2cfc4e2 2003-09-30 devnull (see
617 bf8a59fa 2004-04-11 devnull .IR notify (3)).
618 b2cfc4e2 2003-09-30 devnull .PP
619 b2cfc4e2 2003-09-30 devnull It is safe to use
620 058b0118 2005-01-03 devnull .IR sysfatal (3)
621 b2cfc4e2 2003-09-30 devnull in threaded programs.
622 b2cfc4e2 2003-09-30 devnull .I Sysfatal
623 b2cfc4e2 2003-09-30 devnull will print the error string and call
624 b2cfc4e2 2003-09-30 devnull .IR threadexitsall .
625 b2cfc4e2 2003-09-30 devnull .PP
626 058b0118 2005-01-03 devnull It is not safe to call
627 b2cfc4e2 2003-09-30 devnull .IR rfork
628 058b0118 2005-01-03 devnull in a threaded program, except to call
629 058b0118 2005-01-03 devnull .B rfork(RFNOTEG)
630 058b0118 2005-01-03 devnull from the main proc before any other procs have been created.
631 058b0118 2005-01-03 devnull To create new processes, use
632 058b0118 2005-01-03 devnull .IR proccreate .
633 058b0118 2005-01-03 devnull .\" .PP
634 058b0118 2005-01-03 devnull .\" It is safe to use
635 058b0118 2005-01-03 devnull .\" .IR rfork
636 058b0118 2005-01-03 devnull .\" (see
637 058b0118 2005-01-03 devnull .\" .IR fork (3))
638 058b0118 2005-01-03 devnull .\" to manage the namespace, file descriptors, note group, and environment of a
639 058b0118 2005-01-03 devnull .\" single process.
640 058b0118 2005-01-03 devnull .\" That is, it is safe to call
641 058b0118 2005-01-03 devnull .\" .I rfork
642 058b0118 2005-01-03 devnull .\" with the flags
643 058b0118 2005-01-03 devnull .\" .BR RFNAMEG ,
644 058b0118 2005-01-03 devnull .\" .BR RFFDG ,
645 058b0118 2005-01-03 devnull .\" .BR RFCFDG ,
646 058b0118 2005-01-03 devnull .\" .BR RFNOTEG ,
647 058b0118 2005-01-03 devnull .\" .BR RFENVG ,
648 058b0118 2005-01-03 devnull .\" and
649 058b0118 2005-01-03 devnull .\" .BR RFCENVG.
650 058b0118 2005-01-03 devnull .\" (To create new processes, use
651 058b0118 2005-01-03 devnull .\" .I proccreate
652 058b0118 2005-01-03 devnull .\" and
653 058b0118 2005-01-03 devnull .\" .IR procrfork .)
654 058b0118 2005-01-03 devnull .\" As mentioned above,
655 058b0118 2005-01-03 devnull .\" the thread library depends on all procs being in the
656 058b0118 2005-01-03 devnull .\" same rendezvous group; do not change the rendezvous
657 058b0118 2005-01-03 devnull .\" group with
658 058b0118 2005-01-03 devnull .\" .IR rfork .
659 b2cfc4e2 2003-09-30 devnull .SH FILES
660 c3674de4 2005-01-11 devnull .B \*9/acid/thread
661 b2cfc4e2 2003-09-30 devnull contains useful
662 b2cfc4e2 2003-09-30 devnull .IR acid (1)
663 b2cfc4e2 2003-09-30 devnull functions for debugging threaded programs.
664 b2cfc4e2 2003-09-30 devnull .PP
665 c3674de4 2005-01-11 devnull .B \*9/src/libthread/test
666 058b0118 2005-01-03 devnull contains some example programs.
667 b2cfc4e2 2003-09-30 devnull .SH SOURCE
668 c3674de4 2005-01-11 devnull .B \*9/src/libthread
669 b2cfc4e2 2003-09-30 devnull .SH SEE ALSO
670 bf8a59fa 2004-04-11 devnull .IR intro (3),
671 bf8a59fa 2004-04-11 devnull .IR ioproc (3)
672 058b0118 2005-01-03 devnull .SH BUGS
673 058b0118 2005-01-03 devnull To avoid name conflicts,
674 058b0118 2005-01-03 devnull .IR alt ,
675 058b0118 2005-01-03 devnull .IR nbrecv ,
676 058b0118 2005-01-03 devnull .IR nbrecvp ,
677 058b0118 2005-01-03 devnull .IR nbrecvul ,
678 058b0118 2005-01-03 devnull .IR nbsend ,
679 058b0118 2005-01-03 devnull .IR nbsendp ,
680 058b0118 2005-01-03 devnull .IR nbsendul ,
681 058b0118 2005-01-03 devnull .IR recv ,
682 058b0118 2005-01-03 devnull .IR recvp ,
683 058b0118 2005-01-03 devnull .IR recvul ,
684 058b0118 2005-01-03 devnull .IR send ,
685 058b0118 2005-01-03 devnull .IR sendp ,
686 058b0118 2005-01-03 devnull and
687 058b0118 2005-01-03 devnull .IR sendul
688 058b0118 2005-01-03 devnull are defined as macros that expand to
689 058b0118 2005-01-03 devnull .IR chanalt ,
690 058b0118 2005-01-03 devnull .IR channbrecv ,
691 058b0118 2005-01-03 devnull and so on.
692 c8b6342d 2005-01-13 devnull .I Yield
693 058b0118 2005-01-03 devnull is defined as a macro that expands to
694 058b0118 2005-01-03 devnull .IR threadyield .
695 c8b6342d 2005-01-13 devnull See
696 c8b6342d 2005-01-13 devnull .IR intro (3).
697 058b0118 2005-01-03 devnull .PP
698 058b0118 2005-01-03 devnull The implementation of
699 058b0118 2005-01-03 devnull .I threadnotify
700 058b0118 2005-01-03 devnull may not be correct.
701 33488b09 2005-03-18 devnull .PP
702 33488b09 2005-03-18 devnull There appears to be a race in the Linux NPTL
703 33488b09 2005-03-18 devnull implementation of
704 33488b09 2005-03-18 devnull .I pthread_exit .
705 33488b09 2005-03-18 devnull Call
706 33488b09 2005-03-18 devnull .I threadexitsall
707 33488b09 2005-03-18 devnull rather than coordinating a simultaneous
708 33488b09 2005-03-18 devnull .I threadexits
709 33488b09 2005-03-18 devnull among many threads.