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