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