9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Anthony Martin <ality@pbrane.org>
To: 9front@googlegroups.com
Subject: kernel: pull in tsemacquire from sources
Date: Tue, 3 Jul 2012 19:59:26 -0700	[thread overview]
Message-ID: <20120704025926.GA492@dinah> (raw)

# HG changeset patch
# User Anthony Martin <ality@pbrane.org>
# 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 <u.h>
 .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

             reply	other threads:[~2012-07-04  2:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-04  2:59 Anthony Martin [this message]
2012-07-14  3:50 ` Anthony Martin
2012-07-14 11:11   ` Julius Schmidt
2012-07-14 11:52     ` Anthony Martin
2012-07-14 12:16       ` Julius Schmidt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120704025926.GA492@dinah \
    --to=ality@pbrane.org \
    --cc=9front@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).