From f5752848fde774c5c16c5e58f15558a253a03119 Mon Sep 17 00:00:00 2001 From: Akshat Kumar Date: Wed, 16 May 2012 15:09:28 -0700 Subject: [PATCH] pkg/runtime: Fix semasleep on Plan 9 With the timed semacquire patch (kernel-tsemacquire) for Plan 9, we can now properly do a timed wait for the semaphore, in semasleep. R=golang-dev, rsc, rminnich, ality, r CC=0intro, golang-dev, john, mirtchovski https://golang.org/cl/6197046 --- src/pkg/runtime/os_plan9.h | 1 + src/pkg/runtime/sys_plan9_386.s | 5 +++++ src/pkg/runtime/thread_plan9.c | 28 +++++----------------------- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/pkg/runtime/os_plan9.h b/src/pkg/runtime/os_plan9.h index cc14cc8c5e..6f1f0bb8eb 100644 --- a/src/pkg/runtime/os_plan9.h +++ b/src/pkg/runtime/os_plan9.h @@ -13,6 +13,7 @@ int32 runtime·brk_(void*); int32 runtime·sleep(int32 ms); int32 runtime·rfork(int32 flags, void *stk, M *m, G *g, void (*fn)(void)); int32 runtime·plan9_semacquire(uint32 *addr, int32 block); +int32 runtime·plan9_tsemacquire(uint32 *addr, int32 ms); int32 runtime·plan9_semrelease(uint32 *addr, int32 count); int32 runtime·notify(void (*fn)(void*, byte*)); int32 runtime·noted(int32); diff --git a/src/pkg/runtime/sys_plan9_386.s b/src/pkg/runtime/sys_plan9_386.s index f3e56d689b..cdcf0b2798 100644 --- a/src/pkg/runtime/sys_plan9_386.s +++ b/src/pkg/runtime/sys_plan9_386.s @@ -49,6 +49,11 @@ TEXT runtime·plan9_semacquire(SB),7,$0 INT $64 RET +TEXT runtime·plan9_tsemacquire(SB),7,$0 + MOVL $52, AX + INT $64 + RET + TEXT runtime·notify(SB),7,$0 MOVL $28, AX INT $64 diff --git a/src/pkg/runtime/thread_plan9.c b/src/pkg/runtime/thread_plan9.c index e951e31c09..57d535713d 100644 --- a/src/pkg/runtime/thread_plan9.c +++ b/src/pkg/runtime/thread_plan9.c @@ -43,7 +43,7 @@ static int32 getpid(void) { byte b[20], *c; - int32 fd, n; + int32 fd; runtime·memclr(b, sizeof(b)); fd = runtime·open((byte*)"#c/pid", 0); @@ -276,36 +276,18 @@ runtime·semasleep(int64 ns) int32 ms; if(ns >= 0) { - // TODO: Plan 9 needs a new system call, tsemacquire. - // The kernel implementation is the same as semacquire - // except with a tsleep and check for timeout. - // It would be great if the implementation returned the - // value that was added to the semaphore, so that on - // timeout the return value would be 0, on success 1. - // Then the error string does not have to be parsed - // to detect timeout. - // - // If a negative time indicates no timeout, then - // semacquire can be implemented (in the kernel) - // as tsemacquire(p, v, -1). - runtime·throw("semasleep: timed sleep not implemented on Plan 9"); - - /* - if(ns < 0) - ms = -1; - else if(ns/1000 > 0x7fffffffll) + if(ns/1000000 > 0x7fffffffll) ms = 0x7fffffff; else - ms = ns/1000; - ret = runtime·plan9_tsemacquire(&m->waitsemacount, 1, ms); + ms = ns/1000000; + ret = runtime·plan9_tsemacquire(&m->waitsemacount, ms); if(ret == 1) return 0; // success return -1; // timeout or interrupted - */ } while(runtime·plan9_semacquire(&m->waitsemacount, 1) < 0) { - /* interrupted; try again */ + /* interrupted; try again (c.f. lock_sema.c) */ } return 0; // success } -- 2.48.1