Blob


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