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