tsan.go 700 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. int val;
  12. int getVal() {
  13. return val;
  14. }
  15. void setVal(int i) {
  16. val = i;
  17. }
  18. */
  19. import "C"
  20. import (
  21. "runtime"
  22. )
  23. func main() {
  24. runtime.LockOSThread()
  25. C.setVal(1)
  26. c := make(chan bool)
  27. go func() {
  28. runtime.LockOSThread()
  29. C.setVal(2)
  30. c <- true
  31. }()
  32. <-c
  33. if v := C.getVal(); v != 2 {
  34. panic(v)
  35. }
  36. }