henesyinit from p9f0ed2a895 years agoHistory
678 lines · 14.2 KB · Text
RawDownload
.TH THREAD 2.SH NAMEalt,chancreate,chanfree,chaninit,proccreate,procdata,procexec,procexecl,procrfork,recv,recvp,recvul,send,sendp,sendul,nbrecv,nbrecvp,nbrecvul,nbsend,nbsendp,nbsendul,threadchdir,threadcreate,threaddata,threadexits,threadexitsall,threadgetgrp,threadgetname,threadkill,threadkillgrp,threadmain,threadnonotes,threadpid,threadprint,threadsetgrp,threadsetname,threadwaitchan,yield \- thread and proc management.SH SYNOPSIS.PP.EX.ta 4n +4n +4n +4n +4n +4n +4n#include <u.h>#include <libc.h>#include <thread.h>.sp#define	CHANEND		0#define	CHANSND		1#define	CHANRCV		2#define	CHANNOP		3#define	CHANNOBLK	4.ifn .sp.ift .sp 0.5typedef struct Alt {	Channel	*c;	void	*v;	int	op;	Channel	**tag;	ulong	q;} Alt;.ifn .sp.ift .sp 0.5void  threadmain(int argc, char *argv[]).ift .sp 0.2int   proccreate(void (*f)(void *arg), void *arg,			uint stacksize).ift .sp 0.2int   procrfork(void (*f)(void *arg), void *arg,			uint stacksize, int rforkflag).ift .sp 0.2int   threadcreate(void (*f)(void *arg), void *arg,			uint stacksize).ift .sp 0.2void  yield(void).ift .sp 0.2void  threadexits(char *status).ift .sp 0.2void  threadexitsall(char *status).ift .sp 0.2Channel	*chancreate(int elemsize, int bufsize).ift .sp 0.2int chaninit(Channel *c, int elemsize, int elemcnt).ift .sp 0.2int   alt(Alt alts[]).ift .sp 0.2int   recv(Channel *c, void *v).ift .sp 0.2ulong recvul(Channel *c).ift .sp 0.2void  *recvp(Channel *c).ift .sp 0.2int   nbrecv(Channel *c, void *v).ift .sp 0.2ulong nbrecvul(Channel *c).ift .sp 0.2void  *nbrecvp(Channel *c).ift .sp 0.2int   send(Channel *c, void *v).ift .sp 0.2int   sendul(Channel *c, ulong v).ift .sp 0.2int   sendp(Channel *c, void *v).ift .sp 0.2int   nbsend(Channel *c, void *v).ift .sp 0.2int   nbsendul(Channel *c, ulong v).ift .sp 0.2int   nbsendp(Channel *c, void *v).ift .sp 0.2void  chanfree(Channel *c).ift .sp 0.2void  threadsetname(char *name).ift .sp 0.2char  *threadgetname(void).ift .sp 0.2ulong *procdata(void).ift .sp 0.2ulong *threaddata(void).ift .sp 0.2int   threadgetgrp(void).ift .sp 0.2int   threadsetgrp(int group).ift .sp 0.2void threadkill(int id).ift .sp 0.2void  threadkillgrp(int group).ift .sp 0.2int threadpid(int id).ift .sp 0.2void  procexecl(Channel *pidc, char *file, ...).ift .sp 0.2void  procexec(Channel *pidc, char *file, char *args[]).ift .sp 0.2Channel	*threadwaitchan(void).ift .sp 0.2int   threadprint(int fd, char *format, ...).EE.SH DESCRIPTION.PPThe thread library provides parallel-programming support similar to thatof the languagesAlef and Newsqueak.Threadsandprocscan be created that occupy a shared address space in which they cancommunicate through shared variables and.IR channels .A.B Channelis a buffered or unbuffered FIFO for fixed-size messages.  Procs andthreads can.I sendmessages into the fifo and.I recvmessages from the fifo.  If the fifo is unbuffered, a.I sendoperation blocks until the corresponding.I recvoperation occurs and.IR "vice versa" ..PPA.I procis a Plan 9 process that contains one or more.IR threads .The threads in a proc are coroutines.  Runnable threads are schedulednonpreemptively in a round-robin fashion.  A thread must explicitlyrelinquish control of the processor before another thread in the sameproc is run.  Calls that do this are.IR yield ,.IR send ,.I recv(and the calls related to.I sendand.I recv\- see their descriptions further on),.IR alt ,and.IR threadexits ..PPProcsare scheduled by the operating system.  Threads in different procs,therefore, can preempt one another in arbitrary ways, unless theyexplicitly synchronize their actions using.B qlocks(see.IR lock (2))or channel communication..PPBlocking system calls such as.IR read (2)do not cause another thread in the same proc to be scheduled.All threads in a proc block until the system call finishes..PPThread stacks are in shared memory, making it valid to pass pointers to stack variables between threads and procs..PPThreads in different procs are scheduled independently and preemptively.Data structures shared between threads in different procs need to beprotected by a.IR lock (2)..PPPrograms using threads must replace.B mainby.IR threadmain .The thread library will set up a proc with a single thread and call.IR threadmain ..PPA new proc is created by a call to.IR proccreate .The arguments are a function.I fwith a single.B void*argument to be called in the new proc, the argument.I argto that function, and the size of the stack for the new proc..I Proccreate.\" returns the thread id of the (single) thread in the newly created proc.returns the pid of the newly created proc.Processes are created by calling.I rfork(see .IR fork (2))with flags.B RFPROCand.BR RFMEM ..I Procrforkis like.I proccreatebut uses.I rforkflagsor'ed with.B RFPROCand.BR RFMEM ..PP.I Argcan be used to pass arbitrary data to.IR f .Be aware, however, that.I fruns in a new process and that it can take some time before.I fis scheduled..I Argshould not point to data on the stack of a function that couldreturn before the new process is scheduled..PP.I Threadcreatecreates a new thread in the proc of the calling thread.  The argumentsare the same as those of.IR proccreate ..I Threadcreatereturns a.IR "thread id" ..PP.I Yieldgives up the processor to another thread in the calling proc..PP.I Threadexitscauses the calling thread to be destroyed.  If the thread is the onlyremaining thread in its proc, the proc exits too (using the exit statussupplied to.IR threadexits )..PP.I Threadexitsallkills all threads in the application (and thus all procs) andexits with the status supplied..PP.I Chancreatecreates a buffered or unbuffered.BR Channel .The arguments are thesize of the elements in the channel (must by greater than zero) and thenumber of elements in the fifo.  If the number is zero, the channelis blocking (synchronous).  If it is greater than zero, the channelis buffered and blocks only if the fifo is empty.RI ( recv )or full.RI ( send )..I Chancreatereturns a pointer to the channel created..PP.I Chaninitinitializes an already allocated channel;.BR elemsize ,and.B elemcntare as in.BR chancreate ..PP.I Recvreceives an element from the channel named by its first argument andstores it in the location pointed to by its second argument.  It returns.B 1for success, and.B -1when it was interrupted.If the second argument is null,the received value is ignored..PP.I Recvuland.I recvpreceive an unsigned long or a pointer, respectively, from a channel whoseelement size must be.B sizeof(ulong)or.BR "sizeof(void *)" ..PP.IR Nbrecv ,.IR nbrecvul ,and.I nbrecvpare non-blocking versions of.IR recv ,.IR recvul ,and.IR recvp .Note that.I nbrecvuland.I nbrecvpcan alsoreturn.B 0when the channel is empty.Since.B 0is both a valid data value and an error return,use.I nbrecvinsteadif you wish to distinguish an empty channel from onethat contains a zero element..PP.I Sendsends the value pointed to by the second parameter to the channel pointedto by the first parameter.  If the value pointer is 0,zeroes are sent..I Send.IR yield s,so, if the receiving thread is blocked and in the same proc, it willrun first..PP.IR Sendul ,.IR sendp ,.IR nbsend ,.IR nbsendul ,and.I nbsendpare the specialized and nonblocking versions, analogous to the.I recvfamily of calls..PP.I Altcan be used to recv from or send to one of a number of channels..I Alttakes as its parameter an array of.B Altstructures.  Each of these structures describes a potential send or recvoperation:.B cand.B vare the channel and value pointer (which may be.BR nil )and.B opspecifies the operation:.B CHANSNDfor a send operation,.B CHANRECVfor a recv operation;.B CHANNOPfor no operation\(emthe entry should be skipped.  This can be usefulwhen.I altcalls are used with a varying set of operations.The array of.B Altstructures is terminated by a null entry whose opcode is.B CHANENDor.BR CHANNOBLK ..B CHANNOBLKis a default entry that `fires' when none of the other operations can fire.It makes an.I altstatement non-blocking..PP.I Altlooks for channel operations in the array of.I Altstructures that can proceed.  If there is one, the associated.I sendor.I recvoperation occurs.  If there is more than one, oneof them is chosen at random and the associated communication eventoccurs.  If there are none, and the list is terminated by a.B CHANENDentry,.I altblocks until one of the operations can proceed.  If there are none and the list is terminated by a.B CHANNOBLKentry, that entry will fire..I Altreturns the index of the operation that succeeded.  The fields.B tagand.B qin the.B Altstructure are used by the implementation of alt.  They are not usedbetween.I altcalls..PP.I Chanfreefrees a channel that is no longer used..I Chanfreecan be called by either sender or receiver after the last item has beensent or received.  Freeing the channel will be delayed if there is a threadblocked on it until that thread unblocks (but chanfree returns instantly)..PP.I Threadgetnameand.I threadsetnamecan be used to read or set the name of a thread.  This can be usefulfor debugging..PP.I Threadkillkills the thread identified by its.I "thread ID(as returned by threadcreate) and cleans up its stack..PP.I Procdatareturns a pointer to a per-process location (big enough to hold a pointer)into which applications can store per-proc data..PPThreads have a group number that is inherited on proc and thread creation,much like a process group..I Threadsetgrpcan be used to set this group to an arbitrary integer..I Threadgetgrpreturns the group of the current thread..I Threadkillgrpcan be used to kill all threads in a group..PP.I Threadpidreturns the.I pidof the proc of the thread whose.I idis given.  If.I idequals zero, the pid of the proc of the current thread is returned and if.I iddoes not correspond to an existing thread, \-1 is returned..PP.I Procexecland.I procexecmay only be called from a thread which is the sole thread ina proc.  The thread is removed from the program and the processexecutes independently.The first argument, if not nil, is a channel on which the pidof the exec-ed process will be returned..PPIf the pid channel is set,.I procexecand.I procexeclwill return (i.e., fail) if and only if they also send \-1 on the pid channel..PPThe rest of the arguments are the same as those of.I execland.I exec(see.IR exec (2)).To simplify resource management, these routines first use.IR access (2)to check if the file exists and is executable before attempting to.IR exec (2)it..PP.I Threadwaitchanreturns a channel of.B Waitmsgstructures (see

Lines 1–500 of 678.