summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2010-01-19 21:14:15 -0800
committerRuss Cox <rsc@golang.org>2010-01-19 21:14:15 -0800
commitb87e2dc2f56155501b9ac46e136c8015d4f36877 (patch)
treecb822d2710fefc5c8f5c672842cffc2e1de678b4
parent1c09a40de9e42d6a46d36108b3ac421794aa635a (diff)
downloadgolang-b87e2dc2f56155501b9ac46e136c8015d4f36877.tar.gz
runtime: wait to allocate mach semaphores backing Locks until needed
need better management of mach semaphores eventually but this avoids allocating them for uncontended Locks. R=r CC=agl1, golang-dev http://codereview.appspot.com/190079
-rw-r--r--src/pkg/runtime/darwin/thread.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/pkg/runtime/darwin/thread.c b/src/pkg/runtime/darwin/thread.c
index f68b63f7a..2f0bee9e6 100644
--- a/src/pkg/runtime/darwin/thread.c
+++ b/src/pkg/runtime/darwin/thread.c
@@ -60,12 +60,12 @@ lock(Lock *l)
throw("lock count");
m->locks++;
- // Allocate semaphore if needed.
- if(l->sema == 0)
- initsema(&l->sema);
-
- if(xadd(&l->key, 1) > 1) // someone else has it; wait
+ if(xadd(&l->key, 1) > 1) { // someone else has it; wait
+ // Allocate semaphore if needed.
+ if(l->sema == 0)
+ initsema(&l->sema);
mach_semacquire(l->sema);
+ }
}
void
@@ -75,8 +75,12 @@ unlock(Lock *l)
if(m->locks < 0)
throw("lock count");
- if(xadd(&l->key, -1) > 0) // someone else is waiting
+ if(xadd(&l->key, -1) > 0) { // someone else is waiting
+ // Allocate semaphore if needed.
+ if(l->sema == 0)
+ initsema(&l->sema);
mach_semrelease(l->sema);
+ }
}
@@ -87,15 +91,21 @@ unlock(Lock *l)
void
usemacquire(Usema *s)
{
- if((int32)xadd(&s->u, -1) < 0)
+ if((int32)xadd(&s->u, -1) < 0) {
+ if(s->k == 0)
+ initsema(&s->k);
mach_semacquire(s->k);
+ }
}
void
usemrelease(Usema *s)
{
- if((int32)xadd(&s->u, 1) <= 0)
+ if((int32)xadd(&s->u, 1) <= 0) {
+ if(s->k == 0)
+ initsema(&s->k);
mach_semrelease(s->k);
+ }
}
@@ -109,8 +119,6 @@ noteclear(Note *n)
void
notesleep(Note *n)
{
- if(n->sema.k == 0)
- initsema(&n->sema.k);
while(!n->wakeup)
usemacquire(&n->sema);
}
@@ -118,8 +126,6 @@ notesleep(Note *n)
void
notewakeup(Note *n)
{
- if(n->sema.k == 0)
- initsema(&n->sema.k);
n->wakeup = 1;
usemrelease(&n->sema);
}