thread 12 KB

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