thread 11 KB

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