github.com/aloncn/graphics-go@v0.0.1/src/runtime/testdata/testprogcgo/aprof.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  // Test that SIGPROF received in C code does not crash the process
     8  // looking for the C code's func pointer.
     9  
    10  // The test fails when the function is the first C function.
    11  // The exported functions are the first C functions, so we use that.
    12  
    13  // extern void GoNop();
    14  import "C"
    15  
    16  import (
    17  	"bytes"
    18  	"fmt"
    19  	"runtime/pprof"
    20  )
    21  
    22  func init() {
    23  	register("CgoCCodeSIGPROF", CgoCCodeSIGPROF)
    24  }
    25  
    26  //export GoNop
    27  func GoNop() {}
    28  
    29  func CgoCCodeSIGPROF() {
    30  	c := make(chan bool)
    31  	go func() {
    32  		for {
    33  			<-c
    34  			for i := 0; i < 1e7; i++ {
    35  				C.GoNop()
    36  			}
    37  			c <- true
    38  		}
    39  	}()
    40  
    41  	var buf bytes.Buffer
    42  	pprof.StartCPUProfile(&buf)
    43  	c <- true
    44  	<-c
    45  	pprof.StopCPUProfile()
    46  
    47  	fmt.Println("OK")
    48  }