1// Copyright 2012 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 cgo
6
7package runtime_test
8
9import (
10	"testing"
11)
12
13func TestCgoCrashHandler(t *testing.T) {
14	testCrashHandler(t, true)
15}
16
17func TestCgoSignalDeadlock(t *testing.T) {
18	/* gccgo does not have a go command
19	got := executeTest(t, cgoSignalDeadlockSource, nil)
20	want := "OK\n"
21	if got != want {
22		t.Fatalf("expected %q, but got %q", want, got)
23	}
24	*/
25}
26
27const cgoSignalDeadlockSource = `
28package main
29
30import "C"
31
32import (
33	"fmt"
34	"runtime"
35	"time"
36)
37
38func main() {
39	runtime.GOMAXPROCS(100)
40	ping := make(chan bool)
41	go func() {
42		for i := 0; ; i++ {
43			runtime.Gosched()
44			select {
45			case done := <-ping:
46				if done {
47					ping <- true
48					return
49				}
50				ping <- true
51			default:
52			}
53			func() {
54				defer func() {
55					recover()
56				}()
57				var s *string
58				*s = ""
59			}()
60		}
61	}()
62	time.Sleep(time.Millisecond)
63	for i := 0; i < 64; i++ {
64		go func() {
65			runtime.LockOSThread()
66			select {}
67		}()
68		go func() {
69			runtime.LockOSThread()
70			select {}
71		}()
72		time.Sleep(time.Millisecond)
73		ping <- false
74		select {
75		case <-ping:
76		case <-time.After(time.Second):
77			fmt.Printf("HANG\n")
78			return
79		}
80	}
81	ping <- true
82	select {
83	case <-ping:
84	case <-time.After(time.Second):
85		fmt.Printf("HANG\n")
86		return
87	}
88	fmt.Printf("OK\n")
89}
90`
91