9front - general discussion about 9front
 help / color / mirror / Atom feed
* kernel: pull in tsemacquire from sources
@ 2012-07-04  2:59 Anthony Martin
  2012-07-14  3:50 ` Anthony Martin
  0 siblings, 1 reply; 5+ messages in thread
From: Anthony Martin @ 2012-07-04  2:59 UTC (permalink / raw)
  To: 9front

# 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: kernel: pull in tsemacquire from sources
  2012-07-04  2:59 kernel: pull in tsemacquire from sources Anthony Martin
@ 2012-07-14  3:50 ` Anthony Martin
  2012-07-14 11:11   ` Julius Schmidt
  0 siblings, 1 reply; 5+ messages in thread
From: Anthony Martin @ 2012-07-14  3:50 UTC (permalink / raw)
  To: 9front

Any interest in this patch? It's slightly out
of date now. Geoff made a few small tweaks
on sources.

Cheers,
  Anthony

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: kernel: pull in tsemacquire from sources
  2012-07-14  3:50 ` Anthony Martin
@ 2012-07-14 11:11   ` Julius Schmidt
  2012-07-14 11:52     ` Anthony Martin
  0 siblings, 1 reply; 5+ messages in thread
From: Julius Schmidt @ 2012-07-14 11:11 UTC (permalink / raw)
  To: 9front

Rationale?

On Fri, 13 Jul 2012, Anthony Martin wrote:

> Any interest in this patch? It's slightly out
> of date now. Geoff made a few small tweaks
> on sources.
>
> Cheers,
>  Anthony
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: kernel: pull in tsemacquire from sources
  2012-07-14 11:11   ` Julius Schmidt
@ 2012-07-14 11:52     ` Anthony Martin
  2012-07-14 12:16       ` Julius Schmidt
  0 siblings, 1 reply; 5+ messages in thread
From: Anthony Martin @ 2012-07-14 11:52 UTC (permalink / raw)
  To: 9front

Julius Schmidt <aiju@phicode.de> once said:
> Rationale?

It's needed for the Go runtime.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: kernel: pull in tsemacquire from sources
  2012-07-14 11:52     ` Anthony Martin
@ 2012-07-14 12:16       ` Julius Schmidt
  0 siblings, 0 replies; 5+ messages in thread
From: Julius Schmidt @ 2012-07-14 12:16 UTC (permalink / raw)
  To: 9front

http://code.google.com/p/plan9front/wiki/golang

but okay, can you update your pull request?

On Sat, 14 Jul 2012, Anthony Martin wrote:

> Julius Schmidt <aiju@phicode.de> once said:
>> Rationale?
>
> It's needed for the Go runtime.
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-07-14 12:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-04  2:59 kernel: pull in tsemacquire from sources Anthony Martin
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

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).