Source file src/cmd/cgo/internal/testsanitizers/testdata/tsan2.go

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

View as plain text