106 lines · 1.8 KB · Text
RawDownload
.TH SEMACQUIRE 2.SH NAMEsemacquire, semrelease \- user level semaphores.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.PP.Bint semacquire(long *addr, int block);.PP.Blong semrelease(long *addr, long count);.SH DESCRIPTION.I Semacquireand .I semreleasefacilitate scheduling between processes sharing memory.Processes arrange to share memory by using.I rforkwith the.B RFMEMflag(see.IR fork (2)), .IR segattach (2),or.IR thread (2)..PPThe semaphore's value is the integer pointed at by.IR addr ..I Semacquireatomically waits until the semaphore has a positive valueand then decrements that value.It returns 1 if the semaphore was acquired and \-1 on error(e.g., if it was interrupted).If.I blockis zeroand the semaphore is not immediately available,.I semacquirereturns 0 instead of waiting..I Semreleaseadds .I countto the semaphore's valueand returns the new value..PP.I Semacquireand.I semreleasecan be thought of as efficient, correct replacements for:.IP.EXintsemacquire(long *addr, int block){	while(*addr == 0){		if(!block)			return 0;		if(interrupted)			return -1;	}	--*addr;	return 1;} intsemrelease(long *addr, int count){	return *addr += count;}.EE.PPLike.IR rendezvous (2),.I semacquireand.I semreleaseare not typically used directly.Instead, they are intended to be used to coordinatescheduling in higher-level abstractions such aslocks, rendezvous points, and channels(see.IR lock (2)and.IR thread (2)).Also like.I rendezvous ,.I semacquireand.I semreleasecannot be used to coordinate between threadsin a single process.Use locks, rendezvous points, or channels instead..SH SOURCE.B /sys/src/9/port/sysproc.c.SH SEE ALSO.IR fork (2),.IR lock (2),.IR rendezvous (2),.IR segattach (2),.IR thread (2).SH DIAGNOSTICSThese functions set.IR errstr .