tsan2.go 940 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2015 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. // This program produced false race reports when run under the C/C++
  6. // ThreadSanitizer, as it did not understand the synchronization in
  7. // the Go code.
  8. /*
  9. #cgo CFLAGS: -fsanitize=thread
  10. #cgo LDFLAGS: -fsanitize=thread
  11. extern void GoRun(void);
  12. // Yes, you can have definitions if you use //export, as long as they are weak.
  13. int val __attribute__ ((weak));
  14. int run(void) __attribute__ ((weak));
  15. int run() {
  16. val = 1;
  17. GoRun();
  18. return val;
  19. }
  20. void setVal(int) __attribute__ ((weak));
  21. void setVal(int i) {
  22. val = i;
  23. }
  24. */
  25. import "C"
  26. import "runtime"
  27. //export GoRun
  28. func GoRun() {
  29. runtime.LockOSThread()
  30. c := make(chan bool)
  31. go func() {
  32. runtime.LockOSThread()
  33. C.setVal(2)
  34. c <- true
  35. }()
  36. <-c
  37. }
  38. func main() {
  39. if v := C.run(); v != 2 {
  40. panic(v)
  41. }
  42. }