From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by 10.229.40.144 with SMTP id k16csp148396qce; Tue, 3 Jul 2012 19:59:32 -0700 (PDT) Return-Path: <9front+bncCI7xme-pExCT487_BBoEuhCRyw@googlegroups.com> Received-SPF: pass (google.com: domain of 9front+bncCI7xme-pExCT487_BBoEuhCRyw@googlegroups.com designates 10.68.200.170 as permitted sender) client-ip=10.68.200.170; Authentication-Results: mr.google.com; spf=pass (google.com: domain of 9front+bncCI7xme-pExCT487_BBoEuhCRyw@googlegroups.com designates 10.68.200.170 as permitted sender) smtp.mail=9front+bncCI7xme-pExCT487_BBoEuhCRyw@googlegroups.com; dkim=pass header.i=9front+bncCI7xme-pExCT487_BBoEuhCRyw@googlegroups.com Received: from mr.google.com ([10.68.200.170]) by 10.68.200.170 with SMTP id jt10mr3112334pbc.15.1341370771984 (num_hops = 1); Tue, 03 Jul 2012 19:59:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=beta; h=x-beenthere:received-spf:date:from:to:subject:message-id :mime-version:x-original-sender:x-original-authentication-results :reply-to:precedence:mailing-list:list-id:x-google-group-id :list-post:list-help:list-archive:sender:list-subscribe :list-unsubscribe:content-type:content-disposition; bh=hL824vcl3vRvZnmyvmpx68Vh770LmPOcS6Of6WTABsU=; b=wuAmbbq4JiavQM34zhCVvCTK3KLrI7AF4fPCb9IZJrGXonoEY4DfiCQkByqNQdqEhG iDHs9s3Ht9n7We2LryYsmb2knr12da8lh/gqZxP7xSn90VpROtGLC7mu/uYU/ii4PeC/ wyJbfKWrTl/zm5im+Gb9AKjnnjRsFCtegVdfI= Received: by 10.68.200.170 with SMTP id jt10mr806684pbc.15.1341370771940; Tue, 03 Jul 2012 19:59:31 -0700 (PDT) X-BeenThere: 9front@googlegroups.com Received: by 10.68.117.141 with SMTP id ke13ls25436495pbb.6.gmail; Tue, 03 Jul 2012 19:59:30 -0700 (PDT) Received: by 10.68.227.67 with SMTP id ry3mr4957588pbc.8.1341370770863; Tue, 03 Jul 2012 19:59:30 -0700 (PDT) Received: by 10.68.227.67 with SMTP id ry3mr4957584pbc.8.1341370770822; Tue, 03 Jul 2012 19:59:30 -0700 (PDT) Received: from mxout-08.mxes.net (mxout-08.mxes.net. [216.86.168.183]) by gmr-mx.google.com with ESMTPS id tf10si479747pbc.0.2012.07.03.19.59.30 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 03 Jul 2012 19:59:30 -0700 (PDT) Received-SPF: neutral (google.com: 216.86.168.183 is neither permitted nor denied by best guess record for domain of ality@pbrane.org) client-ip=216.86.168.183; Received: from localhost (unknown [69.226.99.14]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by smtp.mxes.net (Postfix) with ESMTPSA id EA90750A65 for <9front@googlegroups.com>; Tue, 3 Jul 2012 22:59:28 -0400 (EDT) Date: Tue, 3 Jul 2012 19:59:26 -0700 From: Anthony Martin To: 9front@googlegroups.com Subject: kernel: pull in tsemacquire from sources Message-ID: <20120704025926.GA492@dinah> MIME-Version: 1.0 X-Original-Sender: ality@pbrane.org X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 216.86.168.183 is neither permitted nor denied by best guess record for domain of ality@pbrane.org) smtp.mail=ality@pbrane.org Reply-To: 9front@googlegroups.com Precedence: list Mailing-list: list 9front@googlegroups.com; contact 9front+owners@googlegroups.com List-ID: <9front.googlegroups.com> X-Google-Group-Id: 831096995978 List-Post: , List-Help: , List-Archive: Sender: 9front@googlegroups.com List-Subscribe: , List-Unsubscribe: , Content-Type: text/plain; charset=us-ascii Content-Disposition: inline # HG changeset patch # User Anthony Martin # Date 1341370509 25200 # Node ID c227fb8d125d3d3d3e1379f5d3b7645bdf426469 # Parent 2f76c939970a8f8b1f8cf21482e3d1d89bfdb2ce kernel: pull in tsemacquire from sources diff -r 2f76c939970a -r c227fb8d125d sys/include/libc.h --- a/sys/include/libc.h Wed Jul 04 01:32:49 2012 +0200 +++ b/sys/include/libc.h Tue Jul 03 19:55:09 2012 -0700 @@ -670,6 +670,7 @@ extern long semrelease(long*, long); extern int sleep(long); extern int stat(char*, uchar*, int); +extern int tsemacquire(long*, ulong); extern Waitmsg* wait(void); extern int waitpid(void); extern long write(int, void*, long); diff -r 2f76c939970a -r c227fb8d125d sys/man/2/semacquire --- a/sys/man/2/semacquire Wed Jul 04 01:32:49 2012 +0200 +++ b/sys/man/2/semacquire Tue Jul 03 19:55:09 2012 -0700 @@ -1,6 +1,6 @@ .TH SEMACQUIRE 2 .SH NAME -semacquire, semrelease \- user level semaphores +semacquire, tsemacquire, semrelease - user level semaphores .SH SYNOPSIS .B #include .br @@ -10,10 +10,14 @@ int semacquire(long *addr, int block); .PP .B +int tsemacquire(long *addr, ulong ms); +.PP +.B long semrelease(long *addr, long count); .SH DESCRIPTION -.I Semacquire -and +.IR Semacquire , +.IR tsemacquire , +and .I semrelease facilitate scheduling between processes sharing memory. Processes arrange to share memory by using @@ -22,7 +26,7 @@ .B RFMEM flag (see -.IR fork (2)), +.IR fork (2)), .IR segattach (2), or .IR thread (2). @@ -32,21 +36,30 @@ .I Semacquire atomically waits until the semaphore has a positive value and then decrements that value. -It returns 1 if the semaphore was acquired and \-1 on error -(e.g., if it was interrupted). If .I block is zero and the semaphore is not immediately available, .I semacquire returns 0 instead of waiting. +.I Tsemacquire +only waits +.I ms +milliseconds for the semaphore to attain a positive value +and, if available in that time, decrements that value. +It returns 0 otherwise. +Both functions return 1 if the semaphore was acquired +and -1 on error +(e.g., if they were interrupted). .I Semrelease -adds +adds .I count to the semaphore's value and returns the new value. .PP .I Semacquire +(and analogously for +.IR tsemacquire ) and .I semrelease can be thought of as efficient, correct replacements for: @@ -74,7 +87,8 @@ .PP Like .IR rendezvous (2), -.I semacquire +.IR semacquire , +.IR tsemacquire , and .I semrelease are not typically used directly. @@ -86,8 +100,9 @@ and .IR thread (2)). Also like -.I rendezvous , -.I semacquire +.IR rendezvous , +.IR semacquire , +.IR tsemacquire , and .I semrelease cannot be used to coordinate between threads diff -r 2f76c939970a -r c227fb8d125d sys/src/9/port/syscallfmt.c --- a/sys/src/9/port/syscallfmt.c Wed Jul 04 01:32:49 2012 +0200 +++ b/sys/src/9/port/syscallfmt.c Tue Jul 03 19:55:09 2012 -0700 @@ -231,6 +231,11 @@ i[0] = va_arg(list, int); fmtprint(&fmt, "%#p %d", v, i[0]); break; + case TSEMACQUIRE: + v = va_arg(list, long*); + l = va_arg(list, ulong); + fmtprint(&fmt, "%#p %ld", v, l); + break; case SEEK: v = va_arg(list, vlong*); i[0] = va_arg(list, int); diff -r 2f76c939970a -r c227fb8d125d sys/src/9/port/sysproc.c --- a/sys/src/9/port/sysproc.c Wed Jul 04 01:32:49 2012 +0200 +++ b/sys/src/9/port/sysproc.c Tue Jul 03 19:55:09 2012 -0700 @@ -1034,6 +1034,50 @@ return 1; } +/* Acquire semaphore or time-out */ +static int +tsemacquire(Segment *s, long *addr, ulong ms) +{ + int acquired, timedout; + ulong t; + Sema phore; + + if(canacquire(addr)) + return 1; + if(ms == 0) + return 0; + acquired = timedout = 0; + semqueue(s, addr, &phore); + for(;;){ + phore.waiting = 1; + coherence(); + if(canacquire(addr)){ + acquired = 1; + break; + } + if(waserror()) + break; + t = m->ticks; + tsleep(&phore, semawoke, &phore, ms); + if(TK2MS(m->ticks - t) >= ms){ + timedout = 1; + poperror(); + break; + } + ms -= TK2MS(m->ticks - t); + poperror(); + } + semdequeue(s, &phore); + coherence(); /* not strictly necessary due to lock in semdequeue */ + if(!phore.waiting) + semwakeup(s, addr, 1); + if(timedout) + return 0; + if(!acquired) + nexterror(); + return 1; +} + long syssemacquire(ulong *arg) { @@ -1054,6 +1098,25 @@ } long +systsemacquire(ulong *arg) +{ + long *addr; + ulong ms; + Segment *s; + + validaddr(arg[0], sizeof(long), 1); + evenaddr(arg[0]); + addr = (long*)arg[0]; + ms = arg[1]; + + if((s = seg(up, (ulong)addr, 0)) == nil) + error(Ebadarg); + if(*addr < 0) + error(Ebadarg); + return tsemacquire(s, addr, ms); +} + +long syssemrelease(ulong *arg) { long *addr, delta; diff -r 2f76c939970a -r c227fb8d125d sys/src/9/port/systab.h --- a/sys/src/9/port/systab.h Wed Jul 04 01:32:49 2012 +0200 +++ b/sys/src/9/port/systab.h Tue Jul 03 19:55:09 2012 -0700 @@ -52,6 +52,7 @@ Syscall sysawait; Syscall syspread; Syscall syspwrite; +Syscall systsemacquire; Syscall sysdeath; Syscall *systab[]={ @@ -105,6 +106,7 @@ [AWAIT] sysawait, [PREAD] syspread, [PWRITE] syspwrite, + [TSEMACQUIRE] systsemacquire, }; char *sysctab[]={ @@ -158,6 +160,7 @@ [AWAIT] "Await", [PREAD] "Pread", [PWRITE] "Pwrite", + [TSEMACQUIRE] "Tsemacquire", }; int nsyscall = (sizeof systab/sizeof systab[0]); diff -r 2f76c939970a -r c227fb8d125d sys/src/libc/9syscall/sys.h --- a/sys/src/libc/9syscall/sys.h Wed Jul 04 01:32:49 2012 +0200 +++ b/sys/src/libc/9syscall/sys.h Tue Jul 03 19:55:09 2012 -0700 @@ -48,3 +48,4 @@ #define AWAIT 47 #define PREAD 50 #define PWRITE 51 +#define TSEMACQUIRE 52