libgo: update to 1.15.6 release
[official-gcc.git] / libgo / go / runtime / testdata / testprogcgo / needmdeadlock.go
blob5a9c359006d7af6a26dd0e39110c2775fb3cfce2
1 // Copyright 2020 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.
5 // +build !plan9,!windows
7 package main
9 // This is for issue #42207.
10 // During a call to needm we could get a SIGCHLD signal
11 // which would itself call needm, causing a deadlock.
14 #include <signal.h>
15 #include <pthread.h>
16 #include <sched.h>
17 #include <unistd.h>
19 extern void GoNeedM();
21 #define SIGNALERS 10
23 static void* needmSignalThread(void* p) {
24 pthread_t* pt = (pthread_t*)(p);
25 int i;
27 for (i = 0; i < 100; i++) {
28 if (pthread_kill(*pt, SIGCHLD) < 0) {
29 return NULL;
31 usleep(1);
33 return NULL;
36 // We don't need many calls, as the deadlock is only likely
37 // to occur the first couple of times that needm is called.
38 // After that there will likely be an extra M available.
39 #define CALLS 10
41 static void* needmCallbackThread(void* p) {
42 int i;
44 for (i = 0; i < SIGNALERS; i++) {
45 sched_yield(); // Help the signal threads get started.
47 for (i = 0; i < CALLS; i++) {
48 GoNeedM();
50 return NULL;
53 static void runNeedmSignalThread() {
54 int i;
55 pthread_t caller;
56 pthread_t s[SIGNALERS];
58 pthread_create(&caller, NULL, needmCallbackThread, NULL);
59 for (i = 0; i < SIGNALERS; i++) {
60 pthread_create(&s[i], NULL, needmSignalThread, &caller);
62 for (i = 0; i < SIGNALERS; i++) {
63 pthread_join(s[i], NULL);
65 pthread_join(caller, NULL);
68 import "C"
70 import (
71 "fmt"
72 "os"
73 "time"
76 func init() {
77 register("NeedmDeadlock", NeedmDeadlock)
80 //export GoNeedM
81 func GoNeedM() {
84 func NeedmDeadlock() {
85 // The failure symptom is that the program hangs because of a
86 // deadlock in needm, so set an alarm.
87 go func() {
88 time.Sleep(5 * time.Second)
89 fmt.Println("Hung for 5 seconds")
90 os.Exit(1)
91 }()
93 C.runNeedmSignalThread()
94 fmt.Println("OK")