]> Cypherpunks repositories - gostls13.git/commitdiff
publish semacquire and semrelease for use by sync.
authorRuss Cox <rsc@golang.org>
Fri, 16 Oct 2009 00:46:53 +0000 (17:46 -0700)
committerRuss Cox <rsc@golang.org>
Fri, 16 Oct 2009 00:46:53 +0000 (17:46 -0700)
more enforcing package boundaries

R=r
DELTA=46  (13 added, 15 deleted, 18 changed)
OCL=35806
CL=35806

src/pkg/Make.deps
src/pkg/runtime/extern.go
src/pkg/runtime/sema.cgo
src/pkg/sync/internal_test.go [deleted file]
src/pkg/sync/mutex.go
src/pkg/sync/mutex_test.go

index 21e412ba383fd968671615ba3008ed770f42cf05..a90da1aba0745ba9ce7810d294c1f38a5dba0f17 100644 (file)
@@ -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
index 131767aef1fd8eb5e58055fdd5ba145b55d5bf61..70c6f434beb905d70c7fc19b9f1f36a9f7d24d07 100644 (file)
@@ -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)
index 81834ae6dc20950339dd59db9d591a71dc4c4bb4..71395ce771f5b626a568eccdcaa312e9b628a024 100644 (file)
@@ -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);
 }
diff --git a/src/pkg/sync/internal_test.go b/src/pkg/sync/internal_test.go
deleted file mode 100644 (file)
index b365f79..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-// 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);
-}
index 23691aa33e89e4450112ac43165de3d1e93f39e7..309970f1e58f00126cab17bf53fd3645d580bcd5 100644 (file)
@@ -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.
index 2944a20fb92ef8d91efc6ceff01bd9fc49bc1c9f..d7be79fff2deb3c3c3191d0da0b7fd6d93901caf 100644 (file)
@@ -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++ {