github.com/s1s1ty/go@v0.0.0-20180207192209-104445e3140f/src/runtime/testdata/testprogcgo/dropm.go (about)

     1  // Copyright 2016 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  // +build !plan9,!windows
     6  
     7  // Test that a sequence of callbacks from C to Go get the same m.
     8  // This failed to be true on arm and arm64, which was the root cause
     9  // of issue 13881.
    10  
    11  package main
    12  
    13  /*
    14  #include <stddef.h>
    15  #include <pthread.h>
    16  
    17  extern void GoCheckM();
    18  
    19  static void* thread(void* arg __attribute__ ((unused))) {
    20  	GoCheckM();
    21  	return NULL;
    22  }
    23  
    24  static void CheckM() {
    25  	pthread_t tid;
    26  	pthread_create(&tid, NULL, thread, NULL);
    27  	pthread_join(tid, NULL);
    28  	pthread_create(&tid, NULL, thread, NULL);
    29  	pthread_join(tid, NULL);
    30  }
    31  */
    32  import "C"
    33  
    34  import (
    35  	"fmt"
    36  	"os"
    37  )
    38  
    39  func init() {
    40  	register("EnsureDropM", EnsureDropM)
    41  }
    42  
    43  var savedM uintptr
    44  
    45  //export GoCheckM
    46  func GoCheckM() {
    47  	m := runtime_getm_for_test()
    48  	if savedM == 0 {
    49  		savedM = m
    50  	} else if savedM != m {
    51  		fmt.Printf("m == %x want %x\n", m, savedM)
    52  		os.Exit(1)
    53  	}
    54  }
    55  
    56  func EnsureDropM() {
    57  	C.CheckM()
    58  	fmt.Println("OK")
    59  }