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