github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/misc/cgo/test/cthread.go (about)

     1  // Copyright 2013 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 cgotest
     6  
     7  // extern void doAdd(int, int);
     8  import "C"
     9  
    10  import (
    11  	"runtime"
    12  	"sync"
    13  	"testing"
    14  )
    15  
    16  var sum struct {
    17  	sync.Mutex
    18  	i int
    19  }
    20  
    21  //export Add
    22  func Add(x int) {
    23  	defer func() {
    24  		recover()
    25  	}()
    26  	sum.Lock()
    27  	sum.i += x
    28  	sum.Unlock()
    29  	var p *int
    30  	*p = 2
    31  }
    32  
    33  func testCthread(t *testing.T) {
    34  	if runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
    35  		t.Skip("the iOS exec wrapper is unable to properly handle the panic from Add")
    36  	}
    37  	sum.i = 0
    38  	C.doAdd(10, 6)
    39  
    40  	want := 10 * (10 - 1) / 2 * 6
    41  	if sum.i != want {
    42  		t.Fatalf("sum=%d, want %d", sum.i, want)
    43  	}
    44  }