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