Commit 5bbd4c2f authored by Russ Cox's avatar Russ Cox

publish semacquire and semrelease for use by sync.

more enforcing package boundaries

R=r
DELTA=46  (13 added, 15 deleted, 18 changed)
OCL=35806
CL=35806
parent 2c029ded
......@@ -61,7 +61,7 @@ runtime.install:
sort.install:
strconv.install: bytes.install math.install os.install unicode.install utf8.install
strings.install: os.install unicode.install utf8.install
sync.install:
sync.install: runtime.install
syscall.install: sync.install
tabwriter.install: bytes.install container/vector.install io.install os.install utf8.install
template.install: bytes.install container/vector.install fmt.install io.install os.install reflect.install runtime.install strings.install
......
......@@ -46,3 +46,14 @@ func GOMAXPROCS(n int)
// Cgocalls returns the number of cgo calls made by the current process.
func Cgocalls() int64
// Semacquire waits until *s > 0 and then atomically decrements it.
// It is intended as a simple sleep primitive for use by the synchronization
// library and should not be used directly.
func Semacquire(s *uint32)
// Semrelease atomically increments *s and notifies a waiting goroutine
// if one is blocked in Semacquire.
// It is intended as a simple wakeup primitive for use by the synchronization
// library and should not be used directly.
func Semrelease(s *uint32)
......@@ -17,7 +17,7 @@
// See Mullender and Cox, ``Semaphores in Plan 9,''
// http://swtch.com/semaphore.pdf
package sync
package runtime
#include "runtime.h"
typedef struct Sema Sema;
......@@ -176,10 +176,10 @@ semrelease(uint32 *addr)
semwakeup(addr);
}
func semacquire(addr *uint32) {
func Semacquire(addr *uint32) {
semacquire(addr);
}
func semrelease(addr *uint32) {
func Semrelease(addr *uint32) {
semrelease(addr);
}
// 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.
// expose internals for testing
package sync
func Semacquire(s *int32) {
semacquire(s);
}
func Semrelease(s *int32) {
semrelease(s);
}
......@@ -8,23 +8,24 @@
// is better done via channels and communication.
package sync
func cas(val *int32, old, new int32) bool
func semacquire(*int32)
func semrelease(*int32)
import "runtime"
func cas(val *uint32, old, new uint32) bool
// A Mutex is a mutual exclusion lock.
// Mutexes can be created as part of other structures;
// the zero value for a Mutex is an unlocked mutex.
type Mutex struct {
key int32;
sema int32;
key uint32;
sema uint32;
}
func xadd(val *int32, delta int32) (new int32) {
func xadd(val *uint32, delta int32) (new uint32) {
for {
v := *val;
if cas(val, v, v+delta) {
return v+delta;
nv := v+uint32(delta);
if cas(val, v, nv) {
return nv;
}
}
panic("unreached");
......@@ -38,7 +39,7 @@ func (m *Mutex) Lock() {
// changed from 0 to 1; we hold lock
return;
}
semacquire(&m.sema);
runtime.Semacquire(&m.sema);
}
// Unlock unlocks m.
......@@ -52,7 +53,7 @@ func (m *Mutex) Unlock() {
// changed from 1 to 0; no contention
return;
}
semrelease(&m.sema);
runtime.Semrelease(&m.sema);
}
// Stub implementation of r/w locks.
......
......@@ -7,20 +7,21 @@
package sync_test
import (
"runtime";
. "sync";
"testing";
)
func HammerSemaphore(s *int32, cdone chan bool) {
func HammerSemaphore(s *uint32, cdone chan bool) {
for i := 0; i < 1000; i++ {
Semacquire(s);
Semrelease(s);
runtime.Semacquire(s);
runtime.Semrelease(s);
}
cdone <- true;
}
func TestSemaphore(t *testing.T) {
s := new(int32);
s := new(uint32);
*s = 1;
c := make(chan bool);
for i := 0; i < 10; i++ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment