thread 12 KB

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