tsan6.go 817 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. package main
  5. // Check that writes to Go allocated memory, with Go synchronization,
  6. // do not look like a race.
  7. /*
  8. #cgo CFLAGS: -fsanitize=thread
  9. #cgo LDFLAGS: -fsanitize=thread
  10. void f(char *p) {
  11. *p = 1;
  12. }
  13. */
  14. import "C"
  15. import (
  16. "runtime"
  17. "sync"
  18. )
  19. func main() {
  20. var wg sync.WaitGroup
  21. var mu sync.Mutex
  22. c := make(chan []C.char, 100)
  23. for i := 0; i < 10; i++ {
  24. wg.Add(2)
  25. go func() {
  26. defer wg.Done()
  27. for i := 0; i < 100; i++ {
  28. c <- make([]C.char, 4096)
  29. runtime.Gosched()
  30. }
  31. }()
  32. go func() {
  33. defer wg.Done()
  34. for i := 0; i < 100; i++ {
  35. p := &(<-c)[0]
  36. mu.Lock()
  37. C.f(p)
  38. mu.Unlock()
  39. }
  40. }()
  41. }
  42. wg.Wait()
  43. }