Blob


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