| 1 |
// Copyright 2016 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
// Check that writes to Go allocated memory, with Go synchronization, |
| 8 |
// do not look like a race. |
| 9 |
|
| 10 |
/* |
| 11 |
#cgo CFLAGS: -fsanitize=thread |
| 12 |
#cgo LDFLAGS: -fsanitize=thread |
| 13 |
|
| 14 |
void f(char *p) { |
| 15 |
*p = 1; |
| 16 |
} |
| 17 |
*/ |
| 18 |
import "C" |
| 19 |
|
| 20 |
import ( |
| 21 |
"runtime" |
| 22 |
"sync" |
| 23 |
) |
| 24 |
|
| 25 |
func main() { |
| 26 |
var wg sync.WaitGroup |
| 27 |
var mu sync.Mutex |
| 28 |
c := make(chan []C.char, 100) |
| 29 |
for i := 0; i < 10; i++ { |
| 30 |
wg.Add(2) |
| 31 |
go func() { |
| 32 |
defer wg.Done() |
| 33 |
for i := 0; i < 100; i++ { |
| 34 |
c <- make([]C.char, 4096) |
| 35 |
runtime.Gosched() |
| 36 |
} |
| 37 |
}() |
| 38 |
go func() { |
| 39 |
defer wg.Done() |
| 40 |
for i := 0; i < 100; i++ { |
| 41 |
p := &(<-c)[0] |
| 42 |
mu.Lock() |
| 43 |
C.f(p) |
| 44 |
mu.Unlock() |
| 45 |
} |
| 46 |
}() |
| 47 |
} |
| 48 |
wg.Wait() |
| 49 |
} |