]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: tell race detector about reflectOffs.lock
authorDavid Crawshaw <crawshaw@golang.org>
Wed, 25 May 2016 17:19:11 +0000 (13:19 -0400)
committerDavid Crawshaw <crawshaw@golang.org>
Thu, 26 May 2016 14:43:27 +0000 (14:43 +0000)
Fixes #15832

Change-Id: I6f3f45e3c21edd0e093ecb1d8a067907863478f5
Reviewed-on: https://go-review.googlesource.com/23441
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
src/reflect/all_test.go
src/reflect/export_test.go
src/runtime/runtime1.go
src/runtime/type.go

index c801bfc1ec559fbb60a7a07d077792d998d81bd8..f7cf46daecf524b278e6fd60a0e4d5e4f8bffb4f 100644 (file)
@@ -5722,3 +5722,18 @@ func TestTypeStrings(t *testing.T) {
                }
        }
 }
+
+func TestOffsetLock(t *testing.T) {
+       var wg sync.WaitGroup
+       for i := 0; i < 4; i++ {
+               i := i
+               wg.Add(1)
+               go func() {
+                       for j := 0; j < 50; j++ {
+                               ResolveReflectName(fmt.Sprintf("OffsetLockName:%d:%d", i, j))
+                       }
+                       wg.Done()
+               }()
+       }
+       wg.Wait()
+}
index 00189f3353f19678ce295d41a96c92d6084611be..2cc1530250c084e536ad09ca9d1a69baf4bd9442 100644 (file)
@@ -109,3 +109,7 @@ func IsExported(t Type) bool {
        n := typ.nameOff(typ.str)
        return n.isExported()
 }
+
+func ResolveReflectName(s string) {
+       resolveReflectName(newName(s, "", "", false))
+}
index 90893839045119621a87e2c1981931e8befc5032..302f58de5fa91a9e3724451b962d446c029c9ce8 100644 (file)
@@ -509,7 +509,7 @@ func reflect_resolveTextOff(rtype unsafe.Pointer, off int32) unsafe.Pointer {
 // reflect_addReflectOff adds a pointer to the reflection offset lookup map.
 //go:linkname reflect_addReflectOff reflect.addReflectOff
 func reflect_addReflectOff(ptr unsafe.Pointer) int32 {
-       lock(&reflectOffs.lock)
+       reflectOffsLock()
        if reflectOffs.m == nil {
                reflectOffs.m = make(map[int32]unsafe.Pointer)
                reflectOffs.minv = make(map[unsafe.Pointer]int32)
@@ -522,6 +522,6 @@ func reflect_addReflectOff(ptr unsafe.Pointer) int32 {
                reflectOffs.m[id] = ptr
                reflectOffs.minv[ptr] = id
        }
-       unlock(&reflectOffs.lock)
+       reflectOffsUnlock()
        return id
 }
index 608c601abd2973740aed2dea58a19dfdf21fd25f..d7ec5573a9d851c921e81faaa234529d6a883d39 100644 (file)
@@ -169,6 +169,20 @@ var reflectOffs struct {
        minv map[unsafe.Pointer]int32
 }
 
+func reflectOffsLock() {
+       lock(&reflectOffs.lock)
+       if raceenabled {
+               raceacquire(unsafe.Pointer(&reflectOffs.lock))
+       }
+}
+
+func reflectOffsUnlock() {
+       if raceenabled {
+               racerelease(unsafe.Pointer(&reflectOffs.lock))
+       }
+       unlock(&reflectOffs.lock)
+}
+
 func resolveNameOff(ptrInModule unsafe.Pointer, off nameOff) name {
        if off == 0 {
                return name{}
@@ -182,9 +196,9 @@ func resolveNameOff(ptrInModule unsafe.Pointer, off nameOff) name {
                }
        }
        if md == nil {
-               lock(&reflectOffs.lock)
+               reflectOffsLock()
                res, found := reflectOffs.m[int32(off)]
-               unlock(&reflectOffs.lock)
+               reflectOffsUnlock()
                if !found {
                        println("runtime: nameOff", hex(off), "base", hex(base), "not in ranges:")
                        for next := &firstmoduledata; next != nil; next = next.next {
@@ -219,9 +233,9 @@ func (t *_type) typeOff(off typeOff) *_type {
                }
        }
        if md == nil {
-               lock(&reflectOffs.lock)
+               reflectOffsLock()
                res := reflectOffs.m[int32(off)]
-               unlock(&reflectOffs.lock)
+               reflectOffsUnlock()
                if res == nil {
                        println("runtime: typeOff", hex(off), "base", hex(base), "not in ranges:")
                        for next := &firstmoduledata; next != nil; next = next.next {
@@ -252,9 +266,9 @@ func (t *_type) textOff(off textOff) unsafe.Pointer {
                }
        }
        if md == nil {
-               lock(&reflectOffs.lock)
+               reflectOffsLock()
                res := reflectOffs.m[int32(off)]
-               unlock(&reflectOffs.lock)
+               reflectOffsUnlock()
                if res == nil {
                        println("runtime: textOff", hex(off), "base", hex(base), "not in ranges:")
                        for next := &firstmoduledata; next != nil; next = next.next {