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: ...@@ -61,7 +61,7 @@ runtime.install:
sort.install: sort.install:
strconv.install: bytes.install math.install os.install unicode.install utf8.install strconv.install: bytes.install math.install os.install unicode.install utf8.install
strings.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 syscall.install: sync.install
tabwriter.install: bytes.install container/vector.install io.install os.install utf8.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 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) ...@@ -46,3 +46,14 @@ func GOMAXPROCS(n int)
// Cgocalls returns the number of cgo calls made by the current process. // Cgocalls returns the number of cgo calls made by the current process.
func Cgocalls() int64 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 @@ ...@@ -17,7 +17,7 @@
// See Mullender and Cox, ``Semaphores in Plan 9,'' // See Mullender and Cox, ``Semaphores in Plan 9,''
// http://swtch.com/semaphore.pdf // http://swtch.com/semaphore.pdf
package sync package runtime
#include "runtime.h" #include "runtime.h"
typedef struct Sema Sema; typedef struct Sema Sema;
...@@ -176,10 +176,10 @@ semrelease(uint32 *addr) ...@@ -176,10 +176,10 @@ semrelease(uint32 *addr)
semwakeup(addr); semwakeup(addr);
} }
func semacquire(addr *uint32) { func Semacquire(addr *uint32) {
semacquire(addr); semacquire(addr);
} }
func semrelease(addr *uint32) { func Semrelease(addr *uint32) {
semrelease(addr); 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 @@ ...@@ -8,23 +8,24 @@
// is better done via channels and communication. // is better done via channels and communication.
package sync package sync
func cas(val *int32, old, new int32) bool import "runtime"
func semacquire(*int32)
func semrelease(*int32) func cas(val *uint32, old, new uint32) bool
// A Mutex is a mutual exclusion lock. // A Mutex is a mutual exclusion lock.
// Mutexes can be created as part of other structures; // Mutexes can be created as part of other structures;
// the zero value for a Mutex is an unlocked mutex. // the zero value for a Mutex is an unlocked mutex.
type Mutex struct { type Mutex struct {
key int32; key uint32;
sema int32; sema uint32;
} }
func xadd(val *int32, delta int32) (new int32) { func xadd(val *uint32, delta int32) (new uint32) {
for { for {
v := *val; v := *val;
if cas(val, v, v+delta) { nv := v+uint32(delta);
return v+delta; if cas(val, v, nv) {
return nv;
} }
} }
panic("unreached"); panic("unreached");
...@@ -38,7 +39,7 @@ func (m *Mutex) Lock() { ...@@ -38,7 +39,7 @@ func (m *Mutex) Lock() {
// changed from 0 to 1; we hold lock // changed from 0 to 1; we hold lock
return; return;
} }
semacquire(&m.sema); runtime.Semacquire(&m.sema);
} }
// Unlock unlocks m. // Unlock unlocks m.
...@@ -52,7 +53,7 @@ func (m *Mutex) Unlock() { ...@@ -52,7 +53,7 @@ func (m *Mutex) Unlock() {
// changed from 1 to 0; no contention // changed from 1 to 0; no contention
return; return;
} }
semrelease(&m.sema); runtime.Semrelease(&m.sema);
} }
// Stub implementation of r/w locks. // Stub implementation of r/w locks.
......
...@@ -7,20 +7,21 @@ ...@@ -7,20 +7,21 @@
package sync_test package sync_test
import ( import (
"runtime";
. "sync"; . "sync";
"testing"; "testing";
) )
func HammerSemaphore(s *int32, cdone chan bool) { func HammerSemaphore(s *uint32, cdone chan bool) {
for i := 0; i < 1000; i++ { for i := 0; i < 1000; i++ {
Semacquire(s); runtime.Semacquire(s);
Semrelease(s); runtime.Semrelease(s);
} }
cdone <- true; cdone <- true;
} }
func TestSemaphore(t *testing.T) { func TestSemaphore(t *testing.T) {
s := new(int32); s := new(uint32);
*s = 1; *s = 1;
c := make(chan bool); c := make(chan bool);
for i := 0; i < 10; i++ { 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