2008-12-04 13:51:36 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Semaphore implementation exposed to Go.
|
|
|
|
// Intended use is provide a sleep and wakeup
|
|
|
|
// primitive that can be used in the contended case
|
|
|
|
// of other synchronization primitives.
|
|
|
|
// Thus it targets the same goal as Linux's futex,
|
|
|
|
// but it has much simpler semantics.
|
|
|
|
//
|
|
|
|
// That is, don't think of these as semaphores.
|
|
|
|
// Think of them as a way to implement sleep and wakeup
|
|
|
|
// such that every sleep is paired with a single wakeup,
|
|
|
|
// even if, due to races, the wakeup happens before the sleep.
|
|
|
|
//
|
|
|
|
// See Mullender and Cox, ``Semaphores in Plan 9,''
|
|
|
|
// http://swtch.com/semaphore.pdf
|
|
|
|
|
2009-10-15 18:46:53 -06:00
|
|
|
package runtime
|
2008-12-04 13:51:36 -07:00
|
|
|
#include "runtime.h"
|
|
|
|
|
|
|
|
typedef struct Sema Sema;
|
|
|
|
struct Sema
|
|
|
|
{
|
2011-06-28 13:09:53 -06:00
|
|
|
uint32 volatile *addr;
|
2008-12-04 13:51:36 -07:00
|
|
|
G *g;
|
|
|
|
Sema *prev;
|
|
|
|
Sema *next;
|
|
|
|
};
|
|
|
|
|
2011-06-28 13:09:53 -06:00
|
|
|
typedef struct SemaRoot SemaRoot;
|
|
|
|
struct SemaRoot
|
|
|
|
{
|
|
|
|
Lock;
|
|
|
|
Sema *head;
|
|
|
|
Sema *tail;
|
|
|
|
// Number of waiters. Read w/o the lock.
|
|
|
|
uint32 volatile nwait;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Prime to not correlate with any user patterns.
|
|
|
|
#define SEMTABLESZ 251
|
|
|
|
|
|
|
|
static union
|
|
|
|
{
|
|
|
|
SemaRoot;
|
|
|
|
// Modern processors tend to have 64-byte cache lines,
|
|
|
|
// potentially with 128-byte effective cache line size for reading.
|
|
|
|
// While there are hypothetical architectures
|
|
|
|
// with 16-4096 byte cache lines, 128 looks like a good compromise.
|
|
|
|
uint8 pad[128];
|
|
|
|
} semtable[SEMTABLESZ];
|
|
|
|
|
|
|
|
static SemaRoot*
|
|
|
|
semroot(uint32 *addr)
|
|
|
|
{
|
|
|
|
return &semtable[((uintptr)addr >> 3) % SEMTABLESZ];
|
|
|
|
}
|
2008-12-04 13:51:36 -07:00
|
|
|
|
|
|
|
static void
|
2011-06-28 13:09:53 -06:00
|
|
|
semqueue(SemaRoot *root, uint32 volatile *addr, Sema *s)
|
2008-12-04 13:51:36 -07:00
|
|
|
{
|
2011-06-28 13:09:53 -06:00
|
|
|
s->g = g;
|
2008-12-04 13:51:36 -07:00
|
|
|
s->addr = addr;
|
|
|
|
s->next = nil;
|
2011-06-28 13:09:53 -06:00
|
|
|
s->prev = root->tail;
|
|
|
|
if(root->tail)
|
|
|
|
root->tail->next = s;
|
2008-12-04 13:51:36 -07:00
|
|
|
else
|
2011-06-28 13:09:53 -06:00
|
|
|
root->head = s;
|
|
|
|
root->tail = s;
|
2008-12-04 13:51:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-06-28 13:09:53 -06:00
|
|
|
semdequeue(SemaRoot *root, Sema *s)
|
2008-12-04 13:51:36 -07:00
|
|
|
{
|
|
|
|
if(s->next)
|
|
|
|
s->next->prev = s->prev;
|
|
|
|
else
|
2011-06-28 13:09:53 -06:00
|
|
|
root->tail = s->prev;
|
2008-12-04 13:51:36 -07:00
|
|
|
if(s->prev)
|
|
|
|
s->prev->next = s->next;
|
|
|
|
else
|
2011-06-28 13:09:53 -06:00
|
|
|
root->head = s->next;
|
2008-12-04 13:51:36 -07:00
|
|
|
s->prev = nil;
|
|
|
|
s->next = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32
|
|
|
|
cansemacquire(uint32 *addr)
|
|
|
|
{
|
|
|
|
uint32 v;
|
|
|
|
|
2011-06-28 13:09:53 -06:00
|
|
|
while((v = runtime·atomicload(addr)) > 0)
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
if(runtime·cas(addr, v, v-1))
|
2008-12-04 13:51:36 -07:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-28 13:09:53 -06:00
|
|
|
runtime·semacquire(uint32 volatile *addr)
|
2008-12-04 13:51:36 -07:00
|
|
|
{
|
|
|
|
Sema s;
|
2011-06-28 13:09:53 -06:00
|
|
|
SemaRoot *root;
|
2008-12-04 13:51:36 -07:00
|
|
|
|
|
|
|
// Easy case.
|
|
|
|
if(cansemacquire(addr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Harder case:
|
2011-06-28 13:09:53 -06:00
|
|
|
// increment waiter count
|
|
|
|
// try cansemacquire one more time, return if succeeded
|
|
|
|
// enqueue itself as a waiter
|
|
|
|
// sleep
|
|
|
|
// (waiter descriptor is dequeued by signaler)
|
|
|
|
root = semroot(addr);
|
2008-12-04 13:51:36 -07:00
|
|
|
for(;;) {
|
2011-06-28 13:09:53 -06:00
|
|
|
runtime·lock(root);
|
|
|
|
// Add ourselves to nwait to disable "easy case" in semrelease.
|
|
|
|
runtime·xadd(&root->nwait, 1);
|
|
|
|
// Check cansemacquire to avoid missed wakeup.
|
2008-12-04 13:51:36 -07:00
|
|
|
if(cansemacquire(addr)) {
|
2011-06-28 13:09:53 -06:00
|
|
|
runtime·xadd(&root->nwait, -1);
|
|
|
|
runtime·unlock(root);
|
|
|
|
return;
|
2008-12-04 13:51:36 -07:00
|
|
|
}
|
2011-06-28 13:09:53 -06:00
|
|
|
// Any semrelease after the cansemacquire knows we're waiting
|
|
|
|
// (we set nwait above), so go to sleep.
|
|
|
|
semqueue(root, addr, &s);
|
|
|
|
g->status = Gwaiting;
|
|
|
|
runtime·unlock(root);
|
|
|
|
runtime·gosched();
|
|
|
|
if(cansemacquire(addr))
|
|
|
|
return;
|
2008-12-04 13:51:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-28 13:09:53 -06:00
|
|
|
runtime·semrelease(uint32 volatile *addr)
|
2008-12-04 13:51:36 -07:00
|
|
|
{
|
2011-06-28 13:09:53 -06:00
|
|
|
Sema *s;
|
|
|
|
SemaRoot *root;
|
2008-12-04 13:51:36 -07:00
|
|
|
|
2011-06-28 13:09:53 -06:00
|
|
|
root = semroot(addr);
|
|
|
|
runtime·xadd(addr, 1);
|
|
|
|
|
|
|
|
// Easy case: no waiters?
|
|
|
|
// This check must happen after the xadd, to avoid a missed wakeup
|
|
|
|
// (see loop in semacquire).
|
|
|
|
if(runtime·atomicload(&root->nwait) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Harder case: search for a waiter and wake it.
|
|
|
|
runtime·lock(root);
|
|
|
|
if(runtime·atomicload(&root->nwait) == 0) {
|
|
|
|
// The count is already consumed by another goroutine,
|
|
|
|
// so no need to wake up another goroutine.
|
|
|
|
runtime·unlock(root);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(s = root->head; s; s = s->next) {
|
|
|
|
if(s->addr == addr) {
|
|
|
|
runtime·xadd(&root->nwait, -1);
|
|
|
|
semdequeue(root, s);
|
2008-12-04 13:51:36 -07:00
|
|
|
break;
|
2011-06-28 13:09:53 -06:00
|
|
|
}
|
2008-12-04 13:51:36 -07:00
|
|
|
}
|
2011-06-28 13:09:53 -06:00
|
|
|
runtime·unlock(root);
|
|
|
|
if(s)
|
|
|
|
runtime·ready(s->g);
|
2008-12-04 13:51:36 -07:00
|
|
|
}
|
2009-06-30 21:01:50 -06:00
|
|
|
|
2009-10-15 18:46:53 -06:00
|
|
|
func Semacquire(addr *uint32) {
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·semacquire(addr);
|
2009-06-30 21:01:50 -06:00
|
|
|
}
|
|
|
|
|
2009-10-15 18:46:53 -06:00
|
|
|
func Semrelease(addr *uint32) {
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·semrelease(addr);
|
2009-06-30 21:01:50 -06:00
|
|
|
}
|