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