summaryrefslogtreecommitdiffstats
path: root/src/runtime/testdata/testprogcgo/numgoroutine.go
blob: 1b9f202f46c827c5fa87d391921e46cf04915695 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !plan9 && !windows
// +build !plan9,!windows

package main

/*
#include <stddef.h>
#include <pthread.h>

extern void CallbackNumGoroutine();

static void* thread2(void* arg __attribute__ ((unused))) {
	CallbackNumGoroutine();
	return NULL;
}

static void CheckNumGoroutine() {
	pthread_t tid;
	pthread_create(&tid, NULL, thread2, NULL);
	pthread_join(tid, NULL);
}
*/
import "C"

import (
	"fmt"
	"runtime"
	"strings"
)

var baseGoroutines int

func init() {
	register("NumGoroutine", NumGoroutine)
}

func NumGoroutine() {
	// Test that there are just the expected number of goroutines
	// running. Specifically, test that the spare M's goroutine
	// doesn't show up.
	if _, ok := checkNumGoroutine("first", 1+baseGoroutines); !ok {
		return
	}

	// Test that the goroutine for a callback from C appears.
	if C.CheckNumGoroutine(); !callbackok {
		return
	}

	// Make sure we're back to the initial goroutines.
	if _, ok := checkNumGoroutine("third", 1+baseGoroutines); !ok {
		return
	}

	fmt.Println("OK")
}

func checkNumGoroutine(label string, want int) (string, bool) {
	n := runtime.NumGoroutine()
	if n != want {
		fmt.Printf("%s NumGoroutine: want %d; got %d\n", label, want, n)
		return "", false
	}

	sbuf := make([]byte, 32<<10)
	sbuf = sbuf[:runtime.Stack(sbuf, true)]
	n = strings.Count(string(sbuf), "goroutine ")
	if n != want {
		fmt.Printf("%s Stack: want %d; got %d:\n%s\n", label, want, n, string(sbuf))
		return "", false
	}
	return string(sbuf), true
}

var callbackok bool

//export CallbackNumGoroutine
func CallbackNumGoroutine() {
	stk, ok := checkNumGoroutine("second", 2+baseGoroutines)
	if !ok {
		return
	}
	if !strings.Contains(stk, "CallbackNumGoroutine") {
		fmt.Printf("missing CallbackNumGoroutine from stack:\n%s\n", stk)
		return
	}

	callbackok = true
}