github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/testdata/testprogcgo/issue29707.go (about) 1 // Copyright 2011 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 //go:build !plan9 && !windows 6 // +build !plan9,!windows 7 8 // This is for issue #29707 9 10 package main 11 12 /* 13 #include <pthread.h> 14 15 extern void* callbackTraceParser(void*); 16 typedef void* (*cbTraceParser)(void*); 17 18 static void testCallbackTraceParser(cbTraceParser cb) { 19 pthread_t thread_id; 20 pthread_create(&thread_id, NULL, cb, NULL); 21 pthread_join(thread_id, NULL); 22 } 23 */ 24 import "C" 25 26 import ( 27 "bytes" 28 "fmt" 29 traceparser "internal/trace" 30 "runtime/trace" 31 "time" 32 "unsafe" 33 ) 34 35 func init() { 36 register("CgoTraceParser", CgoTraceParser) 37 } 38 39 //export callbackTraceParser 40 func callbackTraceParser(unsafe.Pointer) unsafe.Pointer { 41 time.Sleep(time.Millisecond) 42 return nil 43 } 44 45 func CgoTraceParser() { 46 buf := new(bytes.Buffer) 47 48 trace.Start(buf) 49 C.testCallbackTraceParser(C.cbTraceParser(C.callbackTraceParser)) 50 trace.Stop() 51 52 _, err := traceparser.Parse(buf, "") 53 if err == traceparser.ErrTimeOrder { 54 fmt.Println("ErrTimeOrder") 55 } else if err != nil { 56 fmt.Println("Parse error: ", err) 57 } else { 58 fmt.Println("OK") 59 } 60 }