github.com/kidsbmilk/gofronted_all@v0.0.0-20220701224323-6479d5976c5d/libgo/misc/cgo/testsanitizers/testdata/tsan5.go (about)

     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  
     5  package main
     6  
     7  // Check that calls to C.malloc/C.free do not collide with the calls
     8  // made by the os/user package.
     9  
    10  // #cgo CFLAGS: -fsanitize=thread
    11  // #cgo LDFLAGS: -fsanitize=thread
    12  // #include <stdlib.h>
    13  import "C"
    14  
    15  import (
    16  	"fmt"
    17  	"os"
    18  	"os/user"
    19  	"runtime"
    20  	"sync"
    21  )
    22  
    23  func main() {
    24  	u, err := user.Current()
    25  	if err != nil {
    26  		fmt.Fprintln(os.Stderr, err)
    27  		// Let the test pass.
    28  		os.Exit(0)
    29  	}
    30  
    31  	var wg sync.WaitGroup
    32  	for i := 0; i < 20; i++ {
    33  		wg.Add(2)
    34  		go func() {
    35  			defer wg.Done()
    36  			for i := 0; i < 1000; i++ {
    37  				user.Lookup(u.Username)
    38  				runtime.Gosched()
    39  			}
    40  		}()
    41  		go func() {
    42  			defer wg.Done()
    43  			for i := 0; i < 1000; i++ {
    44  				p := C.malloc(C.size_t(len(u.Username) + 1))
    45  				runtime.Gosched()
    46  				C.free(p)
    47  			}
    48  		}()
    49  	}
    50  	wg.Wait()
    51  }