tsan7.go 708 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. // Setting an environment variable in a cgo program changes the C
  6. // environment. Test that this does not confuse the race detector.
  7. /*
  8. #cgo CFLAGS: -fsanitize=thread
  9. #cgo LDFLAGS: -fsanitize=thread
  10. */
  11. import "C"
  12. import (
  13. "fmt"
  14. "os"
  15. "sync"
  16. "time"
  17. )
  18. func main() {
  19. var wg sync.WaitGroup
  20. var mu sync.Mutex
  21. f := func() {
  22. defer wg.Done()
  23. for i := 0; i < 100; i++ {
  24. time.Sleep(time.Microsecond)
  25. mu.Lock()
  26. s := fmt.Sprint(i)
  27. os.Setenv("TSAN_TEST"+s, s)
  28. mu.Unlock()
  29. }
  30. }
  31. wg.Add(2)
  32. go f()
  33. go f()
  34. wg.Wait()
  35. }