ddddddf 发表于 2018-9-20 11:37:56

在golang中使用 cgo,如何让被嵌入的c语言代码调用golang

// Copyright 2011 The Go Authors. All rights reserved.  

  

// Use of this source code is governed by a BSD-style  

  

// license that can be found in the LICENSE file.  

  

  
package cgotest
  

  

  
/*
  

  
void callback(void *f);
  

  
void callGoFoo(void);
  

  
void callGoStackCheck(void);
  

  
void callPanic(void);
  

  
int callGoReturnVal(void);
  

  
int returnAfterGrow(void);
  

  
int returnAfterGrowFromGo(void);
  

  
void callGoWithString(void);
  

  
*/
  

  
import "C"
  

  

  
import (
  

  "path"
  

  "runtime"
  

  "strings"
  

  "sync"
  

  "testing"
  

  "unsafe"
  

  
)
  

  

  
// Pass a func value from nestedCall to goCallback using an integer token.
  

  
var callbackMutex sync.Mutex
  

  
var callbackToken int
  

  
var callbackFuncs = make(mapfunc())
  

  

  
// nestedCall calls into C, back into Go, and finally to f.
  

  
func nestedCall(f func()) {
  

  // callback(x) calls goCallback(x)
  

  callbackMutex.Lock()
  

  callbackToken++
  

  i := callbackToken
  

  callbackFuncs = f
  

  callbackMutex.Unlock()
  

  

  // Pass the address of i because the C function was written to
  

  // take a pointer.We could pass an int if we felt like
  

  // rewriting the C code.
  

  C.callback(unsafe.Pointer(&i))
  

  

  callbackMutex.Lock()
  

  delete(callbackFuncs, i)
  

  callbackMutex.Unlock()
  

  
}
  

  

  
//export goCallback
  

  
func goCallback(p unsafe.Pointer) {
  

  i := *(*int)(p)
  

  

  callbackMutex.Lock()
  

  f := callbackFuncs
  

  callbackMutex.Unlock()
  

  

  if f == nil {
  

  panic("missing callback function")
  

  }
  

  f()
  

  
}
  

  

  
func testCallback(t *testing.T) {
  

  var x = false
  

  nestedCall(func() { x = true })
  

  if !x {
  

  t.Fatal("nestedCall did not call func")
  

  }
  

  
}
  

  

  
func testCallbackGC(t *testing.T) {
  

  nestedCall(runtime.GC)
  

  
}
  

  

  
func testCallbackPanic(t *testing.T) {
  

  // Make sure panic during callback unwinds properly.
  

  if lockedOSThread() {
  

  t.Fatal("locked OS thread on entry to TestCallbackPanic")
  

  }
  

  defer func() {
  

  s := recover()
  

  if s == nil {
  

  t.Fatal("did not panic")
  

  }
  

  if s.(string) != "callback panic" {
  

  t.Fatal("wrong panic:", s)
  

  }
  

  if lockedOSThread() {
  

  t.Fatal("locked OS thread on exit from TestCallbackPanic")
  

  }
  

  }()
  

  nestedCall(func() { panic("callback panic") })
  

  panic("nestedCall returned")
  

  
}
  

  

  
func testCallbackPanicLoop(t *testing.T) {
  

  // Make sure we don't blow out m->g0 stack.
  

  for i := 0; i < 100000; i++ {
  

  testCallbackPanic(t)
  

  }
  

  
}
  

  

  
func testCallbackPanicLocked(t *testing.T) {
  

  runtime.LockOSThread()
  

  defer runtime.UnlockOSThread()
  

  

  if !lockedOSThread() {
  

  t.Fatal("runtime.LockOSThread didn't")
  

  }
  

  defer func() {
  

  s := recover()
  

  if s == nil {
  

  t.Fatal("did not panic")
  

  }
  

  if s.(string) != "callback panic" {
  

  t.Fatal("wrong panic:", s)
  

  }
  

  if !lockedOSThread() {
  

  t.Fatal("lost lock on OS thread after panic")
  

  }
  

  }()
  

  nestedCall(func() { panic("callback panic") })
  

  panic("nestedCall returned")
  

  
}
  

  

  
// Callback with zero arguments used to make the stack misaligned,
  

  
// which broke the garbage collector and other things.
  

  
func testZeroArgCallback(t *testing.T) {
  

  defer func() {
  

  s := recover()
  

  if s != nil {
  

  t.Fatal("panic during callback:", s)
  

  }
  

  }()
  

  C.callGoFoo()
  

  
}
  

  

  
//export goFoo
  

  
func goFoo() {
  

  x := 1
  

  for i := 0; i < 10000; i++ {
  

  // variadic call mallocs + writes to
  

  variadic(x, x, x)
  

  if x != 1 {
  

  panic("bad x")
  

  }
  

  }
  

  
}
  

  

  
func variadic(x ...interface{}) {}
  

  

  
func testBlocking(t *testing.T) {
  

  c := make(chan int)
  

  go func() {
  

  for i := 0; i < 10; i++ {
  

  c
页: [1]
查看完整版本: 在golang中使用 cgo,如何让被嵌入的c语言代码调用golang