github.com/ccccaoqing/test@v0.0.0-20220510085219-3985d23445c0/misc/cgo/life/life.go (about)

     1  // skip
     2  
     3  // Copyright 2010 The Go Authors.  All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package life
     8  
     9  // #include "life.h"
    10  import "C"
    11  
    12  import "unsafe"
    13  
    14  func Run(gen, x, y int, a []int32) {
    15  	n := make([]int32, x*y)
    16  	for i := 0; i < gen; i++ {
    17  		C.Step(C.int(x), C.int(y), (*C.int)(unsafe.Pointer(&a[0])), (*C.int)(unsafe.Pointer(&n[0])))
    18  		copy(a, n)
    19  	}
    20  }
    21  
    22  // Keep the channels visible from Go.
    23  var chans [4]chan bool
    24  
    25  //export GoStart
    26  // Double return value is just for testing.
    27  func GoStart(i, xdim, ydim, xstart, xend, ystart, yend C.int, a *C.int, n *C.int) (int, int) {
    28  	c := make(chan bool, int(C.MYCONST))
    29  	go func() {
    30  		C.DoStep(xdim, ydim, xstart, xend, ystart, yend, a, n)
    31  		c <- true
    32  	}()
    33  	chans[i] = c
    34  	return int(i), int(i + 100)
    35  }
    36  
    37  //export GoWait
    38  func GoWait(i C.int) {
    39  	<-chans[i]
    40  	chans[i] = nil
    41  }