thread 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. .TH THREAD 2
  2. .SH NAME
  3. alt,
  4. chanclose,
  5. chancreate,
  6. chanfree,
  7. chaninit,
  8. chanclosing,
  9. chanprint,
  10. mainstacksize,
  11. proccreate,
  12. procdata,
  13. procexec,
  14. procexecl,
  15. procrfork,
  16. recv,
  17. recvp,
  18. recvul,
  19. send,
  20. sendp,
  21. sendul,
  22. nbrecv,
  23. nbrecvp,
  24. nbrecvul,
  25. nbsend,
  26. nbsendp,
  27. nbsendul,
  28. threadcreate,
  29. threaddata,
  30. threadexits,
  31. threadexitsall,
  32. threadgetgrp,
  33. threadgetname,
  34. threadint,
  35. threadintgrp,
  36. threadkill,
  37. threadkillgrp,
  38. threadmain,
  39. threadnotify,
  40. threadid,
  41. threadpid,
  42. threadsetgrp,
  43. threadsetname,
  44. threadspawn,
  45. threadspawnl,
  46. threadwaitchan,
  47. yield \- thread and proc management
  48. .SH SYNOPSIS
  49. .EX
  50. .ta 4n +4n +4n +4n +4n +4n +4n
  51. #include <u.h>
  52. #include <libc.h>
  53. #include <thread.h>
  54. .sp
  55. typedef enum {
  56. CHANEND,
  57. CHANSND,
  58. CHANRCV,
  59. CHANNOP,
  60. CHANNOBLK,
  61. } ChanOp;
  62. .sp
  63. .ta \w' 'u +\w'Channel 'u
  64. typedef struct Alt Alt;
  65. struct Alt {
  66. Channel *c; /* channel */
  67. void *v; /* pointer to value */
  68. ChanOp op; /* operation */
  69. char *err; /* did the op fail? */
  70. /*
  71. * the next variables are used internally to alt
  72. * they need not be initialized
  73. */
  74. Channel **tag; /* pointer to rendez-vous tag */
  75. int entryno; /* entry number */
  76. };
  77. .fi
  78. .de XX
  79. .ift .sp 0.5
  80. .ifn .sp
  81. ..
  82. .PP
  83. .nf
  84. .ft L
  85. .ta \w'\fLChannel* 'u +4n +4n +4n +4n
  86. void threadmain(int argc, char *argv[])
  87. int mainstacksize
  88. int proccreate(void (*fn)(void*), void *arg, uint stacksize)
  89. int procrfork(void (*fn)(void*), void *arg, uint stacksize,
  90. int rforkflag)
  91. int threadcreate(void (*fn)(void*), void *arg, uint stacksize)
  92. void threadexits(char *status)
  93. void threadexitsall(char *status)
  94. void yield(void)
  95. .XX
  96. int threadid(void)
  97. int threadgrp(void)
  98. int threadsetgrp(int group)
  99. int threadpid(int id)
  100. .XX
  101. int threadint(int id)
  102. void threadintgrp(int group)
  103. void threadkill(int id)
  104. int threadkillgrp(int group)
  105. .XX
  106. void threadsetname(char *name, ...)
  107. char* threadgetname(void)
  108. .XX
  109. void** threaddata(void)
  110. void** procdata(void)
  111. .XX
  112. int chaninit(Channel *c, int elsize, int nel)
  113. Channel* chancreate(int elsize, int nel)
  114. void chanfree(Channel *c)
  115. .XX
  116. int alt(Alt *alts)
  117. int recv(Channel *c, void *v)
  118. void* recvp(Channel *c)
  119. ulong recvul(Channel *c)
  120. int nbrecv(Channel *c, void *v)
  121. void* nbrecvp(Channel *c)
  122. ulong nbrecvul(Channel *c)
  123. int send(Channel *c, void *v)
  124. int sendp(Channel *c, void *v)
  125. int sendul(Channel *c, ulong v)
  126. int nbsend(Channel *c, void *v)
  127. int nbsendp(Channel *c, void *v)
  128. int nbsendul(Channel *c, ulong v)
  129. int chanprint(Channel *c, char *fmt, ...)
  130. int chanclose(Channel *c);
  131. int chanclosing(Channel *c);
  132. .XX
  133. int threadspawnl(int fd[3], char *file, ...)
  134. int threadspawn(int fd[3], char *file, char *args[])
  135. void procexecl(Channel *cpid, char *file, ...)
  136. void procexec(Channel *cpid, char *file, char *args[])
  137. Channel* threadwaitchan(void)
  138. .XX
  139. int threadnotify(int (*f)(void*, char*), int in)
  140. .EE
  141. .SH DESCRIPTION
  142. The thread library provides parallel programming support similar to that
  143. of the languages
  144. Alef and Newsqueak.
  145. .I Threads
  146. and
  147. .I procs
  148. occupy a shared address space,
  149. communicating and synchronizing through
  150. .I channels
  151. and shared variables.
  152. .PP
  153. A
  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. .SS Creation
  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 Procrfork
  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 (2))
  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. .SS Scheduling
  234. The threads in a proc are coroutines, scheduled non-preemptively
  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 procexec ,
  242. .IR procexecl ,
  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. plus these from
  255. .IR lock (2):
  256. .IR qlock ,
  257. .IR rlock ,
  258. .IR wlock ,
  259. .IR rsleep .
  260. Procs are scheduled by the operating system.
  261. Therefore, threads in different procs can preempt one another
  262. in arbitrary ways and should synchronize their
  263. actions using
  264. .B qlocks
  265. (see
  266. .IR lock (2))
  267. or channel communication.
  268. System calls such as
  269. .IR read (2)
  270. block the entire proc;
  271. all threads in a proc block until the system call finishes.
  272. .PP
  273. As mentioned above, each thread has a unique integer thread id.
  274. Thread ids are not reused; they are unique across the life of the program.
  275. .I Threadid
  276. returns the id for the current thread.
  277. Each thread also has a thread group id.
  278. The initial thread has a group id of zero.
  279. Each new thread inherits the group id of
  280. the thread that created it.
  281. .I Threadgrp
  282. returns the group id for the current thread;
  283. .I threadsetgrp
  284. sets it.
  285. .I Threadpid
  286. returns the pid of the Plan 9 process containing
  287. the thread identified by
  288. .IR id ,
  289. or \-1
  290. if no such thread is found.
  291. .PP
  292. .I Threadint
  293. interrupts a thread that is blocked in a channel operation
  294. or system call.
  295. .I Threadintgrp
  296. interrupts all threads with the given group id.
  297. .I Threadkill
  298. marks a thread to die when it next relinquishes the processor
  299. (via one of the calls listed above).
  300. If the thread is blocked in a channel operation or system call,
  301. it is also interrupted.
  302. .I Threadkillgrp
  303. kills all threads with the given group id.
  304. Note that
  305. .I threadkill
  306. and
  307. .I threadkillgrp
  308. will not terminate a thread that never relinquishes
  309. the processor.
  310. .SS Names and per-thread data
  311. Primarily for debugging,
  312. threads can have string names associated with them.
  313. .I Threadgetname
  314. returns the current thread's name;
  315. .I threadsetname
  316. sets it.
  317. The pointer returned by
  318. .I threadgetname
  319. is only valid until the next call to
  320. .IR threadsetname .
  321. .PP
  322. .I Threaddata
  323. returns a pointer to a per-thread pointer
  324. that may be modified by threaded programs for
  325. per-thread storage.
  326. Similarly,
  327. .I procdata
  328. returns a pointer to a per-proc pointer.
  329. .SS Executing new programs
  330. .I Procexecl
  331. and
  332. .I procexec
  333. are threaded analogues of
  334. .I exec
  335. and
  336. .I execl
  337. (see
  338. .IR exec (2));
  339. on success,
  340. they replace the calling thread (which must be the only thread in its proc)
  341. and invoke the external program, never returning.
  342. On error, they return \-1.
  343. If
  344. .I cpid
  345. is not null, the pid of the invoked program
  346. will be sent along
  347. .I cpid
  348. once the program has been started, or \-1 will be sent if an
  349. error occurs.
  350. .I Procexec
  351. and
  352. .I procexecl
  353. will not access their arguments after sending a result
  354. along
  355. .IR cpid .
  356. Thus, programs that malloc the
  357. .I argv
  358. passed to
  359. .I procexec
  360. can safely free it once they have
  361. received the
  362. .I cpid
  363. response.
  364. Note that the mount point
  365. .B /mnt/temp
  366. must exist;
  367. .I procexec(l)
  368. mount pipes there.
  369. .PP
  370. .I Threadspawnl
  371. and
  372. .I threadspawn
  373. are like
  374. .I threadexecl
  375. and
  376. .I threadexec
  377. but do not replace the current thread.
  378. They return the pid of the invoked program on success, or
  379. \-1 on error.
  380. .PP
  381. .I Threadwaitchan
  382. returns a channel of pointers to
  383. .B Waitmsg
  384. structures (see
  385. .IR wait (2)).
  386. When an exec'ed process exits, a pointer to a
  387. .B Waitmsg
  388. is sent to this channel.
  389. These
  390. .B Waitmsg
  391. structures have been allocated with
  392. .IR malloc (2)
  393. and should be freed after use.
  394. .SS Channels
  395. A
  396. .B Channel
  397. is a buffered or unbuffered queue for fixed-size messages.
  398. Procs and threads
  399. .I send
  400. messages into the channel and
  401. .I recv
  402. messages from the channel. If the channel is unbuffered, a
  403. .I send
  404. operation blocks until the corresponding
  405. .I recv
  406. operation occurs and
  407. .IR "vice versa" .
  408. .I Chaninit
  409. initializes a
  410. .B Channel
  411. for messages of size
  412. .I elsize
  413. and with a buffer holding
  414. .I nel
  415. messages.
  416. If
  417. .I nel
  418. is zero, the channel is unbuffered.
  419. .IR Chancreate
  420. allocates a new channel and initializes it.
  421. .I Chanfree
  422. frees a channel that is no longer used.
  423. .I Chanfree
  424. can be called by either sender or receiver after the last item has been
  425. sent or received. Freeing the channel will be delayed if there is a thread
  426. blocked on it until that thread unblocks (but
  427. .I chanfree
  428. returns immediately).
  429. .PP
  430. .I Send
  431. sends the element pointed at by
  432. .I v
  433. to the channel
  434. .IR c .
  435. If
  436. .I v
  437. is null, zeros are sent.
  438. .I Recv
  439. receives an element from
  440. .I c
  441. and stores it in
  442. .IR v .
  443. If
  444. .I v
  445. is null,
  446. the received value is discarded.
  447. .I Send
  448. and
  449. .I recv
  450. return 1 on success, \-1 if interrupted.
  451. .I Nbsend
  452. and
  453. .I nbrecv
  454. behave similarly, but return 0 rather than blocking.
  455. .PP
  456. .IR Sendp ,
  457. .IR nbsendp ,
  458. .IR sendul ,
  459. and
  460. .I nbsendul
  461. send a pointer or an unsigned long; the channel must
  462. have been initialized with the appropriate
  463. .IR elsize .
  464. .IR Recvp ,
  465. .IR nbrecvp ,
  466. .IR recvul ,
  467. and
  468. .I nbrecvul
  469. receive a pointer or an unsigned long;
  470. they return zero when a zero is received,
  471. when interrupted, or
  472. (for
  473. .I nbrecvp
  474. and
  475. .IR nbrecvul )
  476. when the operation would have blocked.
  477. To distinguish between these three cases,
  478. use
  479. .I recv
  480. or
  481. .IR nbrecv .
  482. .PP
  483. .I Alt
  484. can be used to recv from or send to one of a number of channels,
  485. as directed by an array of
  486. .B Alt
  487. structures,
  488. each of which describes a potential send or receive operation.
  489. In an
  490. .B Alt
  491. structure,
  492. .B c
  493. is the channel;
  494. .B v
  495. the value pointer (which may be null); and
  496. .B op
  497. the operation:
  498. .B CHANSND
  499. for a send operation,
  500. .B CHANRCV
  501. for a recv operation;
  502. .B CHANNOP
  503. for no operation
  504. (useful
  505. when
  506. .I alt
  507. is called with a varying set of operations).
  508. The array of
  509. .B Alt
  510. structures is terminated by an entry with
  511. .I op
  512. .B CHANEND
  513. or
  514. .BR CHANNOBLK .
  515. If at least one
  516. .B Alt
  517. structure can proceed, one of them is
  518. chosen at random to be executed.
  519. .I Alt
  520. returns the index of the chosen structure.
  521. If no operations can proceed and the list is terminated with
  522. .BR CHANNOBLK ,
  523. .I alt
  524. returns the index of the terminating
  525. .B CHANNOBLK
  526. structure.
  527. Otherwise,
  528. .I alt
  529. blocks until one of the operations can proceed,
  530. eventually returning the index of the structure executes.
  531. .I Alt
  532. returns \-1 when interrupted.
  533. The
  534. .B tag
  535. and
  536. .B entryno
  537. fields in the
  538. .B Alt
  539. structure are used internally by
  540. .I alt
  541. and need not be initialized.
  542. They are not used between
  543. .I alt
  544. calls.
  545. .PP
  546. .I Chanprint
  547. formats its arguments in the manner of
  548. .IR print (2)
  549. and sends the result to the channel
  550. .IR c .
  551. The string delivered by
  552. .I chanprint
  553. is allocated with
  554. .IR malloc (2)
  555. and should be freed upon receipt.
  556. .PP
  557. .I Chanclose
  558. prevents further elements being sent to the channel
  559. .IR c .
  560. After closing a channel,
  561. .I send
  562. and
  563. .I recv
  564. never block.
  565. .I Send
  566. always
  567. returns \-1.
  568. .I Recv
  569. returns \-1 if the channel is empty.
  570. .I Alt
  571. may choose a
  572. .B CHANSND
  573. or
  574. .B CHANRCV
  575. that failed because the channel was closed.
  576. In this case, the
  577. .B err
  578. field of the
  579. .B Alt
  580. entry points to an error string stating that the
  581. channel was closed and the operation was completed
  582. with failure.
  583. If all entries have been selected and failed because
  584. they were closed,
  585. .I alt
  586. returns \-1.
  587. .SS Errors, notes and resources
  588. Thread library functions do not return on failure;
  589. if errors occur, the entire program is aborted.
  590. .PP
  591. .I Chanclosing
  592. returns \-1 if no one called
  593. .I closed
  594. on the channel, and otherwise
  595. the number of elements still in the channel.
  596. .PP
  597. Threaded programs should use
  598. .I threadnotify
  599. in place of
  600. .I atnotify
  601. (see
  602. .IR notify (2)).
  603. .PP
  604. It is safe to use
  605. .B sysfatal
  606. (see
  607. .IR perror (2))
  608. in threaded programs.
  609. .I Sysfatal
  610. will print the error string and call
  611. .IR threadexitsall .
  612. .PP
  613. It is safe to use
  614. .IR rfork
  615. (see
  616. .IR fork (2))
  617. to manage the namespace, file descriptors, note group, and environment of a
  618. single process.
  619. That is, it is safe to call
  620. .I rfork
  621. with the flags
  622. .BR RFNAMEG ,
  623. .BR RFFDG ,
  624. .BR RFCFDG ,
  625. .BR RFNOTEG ,
  626. .BR RFENVG ,
  627. and
  628. .BR RFCENVG.
  629. (To create new processes, use
  630. .I proccreate
  631. and
  632. .IR procrfork .)
  633. As mentioned above,
  634. the thread library depends on all procs being in the
  635. same rendezvous group; do not change the rendezvous
  636. group with
  637. .IR rfork .
  638. .SH FILES
  639. .TF /sys/lib/acid/thread
  640. .TP
  641. .B /sys/lib/acid/thread
  642. useful
  643. .IR acid (1)
  644. functions for debugging threaded programs.
  645. .TP
  646. .B /sys/src/libthread/example.c
  647. a full example program.
  648. .TP
  649. .B /mnt/temp
  650. a place for
  651. .I procexec
  652. to create pipes.
  653. .SH SOURCE
  654. .B /sys/src/libthread
  655. .SH SEE ALSO
  656. .IR intro (2),
  657. .IR ioproc (2),
  658. .IR lock (2)