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