life.go 898 B

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